Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sample 10 #51

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ add_subdirectory(s04-computeAABBs)
add_subdirectory(s05-computeVertex)
add_subdirectory(s06-computeTransform)
add_subdirectory(s07-multipleGeometry)
add_subdirectory(s08-multipleBLAS)
add_subdirectory(s09-multipleTLAS)
add_subdirectory(s10-doublePrecision)
36 changes: 36 additions & 0 deletions samples/s08-multipleBLAS/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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.

embed_devicecode(
OUTPUT_TARGET
s08_deviceCode
SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/deviceCode.hlsl
)

add_executable(s08_multipleBLAS hostCode.cpp)
target_link_libraries(s08_multipleBLAS
PRIVATE
s08_deviceCode
gprt::gprt
generator
)
132 changes: 132 additions & 0 deletions samples/s08-multipleBLAS/deviceCode.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// 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 "sharedCode.h"

struct Payload
{
float3 color;
};

GPRT_RAYGEN_PROGRAM(raygen, (RayGenData, record))
{
Payload payload;
uint2 pixelID = DispatchRaysIndex().xy;
float2 screen = (float2(pixelID) +
float2(.5f, .5f)) / float2(record.fbSize);

// Generate ray
RayDesc rayDesc;
rayDesc.Origin = record.camera.pos;
rayDesc.Direction =
normalize(record.camera.dir_00
+ screen.x * record.camera.dir_du
+ screen.y * record.camera.dir_dv
);
rayDesc.TMin = 0.0;
rayDesc.TMax = 1e20f;

// Trace ray against surface
RaytracingAccelerationStructure world = gprt::getAccelHandle(record.world);
TraceRay(
world, // the tree
RAY_FLAG_FORCE_OPAQUE, // ray flags
0xff, // instance inclusion mask
0, // ray type
1, // number of ray types
0, // miss type
rayDesc, // the ray to trace
payload // the payload IO
);

const int fbOfs = pixelID.x + record.fbSize.x * pixelID.y;
gprt::store(record.fbPtr, fbOfs, gprt::make_bgra(payload.color));
}

struct TriangleAttributes {
float2 bc;
};

GPRT_CLOSEST_HIT_PROGRAM(trianglesClosestHit,
(TrianglesGeomData, record), (Payload, payload), (TriangleAttributes, attributes))
{
// compute normal:
uint primID = PrimitiveIndex();
int3 index = gprt::load<int3>(record.index, primID);
float3 A = gprt::load<float3>(record.vertex, index.x);
float3 B = gprt::load<float3>(record.vertex, index.y);
float3 C = gprt::load<float3>(record.vertex, index.z);
float3 Ng = normalize(cross(B-A,C-A));
float3 rayDir = normalize(ObjectRayDirection());
payload.color = (.2f + .8f * abs(dot(rayDir,Ng))) * record.color;
}

struct Attribute
{
float radius;
float3 position;
};

GPRT_CLOSEST_HIT_PROGRAM(spheresClosestHit,
(SpheresGeomData, record), (Payload, payload), (Attribute, attribute))
{
float3 origin = attribute.position;
float3 hitPos = ObjectRayOrigin() + RayTCurrent() * ObjectRayDirection();

float3 Ng = normalize(hitPos - origin);
float3 rayDir = normalize(ObjectRayDirection());
payload.color = (.2f + .8f * abs(dot(rayDir,Ng))) * record.color;
}

GPRT_INTERSECTION_PROGRAM(spheresIntersection,
(SpheresGeomData, record))
{
uint primID = PrimitiveIndex();

float3 position = gprt::load<float3>(record.vertex, primID);
float radius = gprt::load<float>(record.radius, primID);

float3 ro = ObjectRayOrigin();
float3 rd = ObjectRayDirection();

float3 oc = ro - position;
float b = dot( oc, rd );
float c = dot( oc, oc ) - radius * radius;
float h = b*b - c;

if( h<0.0 ) return; // -1.0;
float tHit = -b - sqrt( h );

Attribute attr;
attr.radius = radius;
attr.position = position;
ReportHit(tHit, /*hitKind*/ 0, attr);
}

GPRT_MISS_PROGRAM(miss,
(MissProgData, record), (Payload, payload))
{
uint2 pixelID = DispatchRaysIndex().xy;
int pattern = (pixelID.x / 8) ^ (pixelID.y/8);
float3 color = (pattern & 1) ? record.color1 : record.color0;
payload.color = color;
}
Loading