add random bouncing

This commit is contained in:
Shylie 2023-04-10 15:45:27 -04:00
parent 9ab042c485
commit 73ca255664
2 changed files with 58 additions and 33 deletions

View File

@ -13,9 +13,6 @@ GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO);
constexpr u32 FRAMEBUFFER_TRANSFER_FLAGS = constexpr u32 FRAMEBUFFER_TRANSFER_FLAGS =
GX_TRANSFER_RAW_COPY(1); GX_TRANSFER_RAW_COPY(1);
constexpr u32 PREV_FRAME_CLEAR_DATA_SIZE = 240 * 400 * 4;
u32 PREV_FRAME_CLEAR_DATA[PREV_FRAME_CLEAR_DATA_SIZE];
struct vertex struct vertex
{ {
float st[2]; float st[2];
@ -62,8 +59,9 @@ static DVLB_s* vshaderDVLB;
static shaderProgram_s program; static shaderProgram_s program;
static s8 spheresUniformLocation; static s8 spheresUniformLocation;
static s8 sphereColorsUniformLocation; static s8 sphereColorsUniformLocation;
static s8 randLocation;
static constexpr unsigned int VERTEX_COUNT_W = 90; static constexpr unsigned int VERTEX_COUNT_W = 150;
static constexpr unsigned int VERTEX_COUNT_H = 10 * VERTEX_COUNT_W / 6; static constexpr unsigned int VERTEX_COUNT_H = 10 * VERTEX_COUNT_W / 6;
static constexpr unsigned int VERTEX_COUNT = VERTEX_COUNT_W * VERTEX_COUNT_H; static constexpr unsigned int VERTEX_COUNT = VERTEX_COUNT_W * VERTEX_COUNT_H;
@ -89,6 +87,22 @@ static float rand01()
return static_cast<float>(rand()) / RAND_MAX; return static_cast<float>(rand()) / RAND_MAX;
} }
static C3D_FVec randvec()
{
C3D_FVec out;
out.w = rand01();
do
{
out.x = rand01() * 2 - 1;
out.y = rand01() * 2 - 1;
out.z = rand01() * 2 - 1;
}
while (FVec3_Magnitude(out) > 1.0f);
return out;
}
static void setupVertices() static void setupVertices()
{ {
for (unsigned int x = 0; x < VERTEX_COUNT_W; x++) for (unsigned int x = 0; x < VERTEX_COUNT_W; x++)
@ -114,15 +128,17 @@ static void setupVertices()
} }
memcpy(vboData, vertexList, sizeof(vertexList)); memcpy(vboData, vertexList, sizeof(vertexList));
constexpr int NUM_RAND = 4;
C3D_FVec* randPtr = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, randLocation, NUM_RAND);
for (int i = 0; i < NUM_RAND; i++)
{
randPtr[i] = randvec();
}
} }
static void sceneInit() static void sceneInit()
{ {
for (unsigned int i = 0; i < PREV_FRAME_CLEAR_DATA_SIZE; i++)
{
PREV_FRAME_CLEAR_DATA[i] = 0xFFFFFFFF;
}
vboData = linearAlloc(sizeof(vertexList)); vboData = linearAlloc(sizeof(vertexList));
unsigned int v = 0; unsigned int v = 0;
@ -146,6 +162,7 @@ static void sceneInit()
spheresUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "spheres"); spheresUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "spheres");
sphereColorsUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "sphereColors"); sphereColorsUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "sphereColors");
randLocation = shaderInstanceGetUniformLocation(program.vertexShader, "rand");
C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); C3D_AttrInfo* attrInfo = C3D_GetAttrInfo();
AttrInfo_Init(attrInfo); AttrInfo_Init(attrInfo);
@ -173,13 +190,13 @@ static void sceneInit()
constexpr u16 NUM_SPHERES = 3; constexpr u16 NUM_SPHERES = 3;
C3D_FVec* spheres = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, spheresUniformLocation, NUM_SPHERES); C3D_FVec* spheres = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, spheresUniformLocation, NUM_SPHERES);
spheres[0] = FVec4_New(0.0f, -100.5f, -1.0f, 100.0f); spheres[0] = FVec4_New(0.0f, -100.5f, -1.0f, 100.0f);
spheres[1] = FVec4_New(0.5f, 0.0f, -1.0f, 0.5f); spheres[1] = FVec4_New(0.6f, 0.0f, -1.0f, 0.5f);
spheres[2] = FVec4_New(-0.5f, 0.0f, -1.0f, 0.5f); spheres[2] = FVec4_New(-0.6f, 0.0f, -1.0f, 0.5f);
C3D_FVec* sphereColors = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, sphereColorsUniformLocation, NUM_SPHERES); C3D_FVec* sphereColors = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, sphereColorsUniformLocation, NUM_SPHERES);
sphereColors[0] = FVec3_New(0.7f, 0.3f, 0.3f); sphereColors[0] = FVec3_New(0.9f, 0.3f, 0.3f);
sphereColors[1] = FVec3_New(0.3f, 0.7f, 0.3f); sphereColors[1] = FVec3_New(0.3f, 0.9f, 0.3f);
sphereColors[2] = FVec3_New(0.3f, 0.3f, 0.7f); sphereColors[2] = FVec3_New(0.3f, 0.3f, 0.9f);
} }
static void sceneRender() static void sceneRender()
@ -328,6 +345,7 @@ int main(int argc, char* argv[])
sceneExit(); sceneExit();
C3D_TexDelete(&prevFrame); C3D_TexDelete(&prevFrame);
C3D_RenderTargetDelete(target);
C3D_Fini(); C3D_Fini();
gfxExit(); gfxExit();

View File

@ -7,7 +7,7 @@
.alias far myconst.wwww .alias far myconst.wwww
.alias noHit myconst2.yyyy .alias noHit myconst2.yyyy
.consti bounceLoopParams(4, 0, 1, 0) .consti bounceLoopParams(3, 0, 1, 0)
.consti calcSphereLoopParams(2, 0, 1, 0) .consti calcSphereLoopParams(2, 0, 1, 0)
.setb b0 true .setb b0 true
@ -20,6 +20,9 @@
; material albedo ; material albedo
.fvec sphereColors[3] .fvec sphereColors[3]
; random numbers
.fvec rand[4]
.in inOrigin v0 .in inOrigin v0
.in inLowerLeftCorner v1 .in inLowerLeftCorner v1
.in inHorizontal v2 .in inHorizontal v2
@ -54,6 +57,9 @@
; calculate light bounces ; calculate light bounces
for bounceLoopParams for bounceLoopParams
; setup random numbers for this iteration
mov r11, rand[aL]
; reset max ray distance ; reset max ray distance
mov r4.w, far mov r4.w, far
@ -147,7 +153,7 @@
rcp r3.w, r3.w rcp r3.w, r3.w
mul r7.xyz, r7.xyz, r3.w mul r7.xyz, r7.xyz, r3.w
; multiply color by albedo ; set albedo
mov r10.xyz, sphereColors[aL].xyz mov r10.xyz, sphereColors[aL].xyz
; early exit label ; early exit label
@ -165,10 +171,10 @@
mul r4.xyz, r4.xyz, r10.xyz mul r4.xyz, r4.xyz, r10.xyz
; set r1 to new ray origin ; set r1 to new ray origin
add r1.xyz, r5.xyz, r7.xyz mov r1.xyz, r5.xyz
; set r2 to new ray direction ; set r2 to new ray direction
mov r2.xyz, r7.xyz add r2.xyz, r7.xyz, r11.xyz
.end .end
noHitLabel: noHitLabel:
@ -181,6 +187,9 @@
end end
.end .end
; Calculate Sphere Intersection
; -----------------------------
;
; Inputs ; Inputs
; ------ ; ------
; r1.xyz: ray origin ; r1.xyz: ray origin
@ -188,6 +197,7 @@
; r3.xyz: sphere origin ; r3.xyz: sphere origin
; r3.w: sphere radius ; r3.w: sphere radius
; r4.w: min distance ; r4.w: min distance
; r11.xyzw: random numbers
; ;
; Outputs ; Outputs
; ------- ; -------
@ -201,6 +211,3 @@
; r7.xyz: hit normal ; r7.xyz: hit normal
; r8.xyzw: used for calculations ; r8.xyzw: used for calculations
; r9.xyzw: used for calculations ; r9.xyzw: used for calculations
;
;.proc calcSphere
;.end