-
Notifications
You must be signed in to change notification settings - Fork 13
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
Raytracing pipeline demo #173
base: master
Are you sure you want to change the base?
Changes from 1 commit
881e40b
444c917
c991e20
6ac8f88
9091da1
d303356
f261f7c
7b5059c
8cbb0dc
a9d5f8b
feedf65
e369368
c2c82d4
34a3fa9
ec88268
d21c22c
bc093c7
1dc682a
ab58218
6966942
ef02db2
c810949
13251ca
c48b5b9
73b3f99
adb7bb6
bc16d1b
dc7db46
86f4f4b
96f25ca
583c0f9
0797b33
7409e4b
f9c3fad
0da41df
890c992
ee10d47
19ad8b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: kevyuu <kevin.kayu@gmail.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ struct VertexData | |
float32_t3 normal; | ||
}; | ||
|
||
VertexData fetchVertexData(int instID, int primID, SGeomInfo geom, float2 bary) | ||
VertexData fetchVertexData(int instID, int primID, STriangleGeomInfo geom, float2 bary) | ||
{ | ||
uint idxOffset = primID * 3; | ||
|
||
|
@@ -116,7 +116,7 @@ void main(inout ColorPayload p, in BuiltInTriangleIntersectionAttributes attribs | |
{ | ||
const int instID = InstanceID(); | ||
const int primID = PrimitiveIndex(); | ||
const SGeomInfo geom = vk::RawBufferLoad < SGeomInfo > (pc.geometryInfoBuffer + instID * sizeof(SGeomInfo)); | ||
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo)); | ||
const VertexData vertexData = fetchVertexData(instID, primID, geom, attribs.barycentrics); | ||
const float32_t3 worldPosition = mul(ObjectToWorld3x4(), float32_t4(vertexData.position, 1)); | ||
const float32_t3 worldNormal = mul(vertexData.normal, WorldToObject3x4()).xyz; | ||
|
@@ -132,16 +132,16 @@ void main(inout ColorPayload p, in BuiltInTriangleIntersectionAttributes attribs | |
if (dot(worldNormal, cLight.outLightDir) > 0) | ||
{ | ||
RayDesc rayDesc; | ||
rayDesc.Origin = WorldRayOrigin() + WorldRayDirection() * RayTCurrent() + worldNormal * 0.02f; | ||
rayDesc.Origin = WorldRayOrigin() + WorldRayDirection() * RayTCurrent(); | ||
rayDesc.Direction = cLight.outLightDir; | ||
rayDesc.TMin = 0.001; | ||
rayDesc.TMin = 0.01; | ||
rayDesc.TMax = cLight.outLightDistance; | ||
|
||
uint flags = RAY_FLAG_SKIP_CLOSEST_HIT_SHADER; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the reason for not having |
||
ShadowPayload shadowPayload; | ||
shadowPayload.isShadowed = true; | ||
shadowPayload.seed = p.seed; | ||
TraceRay(topLevelAS, flags, 0xFF, 1, 0, 1, rayDesc, shadowPayload); | ||
TraceRay(topLevelAS, flags, 0xFF, ERT_OCCLUSION, 0, EMT_OCCLUSION, rayDesc, shadowPayload); | ||
|
||
bool isShadowed = shadowPayload.isShadowed; | ||
if (isShadowed) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "common.hlsl" | ||
|
||
[[vk::push_constant]] SPushConstants pc; | ||
|
||
struct Ray | ||
{ | ||
float32_t3 origin; | ||
float32_t3 direction; | ||
}; | ||
|
||
struct Attrib | ||
{ | ||
float3 HitAttribute; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you never fill this out? |
||
|
||
// Ray-Sphere intersection | ||
// http://viclw17.github.io/2018/07/16/raytracing-ray-sphere-intersection/ | ||
float32_t hitSphere(SProceduralGeomInfo s, Ray r) | ||
{ | ||
float32_t3 oc = r.origin - s.center; | ||
float32_t a = dot(r.direction, r.direction); | ||
float32_t b = 2.0 * dot(oc, r.direction); | ||
float32_t c = dot(oc, oc) - s.radius * s.radius; | ||
float32_t discriminant = b * b - 4 * a * c; | ||
|
||
if (discriminant < 0) | ||
{ | ||
return -1.0; | ||
} | ||
else | ||
{ | ||
return (-b - sqrt(discriminant)) / (2.0 * a); | ||
} | ||
} | ||
|
||
[shader("intersection")] | ||
void main() | ||
{ | ||
Ray ray; | ||
ray.origin = WorldRayOrigin(); | ||
ray.direction = WorldRayDirection(); | ||
|
||
const int primID = PrimitiveIndex(); | ||
|
||
// Sphere data | ||
SProceduralGeomInfo sphere = vk::RawBufferLoad < SProceduralGeomInfo > (pc.proceduralGeomInfoBuffer + primID * sizeof(SProceduralGeomInfo)); | ||
|
||
float32_t tHit = hitSphere(sphere, ray); | ||
|
||
Attrib attrib; | ||
// Report hit point | ||
if (tHit > 0) | ||
ReportHit(tHit, 0, attrib); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "common.hlsl" | ||
|
||
[[vk::push_constant]] SPushConstants pc; | ||
|
||
[[vk::binding(0, 0)]] RaytracingAccelerationStructure topLevelAS; | ||
|
||
[shader("closesthit")] | ||
void main(inout ColorPayload p, in BuiltInTriangleIntersectionAttributes attribs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
const int instID = InstanceID(); | ||
const int primID = PrimitiveIndex(); | ||
float32_t3 worldPosition = WorldRayOrigin() + WorldRayDirection() * RayTCurrent(); | ||
|
||
SProceduralGeomInfo sphere = vk::RawBufferLoad < SProceduralGeomInfo > (pc.proceduralGeomInfoBuffer + primID * sizeof(SProceduralGeomInfo)); | ||
|
||
// Computing the normal at hit position | ||
float32_t3 worldNormal = normalize(worldPosition - sphere.center); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you already had to load and compute some of this in the intersection shader, is it not possible to pass it in the payload/custom attribs to the closest hit shader? |
||
|
||
RayLight cLight; | ||
cLight.inHitPosition = worldPosition; | ||
CallShader(pc.light.type, cLight); | ||
|
||
// Material of the object | ||
Material mat = sphere.material; | ||
|
||
// Diffuse | ||
float3 diffuse = computeDiffuse(sphere.material, cLight.outLightDir, worldNormal); | ||
float3 specular = float3(0, 0, 0); | ||
float attenuation = 1; | ||
|
||
// Tracing shadow ray only if the light is visible from the surface | ||
if (dot(worldNormal, cLight.outLightDir) > 0) | ||
{ | ||
RayDesc rayDesc; | ||
rayDesc.Origin = WorldRayOrigin() + WorldRayDirection() * RayTCurrent(); | ||
rayDesc.Direction = cLight.outLightDir; | ||
rayDesc.TMin = 0.01; | ||
rayDesc.TMax = cLight.outLightDistance; | ||
|
||
uint flags = | ||
RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_FORCE_OPAQUE | | ||
RAY_FLAG_SKIP_CLOSEST_HIT_SHADER; | ||
|
||
ShadowPayload shadowPayload; | ||
shadowPayload.isShadowed = true; | ||
shadowPayload.seed = p.seed; | ||
TraceRay(topLevelAS, flags, 0xFF, ERT_OCCLUSION, 0, EMT_PRIMARY, rayDesc, shadowPayload); | ||
|
||
bool isShadowed = shadowPayload.isShadowed; | ||
if (isShadowed) | ||
{ | ||
attenuation = 0.3; | ||
} | ||
else | ||
{ | ||
specular = computeSpecular(sphere.material, WorldRayDirection(), cLight.outLightDir, worldNormal); | ||
} | ||
} | ||
|
||
p.hitValue = (cLight.outIntensity * attenuation * (diffuse + specular)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get in the habit of placing large members first to avoid padding