Skip to content

Commit

Permalink
Single vertex megabuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
krupitskas committed Nov 21, 2024
1 parent 0802051 commit 8e3dc11
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 434 deletions.
26 changes: 0 additions & 26 deletions Shaders/ForwardPassPS.hlsl
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
struct RS2PS
{
float4 position : SV_POSITION;

float4 position_shadow_space : POSITION;

#ifdef HAS_NORMAL
float3 normal : NORMAL;
#endif

#ifdef HAS_TANGENT
float4 tangent : TANGENT;
#endif

#ifdef HAS_TEXCOORD_0
float2 texcoord_0: TEXCOORD_0;
#endif

#ifdef HAS_TEXCOORD_1
float2 texcoord_1: TEXCOORD_1;
#endif
};

#define ALBEDO_ENABLED_BITMASK 0
Expand Down Expand Up @@ -110,17 +96,9 @@ float4 main(RS2PS input) : SV_Target
float3 normal = 0;
float4 tangent = 0;

#ifdef HAS_TEXCOORD_0
uv = input.texcoord_0;
#endif

#ifdef HAS_NORMAL
normal = input.normal;
#endif

#ifdef HAS_TANGENT
tangent = input.tangent;
#endif

float4 base_color = pbr_material.base_color_factor;
float metallicResult = pbr_material.metallic_factor;
Expand All @@ -129,8 +107,6 @@ float4 main(RS2PS input) : SV_Target
float occlusion = 0;
float3 emissive = 0;

#ifdef HAS_TEXCOORD_0

if (pbr_material.texture_enable_bitmask & (1 << ALBEDO_ENABLED_BITMASK))
{
Texture2D albedo_texture = ResourceDescriptorHeap[pbr_material.albedo_texture_index];
Expand Down Expand Up @@ -174,8 +150,6 @@ float4 main(RS2PS input) : SV_Target
emissive = emissive_texture.Sample(LinearSampler, uv).rgb;
}

#endif // HAS_TEXCOORD_0

float3 V = normalize(camera_position.xyz - input.position.xyz); // From the shading location to the camera
float3 L = directional_light_direction.xyz; //normalize(LightPosition.xyz - input.position.xyz); // From the shading location to the LIGHT
float3 N = normal; // Interpolated vertex normal
Expand Down
39 changes: 0 additions & 39 deletions Shaders/ForwardPassVS.hlsl
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
struct IA2VS
{
float3 position : POSITION;

#ifdef HAS_NORMAL
float3 normal : NORMAL;
#endif

#ifdef HAS_TANGENT
float4 tangent : TANGENT;
#endif

#ifdef HAS_TEXCOORD_0
float2 texcoord_0: TEXCOORD_0;
#endif

#ifdef HAS_TEXCOORD_1
float2 texcoord_1: TEXCOORD_1;
#endif
};

struct VS2RS
Expand All @@ -26,22 +13,9 @@ struct VS2RS
#ifndef SHADOW_PASS
float4 position_shadow_space : POSITION;
#endif

#ifdef HAS_NORMAL
float3 normal : NORMAL;
#endif

#ifdef HAS_TANGENT
float4 tangent : TANGENT;
#endif

#ifdef HAS_TEXCOORD_0
float2 texcoord_0: TEXCOORD_0;
#endif

#ifdef HAS_TEXCOORD_1
float2 texcoord_1: TEXCOORD_1;
#endif
};

#ifdef SHADOW_PASS
Expand Down Expand Up @@ -91,23 +65,10 @@ VS2RS main(IA2VS input)
#endif

output.position = mul(view_projection, output.position);

#ifdef HAS_NORMAL
output.normal = mul(float4(input.normal, 1.0), model_matrix);
#endif

#ifdef HAS_TANGENT
output.tangent.xyz = mul(float4(input.tangent.xyz, 1.0), model_matrix);
output.tangent.w = input.tangent.w;
#endif

#ifdef HAS_TEXCOORD_0
output.texcoord_0 = input.texcoord_0;
#endif

#ifdef HAS_TEXCOORD_1
output.texcoord_1 = input.texcoord_1;
#endif

return output;
}
10 changes: 0 additions & 10 deletions Yasno/Graphics/Primitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@

namespace ysn
{
struct Attribute
{
std::string name;

DXGI_FORMAT format;
D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view;
};

struct Primitive
{
PsoId pso_id = -1;
Expand All @@ -34,8 +26,6 @@ namespace ysn

int material_id = -1;

std::unordered_map<std::string, Attribute> attributes;

// Moving to new way of render
bool opaque = true; // for rtx
AABB bbox;
Expand Down
1 change: 0 additions & 1 deletion Yasno/Graphics/RenderScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ namespace ysn
std::vector<NodeTransform> transforms;

// Not sure about that
std::vector<GpuResource> buffers;
std::vector<GpuTexture> textures;

// Not sure about that x2
Expand Down
145 changes: 54 additions & 91 deletions Yasno/Graphics/Techniques/ForwardPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,57 @@

namespace ysn
{
static std::vector<DxcDefine> BuildAttributeDefines(const std::unordered_map<std::string, Attribute>& attributes)
{
std::vector<DxcDefine> defines;

for (const auto& [name, attribute] : attributes)
{
if (attribute.name == "NORMAL")
{
defines.push_back({ L"HAS_NORMAL", L"1" });
}
else if (attribute.name == "TANGENT")
{
defines.push_back({ L"HAS_TANGENT", L"1" });
}
else if (attribute.name == "TEXCOORD_0")
{
defines.push_back({ L"HAS_TEXCOORD_0", L"1" });
}
else if (attribute.name == "TEXCOORD_1")
{
defines.push_back({ L"HAS_TEXCOORD_1", L"1" });
}
}

return defines;
}


static std::vector<D3D12_INPUT_ELEMENT_DESC> BuildInputElementDescs(const std::unordered_map<std::string, Attribute>& render_attributes)
{
std::vector<D3D12_INPUT_ELEMENT_DESC> input_element_desc_arr;

for (const auto& [name, attribute] : render_attributes)
{
D3D12_INPUT_ELEMENT_DESC input_element_desc = {};

input_element_desc.SemanticName = &attribute.name[0];
input_element_desc.Format = attribute.format;

// TODO: Need to parse semantic name and index from attribute name to reduce number of ifdefs
if (attribute.name == "TEXCOORD_0")
{
input_element_desc.SemanticName = "TEXCOORD_";
input_element_desc.SemanticIndex = 0;
}

if (attribute.name == "TEXCOORD_1")
{
input_element_desc.SemanticName = "TEXCOORD_";
input_element_desc.SemanticIndex = 1;
}

if (attribute.name == "TEXCOORD_2")
{
input_element_desc.SemanticName = "TEXCOORD_";
input_element_desc.SemanticIndex = 2;
}

if (attribute.name == "COLOR_0")
{
input_element_desc.SemanticName = "COLOR_";
input_element_desc.SemanticIndex = 0;
}

if (attribute.name == "COLOR_1")
{
input_element_desc.SemanticName = "COLOR_";
input_element_desc.SemanticIndex = 1;
}

input_element_desc.InputSlot = static_cast<UINT>(input_element_desc_arr.size());
input_element_desc.AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT;
input_element_desc.InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;

input_element_desc_arr.push_back(input_element_desc);
}

return input_element_desc_arr;
}
//static std::vector<D3D12_INPUT_ELEMENT_DESC> BuildInputElementDescs(const std::unordered_map<std::string, Attribute>& render_attributes)
//{
// std::vector<D3D12_INPUT_ELEMENT_DESC> input_element_desc_arr;

// for (const auto& [name, attribute] : render_attributes)
// {
// D3D12_INPUT_ELEMENT_DESC input_element_desc = {};

// input_element_desc.SemanticName = &attribute.name[0];
// input_element_desc.Format = attribute.format;

// // TODO: Need to parse semantic name and index from attribute name to reduce number of ifdefs
// if (attribute.name == "TEXCOORD_0")
// {
// input_element_desc.SemanticName = "TEXCOORD_";
// input_element_desc.SemanticIndex = 0;
// }

// if (attribute.name == "TEXCOORD_1")
// {
// input_element_desc.SemanticName = "TEXCOORD_";
// input_element_desc.SemanticIndex = 1;
// }

// if (attribute.name == "TEXCOORD_2")
// {
// input_element_desc.SemanticName = "TEXCOORD_";
// input_element_desc.SemanticIndex = 2;
// }

// if (attribute.name == "COLOR_0")
// {
// input_element_desc.SemanticName = "COLOR_";
// input_element_desc.SemanticIndex = 0;
// }

// if (attribute.name == "COLOR_1")
// {
// input_element_desc.SemanticName = "COLOR_";
// input_element_desc.SemanticIndex = 1;
// }

// input_element_desc.InputSlot = static_cast<UINT>(input_element_desc_arr.size());
// input_element_desc.AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT;
// input_element_desc.InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;

// input_element_desc_arr.push_back(input_element_desc);
// }

// return input_element_desc_arr;
//}

bool ForwardPass::CompilePrimitivePso(ysn::Primitive& primitive, std::vector<Material> materials)
{
Expand Down Expand Up @@ -149,14 +121,11 @@ namespace ysn
new_pso_desc.SetRootSignature(root_signature);
}

const auto primitive_attributes_defines = BuildAttributeDefines(primitive.attributes);;

// Vertex shader
{
ysn::ShaderCompileParameters vs_parameters;
vs_parameters.shader_type = ysn::ShaderType::Vertex;
vs_parameters.shader_path = ysn::GetVirtualFilesystemPath(L"Shaders/ForwardPassVS.hlsl");
vs_parameters.defines = primitive_attributes_defines;

const auto vs_shader_result = renderer->GetShaderStorage()->CompileShader(&vs_parameters);

Expand All @@ -174,7 +143,6 @@ namespace ysn
ysn::ShaderCompileParameters ps_parameters;
ps_parameters.shader_type = ysn::ShaderType::Pixel;
ps_parameters.shader_path = ysn::GetVirtualFilesystemPath(L"Shaders/ForwardPassPS.hlsl");
ps_parameters.defines = primitive_attributes_defines;

const auto ps_shader_result = renderer->GetShaderStorage()->CompileShader(&ps_parameters);

Expand All @@ -187,7 +155,7 @@ namespace ysn
new_pso_desc.SetPixelShader(ps_shader_result.value()->GetBufferPointer(), ps_shader_result.value()->GetBufferSize());
}

std::vector<D3D12_INPUT_ELEMENT_DESC> input_element_desc = BuildInputElementDescs(primitive.attributes);
const auto& input_element_desc = renderer->GetInputElementsDesc();

D3D12_DEPTH_STENCIL_DESC depth_stencil_desc = {};
depth_stencil_desc.DepthEnable = true;
Expand Down Expand Up @@ -265,12 +233,7 @@ namespace ysn

for(auto& primitive : mesh.primitives)
{
uint32_t attribute_slot = 0;
for(const auto& [name, attribute] : primitive.attributes)
{
command_list->IASetVertexBuffers(attribute_slot, 1, &attribute.vertex_buffer_view);
attribute_slot += 1;
}
command_list->IASetVertexBuffers(0, 1, &primitive.vertex_buffer_view);

// TODO: check for -1 as pso_id
const std::optional<Pso> pso = renderer->GetPso(primitive.pso_id);
Expand All @@ -297,7 +260,7 @@ namespace ysn
}
else
{
//ccommand_list->DrawInstanced(primitive.vertexCount, 1, 0, 0);
command_list->DrawInstanced(primitive.vertex_count, 1, 0, 0);
}
}
else
Expand Down
Loading

0 comments on commit 8e3dc11

Please sign in to comment.