diff --git a/Shaders/ForwardPassPS.hlsl b/Shaders/ForwardPassPS.hlsl index 5cc7c41..de2b14b 100644 --- a/Shaders/ForwardPassPS.hlsl +++ b/Shaders/ForwardPassPS.hlsl @@ -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 @@ -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; @@ -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]; @@ -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 diff --git a/Shaders/ForwardPassVS.hlsl b/Shaders/ForwardPassVS.hlsl index 8948d66..6bb7048 100644 --- a/Shaders/ForwardPassVS.hlsl +++ b/Shaders/ForwardPassVS.hlsl @@ -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 @@ -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 @@ -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; } diff --git a/Yasno/Graphics/Primitive.hpp b/Yasno/Graphics/Primitive.hpp index 50e5628..2231b50 100644 --- a/Yasno/Graphics/Primitive.hpp +++ b/Yasno/Graphics/Primitive.hpp @@ -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; @@ -34,8 +26,6 @@ namespace ysn int material_id = -1; - std::unordered_map attributes; - // Moving to new way of render bool opaque = true; // for rtx AABB bbox; diff --git a/Yasno/Graphics/RenderScene.hpp b/Yasno/Graphics/RenderScene.hpp index ce542ae..fa3bb63 100644 --- a/Yasno/Graphics/RenderScene.hpp +++ b/Yasno/Graphics/RenderScene.hpp @@ -39,7 +39,6 @@ namespace ysn std::vector transforms; // Not sure about that - std::vector buffers; std::vector textures; // Not sure about that x2 diff --git a/Yasno/Graphics/Techniques/ForwardPass.cpp b/Yasno/Graphics/Techniques/ForwardPass.cpp index 2fc78b4..d5e4968 100644 --- a/Yasno/Graphics/Techniques/ForwardPass.cpp +++ b/Yasno/Graphics/Techniques/ForwardPass.cpp @@ -7,85 +7,57 @@ namespace ysn { - static std::vector BuildAttributeDefines(const std::unordered_map& attributes) - { - std::vector 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 BuildInputElementDescs(const std::unordered_map& render_attributes) - { - std::vector 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(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 BuildInputElementDescs(const std::unordered_map& render_attributes) + //{ + // std::vector 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(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 materials) { @@ -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); @@ -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); @@ -187,7 +155,7 @@ namespace ysn new_pso_desc.SetPixelShader(ps_shader_result.value()->GetBufferPointer(), ps_shader_result.value()->GetBufferSize()); } - std::vector 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; @@ -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 = renderer->GetPso(primitive.pso_id); @@ -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 diff --git a/Yasno/Graphics/Techniques/ShadowMapPass.cpp b/Yasno/Graphics/Techniques/ShadowMapPass.cpp index c73a273..92b944f 100644 --- a/Yasno/Graphics/Techniques/ShadowMapPass.cpp +++ b/Yasno/Graphics/Techniques/ShadowMapPass.cpp @@ -8,85 +8,57 @@ namespace ysn { - static std::vector BuildAttributeDefines(const std::unordered_map& attributes) - { - std::vector 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 BuildInputElementDescs(const std::unordered_map& render_attributes) - { - std::vector 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(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 BuildInputElementDescs(const std::unordered_map& render_attributes) + //{ + // std::vector 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(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 ShadowMapPass::InitializeCamera(std::shared_ptr p_renderer) @@ -162,14 +134,12 @@ 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; vs_parameters.defines.emplace_back(L"SHADOW_PASS"); const auto vs_shader_result = renderer->GetShaderStorage()->CompileShader(&vs_parameters); @@ -188,7 +158,6 @@ namespace ysn ysn::ShaderCompileParameters ps_parameters; ps_parameters.shader_type = ysn::ShaderType::Pixel; ps_parameters.shader_path = ysn::GetVirtualFilesystemPath(L"Shaders/ShadowPassPS.hlsl"); - ps_parameters.defines = primitive_attributes_defines; const auto ps_shader_result = renderer->GetShaderStorage()->CompileShader(&ps_parameters); @@ -201,7 +170,7 @@ namespace ysn new_pso_desc.SetPixelShader(ps_shader_result.value()->GetBufferPointer(), ps_shader_result.value()->GetBufferSize()); } - std::vector 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; @@ -351,12 +320,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 = renderer->GetPso(primitive.shadow_pso_id); @@ -381,7 +345,7 @@ namespace ysn } else { - //command_list->DrawInstanced(primitive.vertexCount, 1, 0, 0); + command_list->DrawInstanced(primitive.vertex_count, 1, 0, 0); } } else diff --git a/Yasno/Renderer/DxRenderer.cpp b/Yasno/Renderer/DxRenderer.cpp index c19b87e..bc3f31f 100644 --- a/Yasno/Renderer/DxRenderer.cpp +++ b/Yasno/Renderer/DxRenderer.cpp @@ -115,6 +115,42 @@ namespace return allow_tearing; } + + std::vector CreateDefaultInputElementDescArray() + { + std::vector result; + + result.push_back({ + .SemanticName = "POSITION", + .Format = DXGI_FORMAT_R32G32B32_FLOAT, + .AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT, + .InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA + }); + + result.push_back({ + .SemanticName = "NORMAL", + .Format = DXGI_FORMAT_R32G32B32_FLOAT, + .AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT, + .InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA + }); + + result.push_back({ + .SemanticName = "TANGENT", + .Format = DXGI_FORMAT_R32G32B32A32_FLOAT, + .AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT, + .InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA + }); + + result.push_back({ + .SemanticName = "TEXCOORD_", + .SemanticIndex = 0, + .Format = DXGI_FORMAT_R32G32_FLOAT, + .AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT, + .InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA + }); + + return result; + } } // namespace namespace ysn @@ -179,6 +215,8 @@ namespace ysn return false; } + m_default_input_elements_desc = CreateDefaultInputElementDescArray(); + return true; } @@ -269,6 +307,11 @@ namespace ysn return backbuffer_format; } + const std::vector& DxRenderer::GetInputElementsDesc() + { + return m_default_input_elements_desc; + } + void DxRenderer::FlushQueues() { m_direct_command_queue->Flush(); diff --git a/Yasno/Renderer/DxRenderer.hpp b/Yasno/Renderer/DxRenderer.hpp index 78d0797..a4816bf 100644 --- a/Yasno/Renderer/DxRenderer.hpp +++ b/Yasno/Renderer/DxRenderer.hpp @@ -46,6 +46,8 @@ namespace ysn DXGI_FORMAT GetHdrFormat() const; DXGI_FORMAT GetBackBufferFormat() const; + const std::vector& GetInputElementsDesc(); + void FlushQueues(); bool IsTearingSupported(); @@ -64,6 +66,7 @@ namespace ysn wil::com_ptr m_swap_chain; // Data + std::vector m_default_input_elements_desc; VertexStorage m_vertex_storage; IndexStorage m_index_storage; std::shared_ptr m_shader_storage; diff --git a/Yasno/Renderer/RaytracingContext.cpp b/Yasno/Renderer/RaytracingContext.cpp index f9a42c8..91d830f 100644 --- a/Yasno/Renderer/RaytracingContext.cpp +++ b/Yasno/Renderer/RaytracingContext.cpp @@ -167,12 +167,11 @@ void ysn::RaytracingContext::CreateAccelerationStructures(wil::com_ptr dst_buffer; - { - D3D12_HEAP_PROPERTIES heap_properties = {}; - heap_properties.Type = D3D12_HEAP_TYPE_DEFAULT; - heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; - heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; - - D3D12_RESOURCE_DESC resource_desc = {}; - resource_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; - resource_desc.Alignment = 0; - resource_desc.Width = buffer.data.size(); - resource_desc.Height = 1; - resource_desc.DepthOrArraySize = 1; - resource_desc.MipLevels = 1; - resource_desc.Format = DXGI_FORMAT_UNKNOWN; - resource_desc.SampleDesc = { 1, 0 }; - resource_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; - resource_desc.Flags = D3D12_RESOURCE_FLAG_NONE; - - hr = dx_renderer->GetDevice()->CreateCommittedResource(&heap_properties, - D3D12_HEAP_FLAG_NONE, - &resource_desc, - D3D12_RESOURCE_STATE_COPY_DEST, - nullptr, - IID_PPV_ARGS(&dst_buffer)); - - #ifndef YSN_RELEASE - std::wstring name(buffer.name.begin(), buffer.name.end()); - name = name.empty() ? L"GLTF Buffer Dest" : name; - dst_buffer->SetName(name.c_str()); - #endif - - if (hr != S_OK) - { - LogError << "Can't allocate GLTF dst buffer\n"; - return false; - } - } - - wil::com_ptr src_buffer; - { - D3D12_HEAP_PROPERTIES heap_properties = {}; - heap_properties.Type = D3D12_HEAP_TYPE_UPLOAD; - heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; - heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; - - D3D12_RESOURCE_DESC resource_desc = {}; - resource_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; - resource_desc.Alignment = 0; - resource_desc.Width = buffer.data.size(); - resource_desc.Height = 1; - resource_desc.DepthOrArraySize = 1; - resource_desc.MipLevels = 1; - resource_desc.Format = DXGI_FORMAT_UNKNOWN; - resource_desc.SampleDesc = { 1, 0 }; - resource_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; - resource_desc.Flags = D3D12_RESOURCE_FLAG_NONE; - - hr = dx_renderer->GetDevice()->CreateCommittedResource(&heap_properties, - D3D12_HEAP_FLAG_NONE, - &resource_desc, - D3D12_RESOURCE_STATE_GENERIC_READ, - nullptr, IID_PPV_ARGS(&src_buffer)); - - if (hr != S_OK) - { - LogError << "Can't allocate GLTF src buffer\n"; - return false; - } - - build_context.staging_resources.push_back(src_buffer); - - #ifndef YSN_RELEASE - std::wstring name(buffer.name.begin(), buffer.name.end()); - name = name.empty() ? L"GLTF Buffer Source" : name; - src_buffer->SetName(name.c_str()); - #endif - - void* data_ptr; - hr = src_buffer->Map(0, nullptr, &data_ptr); - - if (hr != S_OK) - { - LogError << "Can't map GLTF src buffer\n"; - return false; - } - - // Copy cpu data to cpu src buffer - memcpy(data_ptr, &buffer.data[0], buffer.data.size()); - } - - // Copy from CPU to GPU buffer - build_context.copy_cmd_list->CopyBufferRegion(dst_buffer.get(), 0, src_buffer.get(), 0, buffer.data.size()); - - model.buffers.push_back(dst_buffer); - } - - return true; -} - - static DXGI_FORMAT GetSrgbFormat(DXGI_FORMAT format) { switch (format) @@ -507,10 +396,6 @@ static uint32_t BuildIndexBuffer(ysn::Primitive& primitive, const ysn::Model& mo const UINT8* index_buffer_address = index_buffer.data.data(); const UINT8* base_address = index_buffer_address + index_buffer_view.byteOffset + index_accessor.byteOffset; - //primitive.index_buffer_view.BufferLocation = model.buffers[gltf_buffer_view.buffer].GetGPUVirtualAddress() + gltf_buffer_view.byteOffset + gltf_accessor.byteOffset; - //primitive.index_buffer_view.SizeInBytes = static_cast(gltf_buffer_view.byteLength - gltf_accessor.byteOffset); - //primitive.index_buffer_view.Format = DXGI_FORMAT_R32_UINT; - const int index_stride = tinygltf::GetComponentSizeInBytes(index_accessor.componentType) * tinygltf::GetNumComponentsInType(index_accessor.type); if (index_stride == 1) @@ -533,8 +418,6 @@ static uint32_t BuildIndexBuffer(ysn::Primitive& primitive, const ysn::Model& mo memcpy(half.data(), base_address, (index_accessor.count * index_stride)); - //std::copy(base_address, base_address + (index_accessor.count * index_stride), half.data()); - // Convert half precision indices to full precision for (size_t i = 0; i < index_accessor.count; i++) { @@ -556,49 +439,6 @@ static uint32_t BuildIndexBuffer(ysn::Primitive& primitive, const ysn::Model& mo return 0; } -static void BuildAccessorType(ysn::Attribute& attribute, const int gltf_accessorType) -{ - switch (gltf_accessorType) - { - case TINYGLTF_TYPE_VEC2: - attribute.format = DXGI_FORMAT_R32G32_FLOAT; - break; - case TINYGLTF_TYPE_VEC3: - attribute.format = DXGI_FORMAT_R32G32B32_FLOAT; - break; - case TINYGLTF_TYPE_VEC4: - attribute.format = DXGI_FORMAT_R32G32B32A32_FLOAT; - break; - default:; - } -} - -static void BuildAttributesAccessors(ysn::Primitive& primitive, - const ysn::Model& model, - const tinygltf::Model& gltf_model, - const std::map& gltf_primitive_attributes) -{ - for (const auto& [gltf_attribute_name, gltf_accessor_index] : gltf_primitive_attributes) - { - const tinygltf::Accessor& gltf_accessor = gltf_model.accessors[gltf_accessor_index]; - const tinygltf::BufferView& gltf_buffer_view = gltf_model.bufferViews[gltf_accessor.bufferView]; - - ysn::Attribute render_attribute; - render_attribute.name = gltf_attribute_name; - - BuildAccessorType(render_attribute, gltf_accessor.type); - - D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view = {}; - vertex_buffer_view.BufferLocation = model.buffers[gltf_buffer_view.buffer].GetGPUVirtualAddress() + gltf_buffer_view.byteOffset + gltf_accessor.byteOffset; - vertex_buffer_view.SizeInBytes = static_cast(gltf_buffer_view.byteLength - gltf_accessor.byteOffset); - vertex_buffer_view.StrideInBytes = gltf_accessor.ByteStride(gltf_buffer_view); - - render_attribute.vertex_buffer_view = vertex_buffer_view; - - primitive.attributes.emplace(gltf_attribute_name, render_attribute); - } -} - static uint32_t BuildVertexBuffer(ysn::Primitive& primitive, const tinygltf::Primitive& gltf_primitive, const tinygltf::Model& gltf_model) { int position_index = -1; @@ -671,7 +511,7 @@ static uint32_t BuildVertexBuffer(ysn::Primitive& primitive, const tinygltf::Pri const tinygltf::Buffer& tangent_buffer = gltf_model.buffers[tangent_buffer_view.buffer]; tangent_buffer_address = tangent_buffer.data.data(); tangent_stride = tinygltf::GetComponentSizeInBytes(tangent_accessor.componentType) * tinygltf::GetNumComponentsInType(tangent_accessor.type); - YSN_ASSERT_MSG(tangent_stride == 16, "GLTF model vertex tangent stride not equals 12"); + YSN_ASSERT_MSG(tangent_stride == 16, "GLTF model vertex tangent stride not equals 16"); } // Vertex texture coordinates @@ -742,7 +582,7 @@ static BuildMeshResult BuildMeshes(ysn::Model& model, const tinygltf::Model& glt ysn::Primitive primitive; result.mesh_vertices_count += BuildVertexBuffer(primitive, gltf_primitive, gltf_model); - BuildAttributesAccessors(primitive, model, gltf_model, gltf_primitive.attributes); + //BuildAttributesAccessors(primitive, model, gltf_model, gltf_primitive.attributes); result.mesh_indices_count += BuildIndexBuffer(primitive, model, gltf_primitive.indices, gltf_model); BuildPrimitiveTopology(primitive, gltf_primitive.mode); @@ -1038,16 +878,12 @@ namespace ysn load_gltf_context.staging_resources.reserve(256); load_gltf_context.copy_cmd_list = command_queue->GetCommandList("GLTF upload"); - if(!BuildBuffers(model, load_gltf_context, gltf_model)) - { - LogError << "GLTF loader can't build buffers\n"; - return false; - } if(!BuildImages(model, load_gltf_context, gltf_model)) { LogError << "GLTF loader can't build materials\n"; return false; } + if(!BuildMaterials(model, load_gltf_context, gltf_model)) { LogError << "GLTF loader can't build materials\n"; diff --git a/Yasno/Yasno/Yasno.cpp b/Yasno/Yasno/Yasno.cpp index 05f08f2..3999fd9 100644 --- a/Yasno/Yasno/Yasno.cpp +++ b/Yasno/Yasno/Yasno.cpp @@ -249,7 +249,6 @@ namespace ysn if (capture_loading_pix) { - PIXCaptureParameters pix_capture_parameters; pix_capture_parameters.GpuCaptureParameters.FileName = L"Yasno.pix"; PIXSetTargetWindow(m_pWindow->GetWindowHandle());