| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include "Common.hlsl"
- [shader("raygeneration")]
- void RayGen()
- {
- // Initialize the ray payload
- RayHits rayPayload;
- rayPayload.hitCount = 0;
- SingleHit hitInitial;
- hitInitial.color = float4(0, 0, 0, 0);
- hitInitial.normal = float3(0, 0, 0);
- hitInitial.distance = 0.f;
- hitInitial.dayLight = 0;
- hitInitial.dynamicLight = 0;
- for (int index = 0; index < MAX_TRANSPACENT_HITS; index++)
- {
- rayPayload.hits[index] = hitInitial;
- }
- float3 color = float3(0, 0, 0);
- // Get the location within the dispatched 2D grid of work items
- // (often maps to pixels, so this could represent a pixel coordinate).
- float2 dispatchDimensions = float2(DispatchRaysDimensions().xy);
- uint2 dispatchIndex = DispatchRaysIndex().xy;
- float2 dispatchPercentage = (dispatchIndex + 0.5) / dispatchDimensions;
-
- if (useRays)
- {
- float2 d = (dispatchPercentage * 2.f - 1.f);
- RayDesc ray;
- ray.Origin = mul(inverseView, float4(0, 0, 0, 1)).xyz;
- float4 target = mul(inverseProjection, float4(d.x, -d.y, -1, 1));
- target.w = 1;
- ray.Direction = mul(inverseView, target).xyz - ray.Origin;
- ray.TMin = minDistance;
- ray.TMax = maxDistance;
- TraceRay(TLAS, /*RayFlags*/0, /*InstanceInclusionMask*/0xFF, /*RayContributionToHitGroupIndex*/0,
- /*MultiplierForGeometryContributionToHitGroupIndex*/0, /*MissShaderIndex*/0, ray, rayPayload);
- if (rayPayload.hitCount > 0)
- {
- float3 minLight = float3(0.1f, 0.1f, 0.1f);
- SingleHit lastHit = rayPayload.hits[rayPayload.hitCount - 1];
- float3 dayLight = unpackLight(lastHit.dayLight);
- float3 dynamicLight = unpackLight(lastHit.dynamicLight);
- float3 light = max(minLight, max(dynamicLight, dayLight));
- color = lastHit.color.rgb * light;
- for (int i = rayPayload.hitCount - 2; i >= 0; i--)
- {
- SingleHit currentHit = rayPayload.hits[i];
- dayLight = unpackLight(currentHit.dayLight);
- dynamicLight = unpackLight(currentHit.dynamicLight);
- light = max(minLight, max(dynamicLight, dayLight));
- color = currentHit.color.rgb * light * currentHit.color.a + color * (1 - currentHit.color.a);
- }
- }
- }
-
- uint outWidth, outHeight;
- gOutput.GetDimensions(outWidth, outHeight);
- uint2 outputIndex = uint2(dispatchPercentage * float2(outWidth, outHeight));
- if (renderGui)
- {
- uint guiWidth, guiHeight;
- guiTexture.GetDimensions(guiWidth, guiHeight);
- uint2 guiIndex = uint2(dispatchPercentage * float2(guiWidth, guiHeight));
- float4 guiColor = guiTexture[guiIndex];
- color = color * (1 - guiColor.a) + guiColor.rgb * guiColor.a;
- }
- gOutput[outputIndex] = float4(color, 1.f);
- }
|