Skip to content

Commit

Permalink
Satisfied warnings & errors for new hlsl compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Gerdisch committed Jul 19, 2018
1 parent 265eb0e commit 038da91
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct RWByteAddressBufferPointer
uint offsetInBytes;
};

static
RWByteAddressBufferPointer CreateRWByteAddressBufferPointer(in RWByteAddressBuffer buffer, uint offsetInBytes)
{
RWByteAddressBufferPointer pointer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#pragma once
#include "EmulatedPointer.hlsli"

static
RWByteAddressBuffer PointerGetBuffer(GpuVA address)
{
return DescriptorHeapBufferTable[NonUniformResourceIndex(address[EmulatedPointerDescriptorHeapIndex])];
Expand All @@ -26,6 +27,7 @@ uint4 Load4(GpuVA address)
return PointerGetBuffer(address).Load4(PointerGetBufferStartOffset(address));
}

static
RWByteAddressBufferPointer CreateRWByteAddressBufferPointerFromGpuVA(GpuVA address)
{
return CreateRWByteAddressBufferPointer(PointerGetBuffer(address), PointerGetBufferStartOffset(address));
Expand Down
14 changes: 13 additions & 1 deletion Libraries/D3D12RaytracingFallback/src/RayTracingHelper.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct BoundingBox
#define GetOffsetToInstanceDesc(pointer) \
GetOffsetToOffset(pointer, OffsetToLeafNodeMetaDataOffset)

static
int GetOffsetToBoxes(RWByteAddressBufferPointer pointer)
{
// Optimization, Boxes are always at a fixed location
Expand All @@ -74,11 +75,13 @@ int GetOffsetToBoxes(RWByteAddressBufferPointer pointer)
return pointer.offsetInBytes + SizeOfBVHOffsets;
}

static
int GetOffsetToVertices(RWByteAddressBufferPointer pointer)
{
return GetOffsetToOffset(pointer, OffsetToPrimitivesOffset);
}

static
int GetOffsetToPrimitiveMetaData(RWByteAddressBufferPointer pointer)
{
return GetOffsetToOffset(pointer, OffsetToPrimitiveMetaDataOffset);
Expand Down Expand Up @@ -117,6 +120,7 @@ uint GetBoxAddress(uint startAddress, uint boxIndex)
return startAddress + boxIndex * SizeOfAABBNode;
}

static
BoundingBox RawDataToBoundingBox(int4 a, int4 b, out uint2 flags)
{
BoundingBox box;
Expand All @@ -132,6 +136,7 @@ BoundingBox RawDataToBoundingBox(int4 a, int4 b, out uint2 flags)
return box;
}

static
BoundingBox GetBoxFromBuffer(RWByteAddressBuffer buffer, uint boxStartOffset, uint boxIndex)
{
uint boxAddress = GetBoxAddress(boxStartOffset, boxIndex);
Expand All @@ -149,6 +154,7 @@ BoundingBox GetBoxFromBuffer(RWByteAddressBuffer buffer, uint boxStartOffset, ui
#define GetBVHMetadataFromLeafIndex(byteAddressBufferPointer, offsetToLeafNodeMetaData, leafIndex) \
LoadBVHMetadata(byteAddressBufferPointer.buffer, GetBVHMetadataAddress(byteAddressBufferPointer, offsetToLeafNodeMetaData, leafIndex))

static
BoundingBox BVHReadBoundingBox(RWByteAddressBufferPointer pointer, int nodeIndex, out uint2 flags)
{
const uint boxAddress = GetBoxAddress(GetOffsetToBoxes(pointer), nodeIndex);
Expand Down Expand Up @@ -176,6 +182,7 @@ uint GetPrimitiveMetaDataAddress(uint startAddress, uint triangleIndex)
return startAddress + triangleIndex * SizeOfPrimitiveMetaData;
}

static
PrimitiveMetaData BVHReadPrimitiveMetaData(RWByteAddressBufferPointer pointer, int primitiveIndex)
{
const uint readAddress = GetPrimitiveMetaDataAddress(GetOffsetToPrimitiveMetaData(pointer), primitiveIndex);
Expand All @@ -188,6 +195,7 @@ PrimitiveMetaData BVHReadPrimitiveMetaData(RWByteAddressBufferPointer pointer, i
return metadata;
}

static
void WriteOnlyFlagToBuffer(RWByteAddressBuffer buffer, uint boxStartOffset, uint boxIndex, uint2 flags)
{
uint boxAddress = GetBoxAddress(boxStartOffset, boxIndex);
Expand All @@ -196,6 +204,7 @@ void WriteOnlyFlagToBuffer(RWByteAddressBuffer buffer, uint boxStartOffset, uint
buffer.Store(boxAddress + 4 * 7, flags.y);
}

static
void WriteBoxToBuffer(RWByteAddressBuffer buffer, uint boxStartOffset, uint boxIndex, BoundingBox box, uint2 flags)
{
uint boxAddress = GetBoxAddress(boxStartOffset, boxIndex);
Expand All @@ -207,6 +216,7 @@ void WriteBoxToBuffer(RWByteAddressBuffer buffer, uint boxStartOffset, uint boxI
buffer.Store4(boxAddress + 16, data2);
}

static
void BVHReadTriangle(
RWByteAddressBufferPointer pointer,
out float3 v0,
Expand Down Expand Up @@ -345,15 +355,17 @@ AABB TransformAABB(AABB box, AffineMatrix transform)

static const uint OffsetToAnyHitStateId = 4;
static const uint OffsetToIntersectionStateId = 8;
static
uint GetAnyHitStateId(ByteAddressBuffer shaderTable, uint recordOffset)
{
return shaderTable.Load(recordOffset + OffsetToAnyHitStateId);
}

static
void GetAnyHitAndIntersectionStateId(ByteAddressBuffer shaderTable, uint recordOffset, out uint AnyHitStateId, out uint IntersectionStateId)
{
uint2 stateIds = shaderTable.Load2(recordOffset + OffsetToAnyHitStateId);
AnyHitStateId = stateIds.x;
IntersectionStateId = stateIds.y;
}
#endif // RAYTACING_HELPER_H_INCLUDED
#endif // RAYTACING_HELPER_H_INCLUDED
4 changes: 4 additions & 0 deletions Libraries/D3D12RaytracingFallback/src/RayTracingHlslCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ struct BVHMetadata

#ifdef HLSL
#define Store4StrideInBytes 16
static
void StoreBVHMetadataToRawData(RWByteAddressBuffer buffer, uint offset, BVHMetadata metadata)
{
uint4 data[7];
Expand Down Expand Up @@ -275,6 +276,7 @@ RaytracingInstanceDesc RawDataToRaytracingInstanceDesc(uint4 a, uint4 b, uint4 c
return desc;
}

static
BVHMetadata LoadBVHMetadata(RWByteAddressBuffer buffer, uint offset)
{
uint4 data[7];
Expand All @@ -294,6 +296,7 @@ BVHMetadata LoadBVHMetadata(RWByteAddressBuffer buffer, uint offset)
return metadata;
}

static
RaytracingInstanceDesc LoadRaytracingInstanceDesc(RWByteAddressBuffer buffer, uint offset)
{
uint4 data[4];
Expand All @@ -305,6 +308,7 @@ RaytracingInstanceDesc LoadRaytracingInstanceDesc(RWByteAddressBuffer buffer, ui
return RawDataToRaytracingInstanceDesc(data[0], data[1], data[2], data[3]);
}

static
RaytracingInstanceDesc LoadRaytracingInstanceDesc(ByteAddressBuffer buffer, uint offset)
{
uint4 data[4];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ void RayTriangleIntersect(
}

#define MULTIPLE_LEAVES_PER_NODE 0
static
bool TestLeafNodeIntersections(
RWByteAddressBufferPointer accelStruct,
uint2 flags,
Expand Down
18 changes: 16 additions & 2 deletions Libraries/D3D12RaytracingFallback/src/dxc/dxcdxrfallbackcompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ struct DxcShaderInfo

struct DxcShaderBytecode
{
LPBYTE pShaderBytecode;
UINT32 shaderBytecodeSize;
LPBYTE pData;
UINT32 Size;
};

struct DxcExportDesc
{
LPCWSTR ExportToRename;
LPCWSTR ExportName;
};

struct __declspec(uuid("76bb3c85-006d-4b72-9e10-63cd97df57f0"))
Expand All @@ -48,6 +54,14 @@ struct __declspec(uuid("76bb3c85-006d-4b72-9e10-63cd97df57f0"))

virtual HRESULT STDMETHODCALLTYPE SetDebugOutput(int val) = 0;

virtual HRESULT STDMETHODCALLTYPE RenameAndLink(
_In_count_(libCount) DxcShaderBytecode *pLibs,
UINT32 libCount,
_In_count_(ExportCount) DxcExportDesc *pExports,
UINT32 ExportCount,
_COM_Outptr_ IDxcOperationResult **ppResult
) = 0;

virtual HRESULT STDMETHODCALLTYPE PatchShaderBindingTables(
_In_ const LPCWSTR pEntryName,
_In_ DxcShaderBytecode *pShaderBytecode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool IsInsideViewport(float2 p, Viewport viewport)
[shader("raygeneration")]
void MyRaygenShader()
{
float2 lerpValues = (float2)DispatchRaysIndex() / DispatchRaysDimensions();
float2 lerpValues = (float2)DispatchRaysIndex() / (float2)DispatchRaysDimensions();

// Orthographic projection since we're raytracing in screen space.
float3 rayDir = float3(0, 0, 1);
Expand All @@ -57,12 +57,12 @@ void MyRaygenShader()
TraceRay(Scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, ~0, 0, 1, 0, ray, payload);

// Write the raytraced color to the output texture.
RenderTarget[DispatchRaysIndex()] = payload.color;
RenderTarget[DispatchRaysIndex().xy] = payload.color;
}
else
{
// Render interpolated DispatchRaysIndex outside the stencil window
RenderTarget[DispatchRaysIndex()] = float4(lerpValues, 0, 1);
RenderTarget[DispatchRaysIndex().xy] = float4(lerpValues, 0, 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void Hit(inout RayPayload payload, in BuiltInTriangleIntersectionAttributes attr

float3 worldPosition = WorldRayOrigin() + WorldRayDirection() * RayTCurrent();

uint2 threadID = DispatchRaysIndex();
uint2 threadID = DispatchRaysIndex().xy;
float3 ddxOrigin, ddxDir, ddyOrigin, ddyDir;
GenerateCameraRay(uint2(threadID.x + 1, threadID.y), ddxOrigin, ddxDir);
GenerateCameraRay(uint2(threadID.x, threadID.y + 1), ddyOrigin, ddyDir);
Expand Down Expand Up @@ -288,7 +288,7 @@ void Hit(inout RayPayload payload, in BuiltInTriangleIntersectionAttributes attr
normal = normalize(mul(normal, tbn));
}

float3 outputColor = AmbientColor * diffuseColor * texSSAO[DispatchRaysIndex()];
float3 outputColor = AmbientColor * diffuseColor * texSSAO[DispatchRaysIndex().xy];

float shadow = 1.0;
if (UseShadowRays)
Expand Down Expand Up @@ -328,8 +328,8 @@ void Hit(inout RayPayload payload, in BuiltInTriangleIntersectionAttributes attr
// TODO: Should be passed in via material info
if (IsReflection)
{
outputColor = outputColor * 0.3 + g_screenOutput[DispatchRaysIndex()];
outputColor = outputColor * 0.3 + g_screenOutput[DispatchRaysIndex().xy].xyz;
}

g_screenOutput[DispatchRaysIndex()] = float4(outputColor, 1);
g_screenOutput[DispatchRaysIndex().xy] = float4(outputColor, 1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
void RayGen()
{
float3 origin, direction;
GenerateCameraRay(DispatchRaysIndex(), origin, direction);
GenerateCameraRay(DispatchRaysIndex().xy, origin, direction);

RayDesc rayDesc = { origin,
0.0f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Texture2D<float3> normals : register(t13);
[shader("raygeneration")]
void RayGen()
{
uint2 DTid = DispatchRaysIndex();
uint2 DTid = DispatchRaysIndex().xy;
float2 xy = DTid.xy + 0.5;

// Screen position for the ray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Texture2D<float> depth : register(t1);
[shader("raygeneration")]
void RayGen()
{
uint2 DTid = DispatchRaysIndex();
uint2 DTid = DispatchRaysIndex().xy;
float2 xy = DTid.xy + 0.5;

// Screen position for the ray
Expand Down Expand Up @@ -48,11 +48,11 @@ void RayGen()

if (payload.RayHitT < FLT_MAX)
{
g_screenOutput[DispatchRaysIndex()] = float4(0, 0, 0, 1);
g_screenOutput[DispatchRaysIndex().xy] = float4(0, 0, 0, 1);
}
else
{
g_screenOutput[DispatchRaysIndex()] = float4(1, 1, 1, 1);
g_screenOutput[DispatchRaysIndex().xy] = float4(1, 1, 1, 1);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,19 @@ void main(
((tileLightCountConeShadowed & 0xff) << 16);
lightGrid.Store(tileOffset + 0, lightCount);

uint n;
uint storeOffset = tileOffset + 4;
for (uint n = 0; n < tileLightCountSphere; n++)
for (n = 0; n < tileLightCountSphere; n++)
{
lightGrid.Store(storeOffset, tileLightIndicesSphere[n]);
storeOffset += 4;
}
for (uint n = 0; n < tileLightCountCone; n++)
for (n = 0; n < tileLightCountCone; n++)
{
lightGrid.Store(storeOffset, tileLightIndicesCone[n]);
storeOffset += 4;
}
for (uint n = 0; n < tileLightCountConeShadowed; n++)
for (n = 0; n < tileLightCountConeShadowed; n++)
{
lightGrid.Store(storeOffset, tileLightIndicesConeShadowed[n]);
storeOffset += 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void Hit(inout RayPayload payload, in BuiltInTriangleIntersectionAttributes attr
payload.RayHitT = RayTCurrent();
if (!payload.SkipShading)
{
g_screenOutput[DispatchRaysIndex()] = float4(attr.barycentrics, 1, 1);
g_screenOutput[DispatchRaysIndex().xy] = float4(attr.barycentrics, 1, 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void Miss(inout RayPayload payload)
{
if (!payload.SkipShading)
{
g_screenOutput[DispatchRaysIndex()] = float4(0, 0, 0, 1);
g_screenOutput[DispatchRaysIndex().xy] = float4(0, 0, 0, 1);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,39 @@ float4 CalculatePhongLighting(in float4 albedo, in float3 normal, in bool isInSh
return ambientColor + diffuseColor + specularColor;
}

// Load three 16 bit indices.
static
uint3 Load3x16BitIndices(uint offsetBytes)
{
uint3 indices;

// ByteAdressBuffer loads must be aligned at a 4 byte boundary.
// Since we need to read three 16 bit indices: { 0, 1, 2 }
// aligned at a 4 byte boundary as: { 0 1 } { 2 0 } { 1 2 } { 0 1 } ...
// we will load 8 bytes (~ 4 indices { a b | c d }) to handle two possible index triplet layouts,
// based on first index's offsetBytes being aligned at the 4 byte boundary or not:
// Aligned: { 0 1 | 2 - }
// Not aligned: { - 0 | 1 2 }
const uint dwordAlignedOffset = offsetBytes & ~3;
const uint2 four16BitIndices = g_indices.Load2(dwordAlignedOffset);

// Aligned: { 0 1 | 2 - } => retrieve first three 16bit indices
if (dwordAlignedOffset == offsetBytes)
{
indices.x = four16BitIndices.x & 0xffff;
indices.y = (four16BitIndices.x >> 16) & 0xffff;
indices.z = four16BitIndices.y & 0xffff;
}
else // Not aligned: { - 0 | 1 2 } => retrieve last three 16bit indices
{
indices.x = (four16BitIndices.x >> 16) & 0xffff;
indices.y = four16BitIndices.y & 0xffff;
indices.z = (four16BitIndices.y >> 16) & 0xffff;
}

return indices;
}

//***************************************************************************
//*****------ TraceRay wrappers for radiance and shadow rays. -------********
//***************************************************************************
Expand Down Expand Up @@ -165,14 +198,14 @@ bool TraceShadowRayAndReportIfHit(in Ray ray, in UINT currentRayRecursionDepth)
void MyRaygenShader()
{
// Generate a ray for a camera pixel corresponding to an index from the dispatched 2D grid.
Ray ray = GenerateCameraRay(DispatchRaysIndex(), g_sceneCB.cameraPosition.xyz, g_sceneCB.projectionToWorld);
Ray ray = GenerateCameraRay(DispatchRaysIndex().xy, g_sceneCB.cameraPosition.xyz, g_sceneCB.projectionToWorld);

// Cast a ray into the scene and retrieve a shaded color.
UINT currentRecursionDepth = 0;
float4 color = TraceRadianceRay(ray, currentRecursionDepth);

// Write the raytraced color to the output texture.
g_renderTarget[DispatchRaysIndex()] = color;
g_renderTarget[DispatchRaysIndex().xy] = color;
}

//***************************************************************************
Expand All @@ -189,7 +222,7 @@ void MyClosestHitShader_Triangle(inout RayPayload rayPayload, in BuiltInTriangle
uint baseIndex = PrimitiveIndex() * triangleIndexStride;

// Load up three 16 bit indices for the triangle.
const uint3 indices = Load3x16BitIndices(baseIndex, g_indices);
const uint3 indices = Load3x16BitIndices(baseIndex);

// Retrieve corresponding vertex normals for the triangle vertices.
float3 triangleNormal = g_vertices[indices[0]].normal;
Expand Down
Loading

0 comments on commit 038da91

Please sign in to comment.