| 120 | static_assert(sizeof(ConstantBufferEntry) == nvrhi::c_ConstantBufferOffsetSizeAlignment, "sizeof(ConstantBufferEntry) must be 256 bytes"); |
| 121 | |
| 122 | bool Init() |
| 123 | { |
| 124 | auto nativeFS = std::make_shared<vfs::NativeFileSystem>(); |
| 125 | |
| 126 | std::filesystem::path frameworkShaderPath = app::GetDirectoryWithExecutable() / "shaders/framework" / app::GetShaderTypeName(GetDevice()->getGraphicsAPI()); |
| 127 | std::filesystem::path appShaderPath = app::GetDirectoryWithExecutable() / "shaders/vertex_buffer" / app::GetShaderTypeName(GetDevice()->getGraphicsAPI()); |
| 128 | |
| 129 | std::shared_ptr<vfs::RootFileSystem> rootFS = std::make_shared<vfs::RootFileSystem>(); |
| 130 | rootFS->mount("/shaders/donut", frameworkShaderPath); |
| 131 | rootFS->mount("/shaders/app", appShaderPath); |
| 132 | |
| 133 | std::shared_ptr<engine::ShaderFactory> shaderFactory = std::make_shared<engine::ShaderFactory>(GetDevice(), rootFS, "/shaders"); |
| 134 | m_VertexShader = shaderFactory->CreateShader("app/shaders.hlsl", "main_vs", nullptr, nvrhi::ShaderType::Vertex); |
| 135 | m_PixelShader = shaderFactory->CreateShader("app/shaders.hlsl", "main_ps", nullptr, nvrhi::ShaderType::Pixel); |
| 136 | |
| 137 | if (!m_VertexShader || !m_PixelShader) |
| 138 | { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | m_ConstantBuffer = GetDevice()->createBuffer(nvrhi::utils::CreateStaticConstantBufferDesc(sizeof(ConstantBufferEntry) * c_NumViews, "ConstantBuffer") |
| 143 | .setInitialState(nvrhi::ResourceStates::ConstantBuffer).setKeepInitialState(true)); |
| 144 | |
| 145 | nvrhi::VertexAttributeDesc attributes[] = { |
| 146 | nvrhi::VertexAttributeDesc() |
| 147 | .setName("POSITION") |
| 148 | .setFormat(nvrhi::Format::RGB32_FLOAT) |
| 149 | .setOffset(0) |
| 150 | .setBufferIndex(0) |
| 151 | .setElementStride(sizeof(Vertex)), |
| 152 | nvrhi::VertexAttributeDesc() |
| 153 | .setName("UV") |
| 154 | .setFormat(nvrhi::Format::RG32_FLOAT) |
| 155 | .setOffset(0) |
| 156 | .setBufferIndex(1) |
| 157 | .setElementStride(sizeof(Vertex)), |
| 158 | }; |
| 159 | m_InputLayout = GetDevice()->createInputLayout(attributes, uint32_t(std::size(attributes)), m_VertexShader); |
| 160 | |
| 161 | |
| 162 | engine::CommonRenderPasses commonPasses(GetDevice(), shaderFactory); |
| 163 | engine::TextureCache textureCache(GetDevice(), nativeFS, nullptr); |
| 164 | |
| 165 | m_CommandList = GetDevice()->createCommandList(); |
| 166 | m_CommandList->open(); |
| 167 | |
| 168 | nvrhi::BufferDesc vertexBufferDesc; |
| 169 | vertexBufferDesc.byteSize = sizeof(g_Vertices); |
| 170 | vertexBufferDesc.isVertexBuffer = true; |
| 171 | vertexBufferDesc.debugName = "VertexBuffer"; |
| 172 | vertexBufferDesc.initialState = nvrhi::ResourceStates::CopyDest; |
| 173 | m_VertexBuffer = GetDevice()->createBuffer(vertexBufferDesc); |
| 174 | |
| 175 | m_CommandList->beginTrackingBufferState(m_VertexBuffer, nvrhi::ResourceStates::CopyDest); |
| 176 | m_CommandList->writeBuffer(m_VertexBuffer, g_Vertices, sizeof(g_Vertices)); |
| 177 | m_CommandList->setPermanentBufferState(m_VertexBuffer, nvrhi::ResourceStates::VertexBuffer); |
| 178 | |
| 179 | nvrhi::BufferDesc indexBufferDesc; |