From 3b201e14d1b94b4a4a3816e3f748150a1153397b Mon Sep 17 00:00:00 2001 From: Nanako <469449812@qq.com> Date: Thu, 31 Oct 2024 14:53:38 +0800 Subject: [PATCH] =?UTF-8?q?dx=5Fpipeline=E6=96=B0=E5=A2=9Eindex=5Fbuffer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/backend/dx/dx_pipeline.cpp | 49 ++++++++++++------------- src/renderer/backend/dx/dx_pipeline.h | 4 +- src/renderer/backend/dx/dx_window.cpp | 6 ++- src/renderer/core/pipeline/pipeline.h | 4 ++ 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/renderer/backend/dx/dx_pipeline.cpp b/src/renderer/backend/dx/dx_pipeline.cpp index 5ac386d..0846119 100644 --- a/src/renderer/backend/dx/dx_pipeline.cpp +++ b/src/renderer/backend/dx/dx_pipeline.cpp @@ -7,24 +7,16 @@ #include "core/renderer/renderer.h" dx_pipeline::~dx_pipeline() { - if (vertex_shader) { - vertex_shader->Release(); - } - if (pixel_shader) { - pixel_shader->Release(); - } + if (vertex_shader) { vertex_shader->Release(); } + if (pixel_shader) { pixel_shader->Release(); } delete vertex_buffer; delete constant_buffer; - // if (compute_shader) { - // compute_shader->Release(); - // } } void dx_pipeline::use() { auto d3d_context = aorii::get_renderer()->get_d3d_context(); d3d_context->VSSetShader(vertex_shader, nullptr, 0); d3d_context->PSSetShader(pixel_shader, nullptr, 0); - // d3d_context->CSSetShader(compute_shader, nullptr, 0); d3d_context->IASetInputLayout(input_layout); } @@ -35,24 +27,24 @@ void dx_pipeline::draw() { constexpr UINT stride = sizeof(aorii_vertex_type); constexpr UINT offset = 0; auto* v_buffer = static_cast(vertex_buffer->get_native_handle()); + auto* i_buffer = static_cast(constant_buffer->get_native_handle()); auto* c_buffer = static_cast(constant_buffer->get_native_handle()); d3d_context->IASetVertexBuffers(0, 1, &v_buffer, &stride, &offset); + d3d_context->IASetIndexBuffer(i_buffer, DXGI_FORMAT_R32_UINT, 0); d3d_context->VSSetConstantBuffers(0, 1, &c_buffer); // 设置图元拓扑 d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // 绘制矩形 - d3d_context->Draw(4, 0); + d3d_context->Draw(vertex_buffer->get_size(), 0); } void dx_pipeline::load_pixel_shader() { const auto d3d_device = aorii::get_renderer()->get_d3d_device(); const auto& shader_code = load_shader(AORII_DEFAULT_PIXEL_SHADER_NAME); const auto hr = d3d_device->CreatePixelShader(shader_code.data(), shader_code.size(), nullptr, &pixel_shader); - if (FAILED(hr)) { - spdlog::critical("无法创建像素着色器: {0}", hr); - } + if (FAILED(hr)) { spdlog::critical("无法创建像素着色器: {0}", hr); } } void dx_pipeline::load_vertex_shader() { @@ -64,8 +56,8 @@ void dx_pipeline::load_vertex_shader() { return; } D3D11_INPUT_ELEMENT_DESC layout_desc[] = { - {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, - {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0} + { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; hr = d3d_device->CreateInputLayout(layout_desc, 2, shader_code.data(), shader_code.size(), &input_layout); if (FAILED(hr)) { @@ -79,26 +71,31 @@ void dx_pipeline::build_vertex_buffer(int vertex_count) { vertex_buffer = new dx_buffer(buffer_type::vertex, vertex_count); } +void dx_pipeline::build_index_buffer(int triangle_count) { + delete triangle_buffer; + triangle_buffer = new dx_buffer(buffer_type::index, triangle_count); +} + void dx_pipeline::build_constant_buffer() { constant_buffer = new dx_buffer(buffer_type::constant, 1); } -void dx_pipeline::set_vertex(const std::span& in_vertexes) { - if (vertex_buffer->get_size() > in_vertexes.size()) { - vertex_buffer->resize(in_vertexes.size()); - } +void dx_pipeline::set_triangle(const std::span& in_vertexes, + const std::span& in_triangles) { + if (vertex_buffer->get_size() > in_vertexes.size()) { vertex_buffer->resize(in_vertexes.size()); } aorii_vertex_type* v_buffer = vertex_buffer->lock(); std::ranges::copy(in_vertexes, v_buffer); vertex_buffer->unlock(); + + if (triangle_buffer->get_size() > in_triangles.size()) { triangle_buffer->resize(in_triangles.size()); } + aorii_triangle_type* i_buffer = triangle_buffer->lock(); + std::ranges::copy(in_triangles, i_buffer); + triangle_buffer->unlock(); } -aorii_constant_buffer_type* dx_pipeline::lock_constant_buffer() { - return constant_buffer->lock(); -} +aorii_constant_buffer_type* dx_pipeline::lock_constant_buffer() { return constant_buffer->lock(); } -void dx_pipeline::unlock_constant_buffer() { - constant_buffer->unlock(); -} +void dx_pipeline::unlock_constant_buffer() { constant_buffer->unlock(); } std::vector dx_pipeline::load_shader(const std::string& shader_name) { auto file_pathname = aorii::get_shader_path(shader_name).generic_string() + ".dxbc"; diff --git a/src/renderer/backend/dx/dx_pipeline.h b/src/renderer/backend/dx/dx_pipeline.h index d0d117b..0feb641 100644 --- a/src/renderer/backend/dx/dx_pipeline.h +++ b/src/renderer/backend/dx/dx_pipeline.h @@ -17,9 +17,10 @@ public: void load_pixel_shader(); void load_vertex_shader(); void build_vertex_buffer(int vertex_count); + void build_index_buffer(int triangle_count); void build_constant_buffer(); - void set_vertex(const std::span& in_vertexes); + void set_triangle(const std::span& in_vertexes, const std::span& in_triangles); aorii_constant_buffer_type* lock_constant_buffer(); void unlock_constant_buffer(); @@ -32,6 +33,7 @@ private: ID3D11InputLayout* input_layout = nullptr; dx_buffer* vertex_buffer = nullptr; + dx_buffer* triangle_buffer = nullptr; dx_buffer* constant_buffer = nullptr; int vertex_count = 0; }; diff --git a/src/renderer/backend/dx/dx_window.cpp b/src/renderer/backend/dx/dx_window.cpp index d590897..4422348 100644 --- a/src/renderer/backend/dx/dx_window.cpp +++ b/src/renderer/backend/dx/dx_window.cpp @@ -67,6 +67,7 @@ bool dx_window::create_surface(GLFWwindow* in_window) { } pipeline.build_vertex_buffer(4); + pipeline.build_index_buffer(2); pipeline.build_constant_buffer(); hr = build_render_target_view(); @@ -104,7 +105,10 @@ void dx_window::begin_frame() { const std::array vertices = { v1, v2, v3, v4 }; - pipeline.set_vertex({vertices.data(), vertices.size()}); + const aorii_triangle_type t1 = {0, 1, 2}; + const aorii_triangle_type t2 = {1, 2, 3}; + const std::array indices = { t1, t2 }; + pipeline.set_triangle({vertices.data(), vertices.size()}, {indices.data(), indices.size()}); } pipeline.draw(); diff --git a/src/renderer/core/pipeline/pipeline.h b/src/renderer/core/pipeline/pipeline.h index 4a0d51d..2345342 100644 --- a/src/renderer/core/pipeline/pipeline.h +++ b/src/renderer/core/pipeline/pipeline.h @@ -11,10 +11,14 @@ struct aorii_vertex { Eigen::Vector2f position; linear_color color; }; +struct aorii_triangle { + uint32_t index[3]; +}; struct aorii_constant_buffer { Eigen::Matrix4f projection_matrix; }; using aorii_vertex_type = aorii_vertex; +using aorii_triangle_type = aorii_triangle; using aorii_constant_buffer_type = aorii_constant_buffer; class pipeline {