| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include "Common.hlsl"
- // Raytracing output texture, accessed as a UAV
- RWTexture2D<float4> gOutput : register(u0);
- RWTexture2D<float4> guiTexture : register(u1);
- // Raytracing acceleration structure, accessed as a SRV
- RaytracingAccelerationStructure TLAS : register(t0);
- cbuffer RayGenerationSettings : register(b0)
- {
- int renderGui;
- int useRays;
- float minDistance;
- float maxDistance;
- float4x4 inverseView;
- float4x4 inverseProjection;
- }
- [shader("raygeneration")]
- void RayGen()
- {
- // Initialize the ray payload
- HitInfo rayPayload;
- rayPayload.hitCount = 0;
- 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);
- float dayLightFactor = 1.f; // TODO: set this based on time
- float3 minLight = float3(0.1f, 0.1f, 0.1f);
- float3 dayLight = unpackLight(rayPayload.dayLight[rayPayload.hitCount - 1]);
- float3 dynamicLight = unpackLight(rayPayload.dynamicLight[rayPayload.hitCount - 1]);
- float3 light = max(minLight, max(dynamicLight, dayLight * dayLightFactor));
- color = rayPayload.color[rayPayload.hitCount - 1].rgb * light;
- for (int i = rayPayload.hitCount - 2; i >= 0; i--)
- {
- dayLight = unpackLight(rayPayload.dayLight[i]);
- dynamicLight = unpackLight(rayPayload.dynamicLight[i]);
- light = max(minLight, max(dynamicLight, dayLight * dayLightFactor));
- color = rayPayload.color[i].rgb * light * rayPayload.color[i].a + color * (1 - rayPayload.color[i].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);
- }
|