Some ray tracing changes
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import Common;
|
||||
import LightEnv;
|
||||
import MaterialParameter;
|
||||
import MATERIAL_FILE_NAME;
|
||||
|
||||
struct LightCullingData
|
||||
{
|
||||
@@ -26,7 +25,7 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
|
||||
{
|
||||
result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
for(uint i = 0; i < pLightEnv.numPointLights; ++i)
|
||||
for(uint i = 0; i < lightCount; ++i)
|
||||
{
|
||||
uint lightIndex = pLightCullingData.lightIndexList[startOffset + i];
|
||||
result += pLightEnv.pointLights[lightIndex].illuminate(lightingParams, brdf);
|
||||
|
||||
@@ -49,6 +49,23 @@ struct FragmentParameter
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
static FragmentParameter interpolate(FragmentParameter f0, FragmentParameter f1, FragmentParameter f2, float3 barycentricCoords)
|
||||
{
|
||||
FragmentParameter result;
|
||||
result.position_CS = f0.position_CS * barycentricCoords.x + f1.position_CS * barycentricCoords.y + f2.position_CS * barycentricCoords.z;
|
||||
#ifndef POS_ONLY
|
||||
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
|
||||
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
|
||||
result.biTangent_WS = f0.biTangent_WS * barycentricCoords.x + f1.biTangent_WS * barycentricCoords.y + f2.biTangent_WS * barycentricCoords.z;
|
||||
result.position_WS = f0.position_WS * barycentricCoords.x + f1.position_WS * barycentricCoords.y + f2.position_WS * barycentricCoords.z;
|
||||
result.vertexColor = f0.vertexColor * barycentricCoords.x + f1.vertexColor * barycentricCoords.y + f2.vertexColor * barycentricCoords.z;
|
||||
for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
||||
{
|
||||
result.texCoords[i] = f0.texCoords[i] * barycentricCoords.x + f1.texCoords[i] * barycentricCoords.y + f2.texCoords[i] * barycentricCoords.z;
|
||||
}
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
// data passed to visibility render
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import Common;
|
||||
import RayTracingData;
|
||||
import VertexData;
|
||||
import MaterialParameter;
|
||||
import Scene;
|
||||
import LightEnv;
|
||||
|
||||
// simplification: all BLAS only have 1 geometry
|
||||
|
||||
[shader("closesthit")]
|
||||
void main(inout payload Payload hitValue, in BuiltInTriangleIntersectionAttributes attr)
|
||||
{
|
||||
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
|
||||
|
||||
InstanceData inst = pScene.instances[InstanceID()];
|
||||
MeshData m = pScene.meshData[InstanceID()];
|
||||
|
||||
// offset into the index buffer
|
||||
uint indexOffset = m.firstIndex;
|
||||
// added to indices to reference correct part of global mesh pool
|
||||
uint vertexOffset = pScene.meshletInfos[m.meshletOffset].indicesOffset;
|
||||
|
||||
uint vertexIndex0 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 0];
|
||||
uint vertexIndex1 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 1];
|
||||
uint vertexIndex2 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 2];
|
||||
|
||||
VertexAttributes attr0 = pVertexData.getAttributes(vertexIndex0);
|
||||
VertexAttributes attr1 = pVertexData.getAttributes(vertexIndex1);
|
||||
VertexAttributes attr2 = pVertexData.getAttributes(vertexIndex2);
|
||||
|
||||
FragmentParameter f0 = attr0.getParameter(inst.transformMatrix);
|
||||
FragmentParameter f1 = attr1.getParameter(inst.transformMatrix);
|
||||
FragmentParameter f2 = attr2.getParameter(inst.transformMatrix);
|
||||
|
||||
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
|
||||
|
||||
LightingParameter lightingParams = params.getLightingParameter();
|
||||
MaterialParameter materialParams = params.getMaterialParameter();
|
||||
let brdf = Material.prepare(materialParams);
|
||||
|
||||
float3 result = float3(0, 0, 0);
|
||||
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
|
||||
{
|
||||
result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
for(uint i = 0; i < pLightEnv.numPointLights; ++i)
|
||||
{
|
||||
result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
result += brdf.evaluateAmbient();
|
||||
// gamma correction
|
||||
result = result / (result + float3(1.0));
|
||||
result = pow(result, float3(1.0/2.2));
|
||||
|
||||
hitValue.color = result;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import Common;
|
||||
import RayTracingData;
|
||||
|
||||
[shader("raygeneration")]
|
||||
void main()
|
||||
{
|
||||
const float2 pixelCenter = float2(DispatchRaysIndex().xy) + float2(0.5);
|
||||
const float2 inUV = pixelCenter / float2(DispatchRaysDimensions().xy);
|
||||
float2 d = inUV * 2.0 - 1.0;
|
||||
|
||||
float4 origin = mul(pViewParams.inverseViewMatrix, float4(0, 0, 0, 1));
|
||||
float4 target = mul(pViewParams.inverseProjection, float4(d.x, d.y, 1, 1));
|
||||
float4 direction = mul(pViewParams.inverseViewMatrix, float4(normalize(target.xyz), 0));
|
||||
|
||||
float tmin = 0.0001;
|
||||
float tmax = 10000.0;
|
||||
|
||||
uint max_rays = 24;
|
||||
|
||||
uint object_type = 100;
|
||||
float4 color = float4(0, 0, 0, 0);
|
||||
uint current_mode = 0;
|
||||
float expectedDistance = -1;
|
||||
Payload payload;
|
||||
|
||||
RayDesc desc = {
|
||||
origin.xyz,
|
||||
tmin,
|
||||
direction.xyz,
|
||||
tmax,
|
||||
};
|
||||
|
||||
TraceRay(pRayTracingParams.scene, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, desc, payload);
|
||||
|
||||
pRayTracingParams.image[DispatchRaysIndex().xy] = payload.color;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
struct Payload
|
||||
{
|
||||
float3 color;
|
||||
};
|
||||
|
||||
struct RayTracingParams
|
||||
{
|
||||
RaytracingAccelerationStructure scene;
|
||||
RWTexture2D<float3> image;
|
||||
StructuredBuffer<uint> indexBuffer;
|
||||
};
|
||||
ParameterBlock<RayTracingParams> pRayTracingParams;
|
||||
Reference in New Issue
Block a user