Skip to content

Commit

Permalink
New GPU material instance format
Browse files Browse the repository at this point in the history
Material instances now use a separate GPU buffer.
GPU instance data references Material instances by index.
  • Loading branch information
Benualdo committed Nov 16, 2024
1 parent 4e2ce7c commit afae236
Show file tree
Hide file tree
Showing 29 changed files with 382 additions and 167 deletions.
4 changes: 2 additions & 2 deletions data/Shaders/debugdraw/debugdraw.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ VS_Output VS_DebugDraw(uint _vertexID : VertexID)
{
VS_Output output = (VS_Output) 0;

ByteAddressBuffer buf = getBuffer(debugDrawRootConstants3D.getBufferHandle());
ByteAddressBuffer buf = getBuffer(debugDrawRootConstants3D.getVertexBufferHandle());

Vertex vert;
vert.Load(buf, debugDrawRootConstants3D.getVertexFormat(), _vertexID, debugDrawRootConstants3D.getBufferOffset());
vert.Load(buf, debugDrawRootConstants3D.getVertexFormat(), _vertexID, debugDrawRootConstants3D.getVertexBufferOffset());

ViewConstants viewConstants;
viewConstants.Load(getBuffer(RESERVEDSLOT_BUFSRV_VIEWCONSTANTS));
Expand Down
6 changes: 3 additions & 3 deletions data/Shaders/debugdraw/debugdraw.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ struct DebugDrawRootConstants3D
return taper;
}

void setBufferHandle(uint _stream0, uint _offset = 0)
void setVertexBufferHandle(uint _stream0, uint _offset = 0)
{
stream0 = packUint16low(stream0, _stream0);
stream0Offset = _offset;
}

uint getBufferHandle()
uint getVertexBufferHandle()
{
return unpackUint16low(stream0);
}

uint getBufferOffset()
uint getVertexBufferOffset()
{
return stream0Offset;
}
Expand Down
4 changes: 2 additions & 2 deletions data/Shaders/default/default.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ VS_Output VS_Forward(uint _vertexID : VertexID)
GPUMaterialData materialData = instanceDataHeader.getGPUMaterialData(instanceDataOffset, rootConstants3D.getMatID());

Vertex vert;
vert.Load(getBuffer(rootConstants3D.getBufferHandle()), rootConstants3D.getVertexFormat(), _vertexID, rootConstants3D.getBufferOffset());
vert.Load(getBuffer(rootConstants3D.getVertexBufferHandle()), rootConstants3D.getVertexFormat(), _vertexID, rootConstants3D.getVertexBufferOffset());

ViewConstants viewConstants;
viewConstants.Load(getBuffer(RESERVEDSLOT_BUFSRV_VIEWCONSTANTS));
Expand Down Expand Up @@ -290,7 +290,7 @@ VS_Output VS_Deferred(uint _vertexID : VertexID)
GPUMaterialData materialData = instanceDataHeader.getGPUMaterialData(instanceDataOffset, rootConstants3D.getMatID());

Vertex vert;
vert.Load(getBuffer(rootConstants3D.getBufferHandle()), rootConstants3D.getVertexFormat(), _vertexID, rootConstants3D.getBufferOffset());
vert.Load(getBuffer(rootConstants3D.getVertexBufferHandle()), rootConstants3D.getVertexFormat(), _vertexID, rootConstants3D.getVertexBufferOffset());

ViewConstants viewConstants;
viewConstants.Load(getBuffer(RESERVEDSLOT_BUFSRV_VIEWCONSTANTS));
Expand Down
14 changes: 14 additions & 0 deletions data/Shaders/postprocess/postprocess.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "system/view.hlsli"
#include "system/depthstencil.hlsli"
#include "system/msaa.hlsli"
#include "system/instancedata.hlsli"

#if _FXAA
#include "FXAA.hlsli"
Expand Down Expand Up @@ -151,6 +152,19 @@ float4 DebugRayTracing(float4 color, float2 uv, uint2 screenSize, ViewConstants
color.rgb = float3(frac(primitiveIndex.xxx / 256.0f));
}
break;

case DisplayMode::RayTracing_Material:
{
uint instanceDataOffset = query.CommittedInstanceID();
GPUInstanceData instanceDataHeader = getBuffer(RESERVEDSLOT_BUFSRV_INSTANCEDATA).Load<GPUInstanceData>(instanceDataOffset);
float4 instanceColor = instanceDataHeader.getInstanceColor();

GPUMaterialData materialData = instanceDataHeader.getGPUMaterialData(instanceDataOffset, query.CommittedGeometryIndex());
float4 materialColor = materialData.getAlbedoColor();

color.rgb = instanceColor.rgb * materialColor.rgb;
}
break;
}
}
break;
Expand Down
3 changes: 2 additions & 1 deletion data/Shaders/system/displaymodes.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ vg_enum_class(DisplayMode, uint,
RayTracing_Barycentrics,
RayTracing_InstanceID,
RayTracing_GeometryIndex,
RayTracing_PrimitiveIndex,
RayTracing_PrimitiveIndex,
RayTracing_Material,

PostProcess_Depth,
PostProcess_LinearDepth,
Expand Down
86 changes: 18 additions & 68 deletions data/Shaders/system/instancedata.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,27 @@

#include "system/packing.hlsli"
#include "system/table.hlsli"
#include "system/material_consts.hlsli"
#include "system/materialdata.hlsli"

struct GPUMaterialData
{
#ifdef __cplusplus
GPUMaterialData()
{
setAlbedoTextureHandle(RESERVEDSLOT_TEXSRV_DEFAULT_ALBEDO);
setNormalTextureHandle(RESERVEDSLOT_TEXSRV_DEFAULT_NORMAL);
setPBRTextureHandle(RESERVEDSLOT_TEXSRV_DEFAULT_PBR);

setAlbedoColor(float4(1,1,1,1));
setNormalStrength(1.0f);
setOcclusion(1.0f);
setRoughness(0.25f);
setMetalness(0.0f);
setTiling(float2(1.0f, 1.0f));
}
#endif

uint4 textures; // .x = albedo | normal, .y = pbr | unused, .y = unused, .w = flags
float4 albedoColor;
float4 data;
float4 tiling;

void setAlbedoTextureHandle (uint _value) { textures.x = packUint16low(textures.x, _value); }
uint getAlbedoTextureHandle () { return unpackUint16low(textures.x); }

void setNormalTextureHandle (uint _value) { textures.x = packUint16high(textures.x, _value); }
uint getNormalTextureHandle () { return unpackUint16high(textures.x); }

void setPBRTextureHandle (uint _value) { textures.y = packUint16low(textures.y, _value); }
uint getPBRTextureHandle () { return unpackUint16low(textures.y); }

void setUVSource (UVSource _value) { textures.w = packA8(textures.w, (uint)_value); }
UVSource getUVSource () { return (UVSource)unpackA8(textures.w);}

void setAlbedoColor (float4 _value) { albedoColor = _value; }
float4 getAlbedoColor () { return albedoColor; }

void setNormalStrength (float _value) { data.w = _value; }
float getNormalStrength () { return data.w; }

void setOcclusion (float _value) { data.x = _value; }
float getOcclusion () { return data.x; }

void setRoughness (float _value) { data.y = _value; }
float getRoughness () { return data.y; }

void setMetalness (float _value) { data.z = _value; }
float getMetalness () { return data.z; }

void setTiling (float2 _value) { tiling.xy = _value; }
float2 getTiling () { return tiling.xy; }
};
#define MAX_MATERIAL_PER_INSTANCE 16

//--------------------------------------------------------------------------------------
// GPUInstanceData
// Materials use 16-bits indices and up to 16 materials per instance are supported
//--------------------------------------------------------------------------------------
struct GPUInstanceData
{
uint4 data;
uint4 m_header;
uint m_materials[MAX_MATERIAL_PER_INSTANCE];

void setMaterialCount (uint _count) { data.x = packUint16low(data.x, _count); }
uint getMaterialCount () { return unpackUint16low(data.x); }
void setMaterialCount (uint _count) { m_header.x = packUint16low(m_header.x, _count); }
uint getMaterialCount () { return unpackUint16low(m_header.x); }

void setInstanceColor (float4 _color) { data.y = packRGBA8(_color); }
float4 getInstanceColor () { return unpackRGBA8(data.y); }
void setInstanceColor (float4 _color) { m_header.y = packRGBA8(_color); }
float4 getInstanceColor () { return unpackRGBA8(m_header.y); }

void setMaterialHandle (uint _index, uint _handle) { m_materials[_index] = _handle; }
uint getMaterialHandle (uint _index) { return _index < MAX_MATERIAL_PER_INSTANCE ? m_materials[_index] : 0; }

#ifndef __cplusplus
//--------------------------------------------------------------------------------------
Expand All @@ -75,15 +31,9 @@ struct GPUInstanceData
//--------------------------------------------------------------------------------------
GPUMaterialData getGPUMaterialData (uint instanceDataOffset, uint _matID)
{
uint matIndex = _matID;
uint matOffset;

if (matIndex < getMaterialCount())
matOffset = instanceDataOffset + sizeof(GPUInstanceData) + matIndex * sizeof(GPUMaterialData);
else
matOffset = 0 + sizeof(GPUInstanceData) + 0 * sizeof(GPUMaterialData);

return getBuffer(RESERVEDSLOT_BUFSRV_INSTANCEDATA).Load<GPUMaterialData>(matOffset);
uint matIndex = getMaterialHandle(_matID);
uint matOffset = matIndex * sizeof(GPUMaterialData);
return getBuffer(RESERVEDSLOT_BUFSRV_MATERIALDATA).Load<GPUMaterialData>(matOffset);
}
#endif
};
61 changes: 61 additions & 0 deletions data/Shaders/system/materialdata.hlsli
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include "system/packing.hlsli"
#include "system/table.hlsli"
#include "system/material_consts.hlsli"

struct GPUMaterialData
{
#ifdef __cplusplus
GPUMaterialData()
{
setAlbedoTextureHandle(RESERVEDSLOT_TEXSRV_DEFAULT_ALBEDO);
setNormalTextureHandle(RESERVEDSLOT_TEXSRV_DEFAULT_NORMAL);
setPBRTextureHandle(RESERVEDSLOT_TEXSRV_DEFAULT_PBR);

setAlbedoColor(float4(1,1,1,1));
setNormalStrength(1.0f);
setOcclusion(1.0f);
setRoughness(0.25f);
setMetalness(0.0f);
setTiling(float2(1.0f, 1.0f));

tiling.zw = 0; // unused
}
#endif

uint4 textures; // .x = albedo | normal, .y = pbr | unused, .y = unused, .w = flags
float4 albedoColor;
float4 data;
float4 tiling;

void setAlbedoTextureHandle (uint _value) { textures.x = packUint16low(textures.x, _value); }
uint getAlbedoTextureHandle () { return unpackUint16low(textures.x); }

void setNormalTextureHandle (uint _value) { textures.x = packUint16high(textures.x, _value); }
uint getNormalTextureHandle () { return unpackUint16high(textures.x); }

void setPBRTextureHandle (uint _value) { textures.y = packUint16low(textures.y, _value); }
uint getPBRTextureHandle () { return unpackUint16low(textures.y); }

void setUVSource (UVSource _value) { textures.w = packA8(textures.w, (uint)_value); }
UVSource getUVSource () { return (UVSource)unpackA8(textures.w);}

void setAlbedoColor (float4 _value) { albedoColor = _value; }
float4 getAlbedoColor () { return albedoColor; }

void setNormalStrength (float _value) { data.w = _value; }
float getNormalStrength () { return data.w; }

void setOcclusion (float _value) { data.x = _value; }
float getOcclusion () { return data.x; }

void setRoughness (float _value) { data.y = _value; }
float getRoughness () { return data.y; }

void setMetalness (float _value) { data.z = _value; }
float getMetalness () { return data.z; }

void setTiling (float2 _value) { tiling.xy = _value; }
float2 getTiling () { return tiling.xy; }
};
8 changes: 4 additions & 4 deletions data/Shaders/system/rootConstants3D.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ struct RootConstants3D
return instanceDataOffset;
}

// buffer
// vertex buffer
// stream0 address : 16;
// stream0 offset : 16
void setBufferHandle(uint _stream0, uint _offset = 0)
void setVertexBufferHandle(uint _stream0, uint _offset = 0)
{
stream0 = packUint16low(stream0, _stream0);
stream0Offset = _offset;
}

uint getBufferHandle()
uint getVertexBufferHandle()
{
return unpackUint16low(stream0);
}

uint getBufferOffset()
uint getVertexBufferOffset()
{
return stream0Offset;
}
Expand Down
2 changes: 2 additions & 0 deletions data/Shaders/system/table.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ vg_enum_class(ReservedSlot, uint,
LastTexture = InvalidTexture2D,

// Buffer
MaterialDataBufSrv = BINDLESS_BUFFER_INVALID - 5,
InstanceDataBufSrv = BINDLESS_BUFFER_INVALID - 4,
SkinningMatricesBufSrv = BINDLESS_BUFFER_INVALID - 3,
LightsConstantBufSrv = BINDLESS_BUFFER_INVALID - 2,
Expand Down Expand Up @@ -82,6 +83,7 @@ vg_enum_class(ReservedSlot, uint,
#define RESERVEDSLOT_TEXSRV_IMGUIFONTTEX (BINDLESS_TEXTURE_INVALID - 1)

// Buffer SRV
#define RESERVEDSLOT_BUFSRV_MATERIALDATA (BINDLESS_BUFFER_INVALID - 5)
#define RESERVEDSLOT_BUFSRV_INSTANCEDATA (BINDLESS_BUFFER_INVALID - 4)
#define RESERVEDSLOT_BUFSRV_SKINNINGMATRICES (BINDLESS_BUFFER_INVALID - 3)
#define RESERVEDSLOT_BUFSRV_LIGHTSCONSTANTS (BINDLESS_BUFFER_INVALID - 2)
Expand Down
2 changes: 1 addition & 1 deletion src/gfx/Raytracing/TLAS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ namespace vg::gfx
//--------------------------------------------------------------------------------------
void TLAS::addInstance(const gfx::BLAS * _blas, const core::float4x4 & _world, const core::u32 _instanceID)
{
super::addInstance(_blas, _world, 0/*_instanceID*/);
super::addInstance(_blas, _world, _instanceID);
}
}
10 changes: 5 additions & 5 deletions src/renderer/DebugDraw/DebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ namespace vg::renderer
DebugDrawRootConstants3D debugDraw3D;
{
debugDraw3D.setWorldMatrix(transpose(aabbMatrixWS));
debugDraw3D.setBufferHandle(m_box->getVertexBuffer()->getBufferHandle(), m_box->getVertexBufferOffset());
debugDraw3D.setVertexBufferHandle(m_box->getVertexBuffer()->getBufferHandle(), m_box->getVertexBufferOffset());
debugDraw3D.setVertexFormat(VertexFormat::DebugDraw);
}

Expand Down Expand Up @@ -564,7 +564,7 @@ namespace vg::renderer
DebugDrawRootConstants3D debugDrawRoot3D;

debugDrawRoot3D.setWorldMatrix(float4x4::identity());
debugDrawRoot3D.setBufferHandle(m_gridVB->getBufferHandle());
debugDrawRoot3D.setVertexBufferHandle(m_gridVB->getBufferHandle());
debugDrawRoot3D.setVertexFormat(VertexFormat::DebugDraw);
debugDrawRoot3D.setColor(float4(1, 1, 1, 1));

Expand All @@ -587,7 +587,7 @@ namespace vg::renderer
DebugDrawRootConstants3D debugDrawRoot3D;

debugDrawRoot3D.setWorldMatrix(float4x4::identity());
debugDrawRoot3D.setBufferHandle(m_axisVB->getBufferHandle());
debugDrawRoot3D.setVertexBufferHandle(m_axisVB->getBufferHandle());
debugDrawRoot3D.setVertexFormat(VertexFormat::DebugDraw);
debugDrawRoot3D.setColor(float4(1, 1, 1, 1));

Expand Down Expand Up @@ -854,7 +854,7 @@ namespace vg::renderer
// Root constants
DebugDrawRootConstants3D debugDrawRoot3D;
debugDrawRoot3D.setWorldMatrix(float4x4::identity());
debugDrawRoot3D.setBufferHandle(drawData.m_debugDrawVB->getBufferHandle(), drawData.m_linesVBOffset);
debugDrawRoot3D.setVertexBufferHandle(drawData.m_debugDrawVB->getBufferHandle(), drawData.m_linesVBOffset);
debugDrawRoot3D.setVertexFormat(VertexFormat::DebugDraw);
debugDrawRoot3D.setColor(float4(1, 1, 1, 1));

Expand Down Expand Up @@ -934,7 +934,7 @@ namespace vg::renderer

// Root constants
DebugDrawRootConstants3D debugDraw3D;
debugDraw3D.setBufferHandle(_geometry->getVertexBuffer()->getBufferHandle());
debugDraw3D.setVertexBufferHandle(_geometry->getVertexBuffer()->getBufferHandle());
debugDraw3D.setVertexFormat(VertexFormat::DebugDraw);

// Transparent
Expand Down
9 changes: 6 additions & 3 deletions src/renderer/Instance/Mesh/MeshInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ namespace vg::renderer
auto * tlas = _view->getTLAS();

if (const auto * blas = getInstanceBLAS())
tlas->addInstance(blas, getGlobalMatrix(), _index);
{
u32 rayTracingID = getGPUInstanceDataOffset();
tlas->addInstance(blas, getGlobalMatrix(), rayTracingID);
}

return true;
}
Expand All @@ -260,9 +263,9 @@ namespace vg::renderer
root3D.setGPUInstanceDataOffset(getGPUInstanceDataOffset());

if (IsSkinned())
root3D.setBufferHandle(m_skinnedMeshBuffer->getBufferHandle(), m_skinnedMeshBufferOffset);
root3D.setVertexBufferHandle(m_skinnedMeshBuffer->getBufferHandle(), m_skinnedMeshBufferOffset);
else
root3D.setBufferHandle(geo->getVertexBuffer()->getBufferHandle(), geo->getVertexBufferOffset());
root3D.setVertexBufferHandle(geo->getVertexBuffer()->getBufferHandle(), geo->getVertexBufferOffset());

u16 flags = 0;
root3D.setFlags(flags);
Expand Down
Loading

0 comments on commit afae236

Please sign in to comment.