Skip to content

Commit

Permalink
Provided more buffers to RTX, linear sampler, proper global root sign…
Browse files Browse the repository at this point in the history
…ature flag
  • Loading branch information
krupitskas committed Nov 23, 2024
1 parent 8523d07 commit 31f91fd
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 62 deletions.
4 changes: 3 additions & 1 deletion Shaders/ForwardPassPS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ struct PerInstanceData
{
float4x4 model_matrix;
int material_id;
int pad[3];
int indices_count;
int indices_before;
int pad;
};
StructuredBuffer<PerInstanceData> per_instance_data : register(t0);

Expand Down
4 changes: 3 additions & 1 deletion Shaders/ForwardPassVS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ struct PerInstanceData
{
float4x4 model_matrix;
int material_id;
int pad[3];
int indices_count;
int indices_before;
int pad;
};
StructuredBuffer<PerInstanceData> per_instance_data : register(t0);

Expand Down
120 changes: 108 additions & 12 deletions Shaders/HitRT.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,130 @@ struct Attributes
float2 bary;
};

inline uint PackInstanceID(uint material_id, uint geometry_id)
{
return ((geometry_id & 0x3FFF) << 10) | (material_id & 0x3FF);
}

inline void UnpackInstanceID(uint instanceID, out uint material_id, out uint geometry_id)
{
material_id = instanceID & 0x3FF;
geometry_id = (instanceID >> 10) & 0x3FFF;
}

// Vertex data for usage in Hit shader
struct VertexData
{
float3 position;
float3 shading_normal;
float3 geometry_normal;
float2 uv;
};

// Vertex buffer layout data
struct VertexLayout
{
float3 position;
float3 normal;
float4 tangent;
float2 texcoord_0;
};
StructuredBuffer<VertexLayout> VertexBuffer : register(t1);

inline uint PackInstanceID(uint material_id, uint geometry_id)
// Index Buffer data
StructuredBuffer<uint> IndexBuffer : register(t2);

// Material Buffer Data
struct SurfaceShaderParameters
{
return ((geometry_id & 0x3FFF) << 10) | (material_id & 0x3FF);
}
float4 base_color_factor;
float metallic_factor;
float roughness_factor;

inline void UnpackInstanceID(uint instanceID, out uint material_id, out uint geometry_id)
int texture_enable_bitmask; // Encoded which textures are should be used

int albedo_texture_index;
int metallic_roughness_texture_index;
int normal_texture_index;
int occlusion_texture_index;
int emissive_texture_index;
};
StructuredBuffer<SurfaceShaderParameters> MaterialBuffer : register(t3);

// Per instance data
struct PerInstanceData
{
material_id = instanceID & 0x3FF;
geometry_id = (instanceID >> 10) & 0x3FFF;
float4x4 model_matrix;
int material_id;
int indices_count;
int indices_before;
int pad;
};
StructuredBuffer<PerInstanceData> PerInstanceBuffer : register(t4);

uint3 GetIndices(uint geometry_id, uint triangle_index)
{
PerInstanceData data = PerInstanceBuffer[geometry_id];

uint base_index = (triangle_index * 3);
int address = data.indices_before + base_index;

uint i0 = IndexBuffer[address];
uint i1 = IndexBuffer[address + 1];
uint i2 = IndexBuffer[address + 2];

return uint3(i0, i1, i2);
}

StructuredBuffer<VertexLayout> VertexBuffer : register(t0);
StructuredBuffer<uint> IndexBuffer : register(t1);
VertexData GetVertexData(uint geometry_id, uint triangle_index, float3 barycentrics)
{
// Get the triangle indices
uint3 indices = GetIndices(geometry_id, triangle_index);

VertexData v = (VertexData)0;

float3 triangle_vertices[3];

// Interpolate the vertex attributes
//for (uint i = 0; i < 3; i++)
//{
// int address = (indices[i] * 12) * 4;
//
// // Load and interpolate position and transform it to world space
// triangle_vertices[i] = mul(ObjectToWorld3x4(), float4(asfloat(VertexBuffer[geometry_id].Load3(address)), 1.0f)).xyz;
// v.position += triangle_vertices[i] * barycentrics[i];
// address += 12;
//
// // Load and interpolate normal
// v.normal += asfloat(VertexBuffer[geometry_id].Load3(address)) * barycentrics[i];
// address += 12;
//
// // Load and interpolate tangent
// address += 12;
//
// // Load bitangent direction
// address += 4;
//
// // Load and interpolate texture coordinates
// v.uv += asfloat(vertices[geometry_id].Load2(address)) * barycentrics[i];
//}

//// Transform normal from local to world space
//v.normal = normalize(mul(ObjectToWorld3x4(), float4(v.normal, 0.0f)).xyz);

//// Calculate geometry normal from triangle vertices positions
//float3 edge20 = triangle_vertices[2] - triangle_vertices[0];
//float3 edge21 = triangle_vertices[2] - triangle_vertices[1];
//float3 edge10 = triangle_vertices[1] - triangle_vertices[0];

//v.geometryNormal = normalize(cross(edge20, edge10));

return v;
}

[shader("closesthit")]
void ClosestHit(inout HitInfo payload, Attributes attrib)
{
Texture2D albedo_texture = ResourceDescriptorHeap[3];

uint primitive_index = PrimitiveIndex();

float3 barycentrics = float3(1.f - attrib.bary.x - attrib.bary.y, attrib.bary.x, attrib.bary.y);
Expand All @@ -54,8 +151,7 @@ void ClosestHit(inout HitInfo payload, Attributes attrib)
uint geometry_id;
UnpackInstanceID(primitive_index, material_id, geometry_id);

//VertexLayout vertex_data = GetVertexAttributes(geometry_id, primitive_index, barycentrics);

VertexData vertex_data = GetVertexData(geometry_id, primitive_index, barycentrics);

const float3 A = float3(1, 0, 0);
const float3 B = float3(0, 1, 0);
Expand Down
62 changes: 48 additions & 14 deletions Shaders/RayGenRT.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,47 @@ uint4 Pcg4d(uint4 v)
return v;
}

struct VertexLayout
{
float3 position;
float3 normal;
float4 tangent;
float2 texcoord_0;
};
// Raytracing acceleration structure, accessed as a SRV
RaytracingAccelerationStructure SceneBVH : register(t0);
StructuredBuffer<VertexLayout> VertexBuffer : register(t1);
StructuredBuffer<uint> IndexBuffer : register(t2);

// Material Buffer Data
struct SurfaceShaderParameters
{
float4 base_color_factor;
float metallic_factor;
float roughness_factor;

int texture_enable_bitmask; // Encoded which textures are should be used

int albedo_texture_index;
int metallic_roughness_texture_index;
int normal_texture_index;
int occlusion_texture_index;
int emissive_texture_index;
};
StructuredBuffer<SurfaceShaderParameters> MaterialBuffer : register(t3);

struct PerInstanceData
{
float4x4 model_matrix;
int material_id;
int indices_count;
int indices_before;
int pad;
};
StructuredBuffer<PerInstanceData> PerInstanceBuffer : register(t4);

// Texture Sampler
SamplerState LinearSampler : register(s0);

[shader("raygeneration")]
void RayGen()
Expand Down Expand Up @@ -114,19 +153,6 @@ void RayGen()
ray.TMin = 0;
ray.TMax = 100000;

const int max_bounces = 16;

for (int i = 0; i < max_bounces; i++)
{

}

float3 geometryNormal;
float3 shadingNormal;

//DecodeNormals(payload.encodedNormals, geometryNormal, shadingNormal);


TraceRay(SceneBVH,
RAY_FLAG_NONE,
0xFF,
Expand All @@ -137,5 +163,13 @@ void RayGen()
payload
);

gOutput[pixel_xy] = float4(payload.color_distance.rgb, 1.f);
SurfaceShaderParameters mat = MaterialBuffer[0];

Texture2D albedo_texture = ResourceDescriptorHeap[mat.albedo_texture_index];

float2 uvs = float2(0.0f, 0.0f);

float3 albedo_color = albedo_texture.SampleLevel(LinearSampler, uvs, 0.0f).rgb;

gOutput[pixel_xy] = float4(payload.color_distance.rgb * albedo_color.rgb, 1.f);
}
6 changes: 4 additions & 2 deletions Yasno/Graphics/RenderScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ namespace ysn
geometry_id = (instance_id >> 10) & 0x3FFF;
}

// Per instance data for rendering
// Per instance data for rendering, this can be split into smaller parts
YSN_SHADER_STRUCT RenderInstanceData
{
DirectX::XMMATRIX model_matrix;
int32_t material_id;
int32_t pad[3];
int32_t indices_count;
int32_t indices_before; // offset
int32_t pad;
};

struct RenderScene
Expand Down
Loading

0 comments on commit 31f91fd

Please sign in to comment.