Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
natevm committed Dec 22, 2024
1 parent 3e882cb commit 1bf7d5a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 181 deletions.
50 changes: 11 additions & 39 deletions samples/s2-hitPrograms/s2-0-triangles/deviceCode.slang
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,19 @@ struct Payload {
float3 color;
};

// This closest hit program will be called when rays hit triangles.
// Here, we can fetch per-geometry data, process that data, and send
// it back to our ray generation program.
//
// The first parameter here is the name of our entry point.
//
// The second is the type and name of the shader record. A shader record
// can be thought of as the parameters passed to this kernel.
//
// The third is the type of the ray payload structure. We use the ray payload
// to pass data between this program and our ray generation program.
//
// The fourth is the type of the intersection attributes structure.
// For triangles, this is always a struct containing two floats
// called "barycentrics", which we use to interpolate per-vertex
// values.
// This closest hit shader runs when a ray hits a triangle.
// It processes per-geometry data and communicates with the ray generation shader.
// - The first parameter is the shader record type, representing SBT parameters for this shader.
// - The second is the ray payload type, used for passing data between shaders.
// - The third is the intersection attributes. For triangles, these are two "barycentrics", used
// for interpolating per-vertex values.
[shader("closesthit")]
void TriangleMesh(uniform TrianglesGeomData record, inout Payload payload, in float2 bc) {
payload.color = float3(bc.x, bc.y, 1.0 - (bc.x + bc.y));
}

#define AA 3

// This ray generation program will kick off the ray tracing process,
// generating rays and tracing them into the world.
//
// The first parameter here is the name of our entry point.
//
// The second is the type and name of the shader record. A shader record
// can be thought of as the parameters passed to this kernel.
[shader("raygeneration")]
void raygen(uniform RayGenData record) {
Payload payload;
Expand All @@ -53,7 +36,7 @@ void raygen(uniform RayGenData record) {
// camera matrix
float3 ww = normalize(ta - ro);
float3 uu = normalize(cross(ww, float3(0.0, -1.0, 0.0)));
float3 vv = normalize(cross(uu, ww));
float3 vv = normalize(cross(uu, ww));

float3 tot = float3(0.0);

Expand All @@ -67,21 +50,15 @@ void raygen(uniform RayGenData record) {
// create view ray
float3 rd = normalize(p.x * uu + p.y * vv + 3.0 * ww);

// Trace the ray into the scene
RayDesc rayDesc;
rayDesc.Origin = ro;
rayDesc.Direction = rd;
rayDesc.TMin = 0.0;
rayDesc.TMax = 10000.0;
TraceRay(world, // the tree
RAY_FLAG_NONE, // ray flags
0xff, // instance inclusion mask
0, // ray type
1, // number of ray types
0, // miss index
rayDesc, // the ray to trace
payload // the payload IO
);
TraceRay(world, RAY_FLAG_NONE, 0xff, 0, 1, 0, rayDesc, payload);

// Accumulate the color
tot += payload.color;
}
tot /= float(AA * AA);
Expand All @@ -90,12 +67,7 @@ void raygen(uniform RayGenData record) {
record.frameBuffer[fbOfs] = gprt::make_bgra(tot);
}

// A background with a vignette effect.
[shader("miss")]
void miss(inout Payload payload) {
float2 resolution = DispatchRaysDimensions().xy;
float2 fragCoord = DispatchRaysIndex().xy;
float2 p = (-resolution.xy + 2.0 * fragCoord) / resolution.y;
float3 col = float3(0.08) * (1.0 - 0.3 * length(p)) + 0.02 * WorldRayDirection().y;
payload.color = col;
payload.color = float3(0.0);
}
24 changes: 2 additions & 22 deletions samples/s2-hitPrograms/s2-0-triangles/sharedCode.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
// MIT License

// Copyright (c) 2022 Nathan V. Morrical

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "gprt.h"

#define AA 3 // used for antialiasing

/* variables available to all programs */

/* variables for the triangle mesh geometry */
Expand Down
26 changes: 5 additions & 21 deletions samples/s2-hitPrograms/s2-1-spheres/deviceCode.slang
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ void SphereClosestHit(uniform SphereGeomData record, inout Payload payload) {
payload.color = NDotV * col;
}

#define AA 3

// This ray generation program will kick off the ray tracing process,
// generating rays and tracing them into the world.
[shader("raygeneration")]
Expand Down Expand Up @@ -61,40 +59,26 @@ void raygen(uniform RayGenData record) {
// create view ray
float3 rd = normalize(p.x * uu + p.y * vv + 3.0 * ww);

// Trace the ray into the scene
RayDesc rayDesc;
rayDesc.Origin = ro;
rayDesc.Direction = rd;
rayDesc.TMin = 0.0;
rayDesc.TMax = 10000.0;
TraceRay(world, // the tree
RAY_FLAG_NONE, // ray flags
0xff, // instance inclusion mask
0, // ray type
1, // number of ray types
0, // miss index
rayDesc, // the ray to trace
payload // the payload IO
);
TraceRay(world, RAY_FLAG_NONE, 0xff, 0, 1, 0, rayDesc, payload);

// Accumulate the color
tot += payload.color;
}
}

tot /= float(AA * AA);

// dither to remove banding in the background
tot += fract(sin(pixelID.x * float3(13, 1, 11) + pixelID.y * float3(1, 7, 5)) * 158.391832) / 255.0;

const int fbOfs = pixelID.x + iResolution.x * pixelID.y;
record.frameBuffer[fbOfs] = gprt::make_bgra(tot);
}

// A background with a vignette effect.
[shader("miss")]
void miss(inout Payload payload) {
float2 resolution = DispatchRaysDimensions().xy;
float2 fragCoord = DispatchRaysIndex().xy;
float2 p = (-resolution.xy + 2.0 * fragCoord) / resolution.y;
float3 col = float3(0.08) * (1.0 - 0.3 * length(p)) + 0.02 * WorldRayDirection().y;
payload.color = col;
}
payload.color = float3(0.0);
}
24 changes: 2 additions & 22 deletions samples/s2-hitPrograms/s2-1-spheres/sharedCode.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
// MIT License

// Copyright (c) 2022 Nathan V. Morrical

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "gprt.h"

#define AA 3 // used for antialiasing

/* variables available to all programs */

/* variables for the sphere geometry */
Expand Down
26 changes: 5 additions & 21 deletions samples/s2-hitPrograms/s2-2-lss/deviceCode.slang
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ void LSSClosestHit(uniform LSSGeomData record, inout Payload payload, in float u
payload.color = col;
}

#define AA 3

// This ray generation program will kick off the ray tracing process,
// generating rays and tracing them into the world.
[shader("raygeneration")]
Expand Down Expand Up @@ -74,40 +72,26 @@ void raygen(uniform RayGenData record) {
// create view ray
float3 rd = normalize(p.x * uu + p.y * vv + 6.0 * ww);

// Trace the ray into the scene
RayDesc rayDesc;
rayDesc.Origin = ro;
rayDesc.Direction = rd;
rayDesc.TMin = 0.0;
rayDesc.TMax = 10000.0;
TraceRay(world, // the tree
RAY_FLAG_NONE, // ray flags
0xff, // instance inclusion mask
0, // ray type
1, // number of ray types
0, // miss index
rayDesc, // the ray to trace
payload // the payload IO
);
TraceRay(world, RAY_FLAG_NONE, 0xff, 0, 1, 0, rayDesc, payload);

// Accumulate the color
tot += payload.color;
}
}

tot /= float(AA * AA);

// dither to remove banding in the background
tot += fract(sin(pixelID.x * float3(13, 1, 11) + pixelID.y * float3(1, 7, 5)) * 158.391832) / 255.0;

const int fbOfs = pixelID.x + iResolution.x * pixelID.y;
record.frameBuffer[fbOfs] = gprt::make_bgra(tot);
}

// A background with a vignette effect.
[shader("miss")]
void miss(inout Payload payload) {
float2 resolution = DispatchRaysDimensions().xy;
float2 fragCoord = DispatchRaysIndex().xy;
float2 p = (-resolution.xy + 2.0 * fragCoord) / resolution.y;
float3 col = float3(0.08) * (1.0 - 0.3 * length(p)) + 0.02 * WorldRayDirection().y;
payload.color = col;
}
payload.color = float3(0.0);
}
24 changes: 2 additions & 22 deletions samples/s2-hitPrograms/s2-2-lss/sharedCode.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
// MIT License

// Copyright (c) 2022 Nathan V. Morrical

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "gprt.h"

#define AA 3 // used for antialiasing

/* variables available to all programs */

/* variables for the line-swept sphere geometry */
Expand Down
14 changes: 2 additions & 12 deletions samples/s2-hitPrograms/s2-5-aabbs/deviceCode.slang
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ void AABBClosestHit(uniform AABBGeomData record, inout Payload payload, in BBoxA
}
}

#define AA 3

// This ray generation program will kick off the ray tracing process,
// generating rays and tracing them into the world.
[shader("raygeneration")]
Expand Down Expand Up @@ -151,19 +149,11 @@ void raygen(uniform RayGenData record) {

tot /= float(AA * AA);

// dither to remove banding in the background
tot += fract(sin(pixelID.x * float3(13, 1, 11) + pixelID.y * float3(1, 7, 5)) * 158.391832) / 255.0;

const int fbOfs = pixelID.x + iResolution.x * pixelID.y;
record.frameBuffer[fbOfs] = gprt::make_bgra(tot);
}

// A background with a vignette effect.
[shader("miss")]
void miss(inout Payload payload) {
float2 resolution = DispatchRaysDimensions().xy;
float2 fragCoord = DispatchRaysIndex().xy;
float2 p = (-resolution.xy + 2.0 * fragCoord) / resolution.y;
float3 col = float3(0.08) * (1.0 - 0.3 * length(p)) + 0.02 * WorldRayDirection().y;
payload.color = col;
}
payload.color = float3(0.0);
}
24 changes: 2 additions & 22 deletions samples/s2-hitPrograms/s2-5-aabbs/sharedCode.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
// MIT License

// Copyright (c) 2022 Nathan V. Morrical

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "gprt.h"

#define AA 3 // used for antialiasing

/* variables available to all programs */

/* variables for the triangle mesh geometry */
Expand Down

0 comments on commit 1bf7d5a

Please sign in to comment.