From 804d336bcace999d3d7db957d3104ab03c0114e9 Mon Sep 17 00:00:00 2001 From: Nanako <469449812@qq.com> Date: Thu, 31 Oct 2024 11:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=BD=E5=90=8D=E7=AE=A1=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E5=B0=86pipeline=E6=8B=86=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/shader/default_shader.slang | 3 +- src/renderer/backend/dx/dx_buffer.h | 6 +-- src/renderer/backend/dx/dx_pipeline.cpp | 45 ++++++++----------- src/renderer/backend/dx/dx_pipeline.h | 22 ++++----- src/renderer/backend/dx/dx_window.cpp | 47 +++++++++++--------- src/renderer/backend/dx/dx_window.h | 3 +- src/renderer/core/pipeline/pipeline.h | 37 +++++++++++---- src/renderer/core/renderer/renderer_buffer.h | 18 ++++---- 8 files changed, 99 insertions(+), 82 deletions(-) diff --git a/resource/shader/default_shader.slang b/resource/shader/default_shader.slang index 2653b26..bd2517b 100644 --- a/resource/shader/default_shader.slang +++ b/resource/shader/default_shader.slang @@ -2,7 +2,8 @@ struct MatrixBuffer { matrix transform; }; -ParameterBlock matrix_buffer; +ParameterBlock matrix_buffer : register(b0); +SamplerState texture_sampler : register(s0); struct VSInput { float2 position : POSITION; diff --git a/src/renderer/backend/dx/dx_buffer.h b/src/renderer/backend/dx/dx_buffer.h index 4c255cd..ce6d81c 100644 --- a/src/renderer/backend/dx/dx_buffer.h +++ b/src/renderer/backend/dx/dx_buffer.h @@ -8,7 +8,7 @@ template class dx_buffer : public renderer_buffer { public: - explicit dx_buffer(buffer_type in_type, const int count); + explicit dx_buffer(buffer_type in_type, int in_count); ~dx_buffer() override; @@ -37,13 +37,13 @@ private: }; template -dx_buffer::dx_buffer(buffer_type in_type, const int count): renderer_buffer(in_type, count) { +dx_buffer::dx_buffer(buffer_type in_type, const int in_count): renderer_buffer(in_type, in_count) { const auto d3d_device = aorii::get_renderer()->get_d3d_device(); const auto d3d_context = aorii::get_renderer()->get_d3d_context(); D3D11_BUFFER_DESC buffer_desc = {}; buffer_desc.Usage = D3D11_USAGE_DYNAMIC; - buffer_desc.ByteWidth = sizeof(DataType) * count; + buffer_desc.ByteWidth = sizeof(DataType) * in_count; buffer_desc.BindFlags = get_dx_buffer_type(); buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; diff --git a/src/renderer/backend/dx/dx_pipeline.cpp b/src/renderer/backend/dx/dx_pipeline.cpp index 7ef875f..5ac386d 100644 --- a/src/renderer/backend/dx/dx_pipeline.cpp +++ b/src/renderer/backend/dx/dx_pipeline.cpp @@ -32,7 +32,7 @@ void dx_pipeline::draw() { auto d3d_context = aorii::get_renderer()->get_d3d_context(); // 设置顶点缓冲区 - constexpr UINT stride = sizeof(vertex_type); + constexpr UINT stride = sizeof(aorii_vertex_type); constexpr UINT offset = 0; auto* v_buffer = static_cast(vertex_buffer->get_native_handle()); auto* c_buffer = static_cast(constant_buffer->get_native_handle()); @@ -46,18 +46,18 @@ void dx_pipeline::draw() { d3d_context->Draw(4, 0); } -void dx_pipeline::set_pixel_shader(const std::string& shader_name) { +void dx_pipeline::load_pixel_shader() { const auto d3d_device = aorii::get_renderer()->get_d3d_device(); - const auto& shader_code = load_shader(shader_name); + 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); } } -void dx_pipeline::set_vertex_shader(const std::string& shader_name) { +void dx_pipeline::load_vertex_shader() { const auto d3d_device = aorii::get_renderer()->get_d3d_device(); - const auto& shader_code = load_shader(shader_name); + const auto& shader_code = load_shader(AORII_DEFAULT_VERTEX_SHADER_NAME); auto hr = d3d_device->CreateVertexShader(shader_code.data(), shader_code.size(), nullptr, &vertex_shader); if (FAILED(hr)) { spdlog::critical("无法创建顶点着色器: {0}", hr); @@ -76,38 +76,29 @@ void dx_pipeline::set_vertex_shader(const std::string& shader_name) { void dx_pipeline::build_vertex_buffer(int vertex_count) { delete vertex_buffer; - vertex_buffer = new dx_buffer(buffer_type::vertex, vertex_count); + vertex_buffer = new dx_buffer(buffer_type::vertex, vertex_count); } void dx_pipeline::build_constant_buffer() { - constant_buffer = new dx_buffer(buffer_type::constant, 1); + constant_buffer = new dx_buffer(buffer_type::constant, 1); } -std::span dx_pipeline::lock_vertex_buffer() { - return {vertex_buffer->lock(), 4}; -} - -void dx_pipeline::unlock_vertex_buffer() { +void dx_pipeline::set_vertex(const std::span& in_vertexes) { + 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(); } -void dx_pipeline::set_projection_matrix(const Eigen::Matrix4f& matrix) { - if (!constant_buffer) { - return; - } - const auto data = constant_buffer->lock(); - data->projection_matrix = matrix; - constant_buffer->unlock(); +aorii_constant_buffer_type* dx_pipeline::lock_constant_buffer() { + return constant_buffer->lock(); } -// void dx_pipeline::set_compute_shader(const std::string& shader_name) { -// const auto d3d_device = aorii::get_renderer()->get_d3d_device(); -// const auto& shader_code = load_shader(shader_name); -// const auto hr = d3d_device->CreateComputeShader(shader_code.data(), shader_code.size(), nullptr, &compute_shader); -// if (FAILED(hr)) { -// spdlog::critical("无法创建计算着色器: {0}", hr); -// } -// } +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 cb254a0..d0d117b 100644 --- a/src/renderer/backend/dx/dx_pipeline.h +++ b/src/renderer/backend/dx/dx_pipeline.h @@ -14,24 +14,24 @@ public: void use() override; void draw() override; - void set_pixel_shader(const std::string& shader_name) override; - void set_vertex_shader(const std::string& shader_name) override; - void build_vertex_buffer(int vertex_count) override; - void build_constant_buffer() override; - // void set_compute_shader(const std::string& shader_name) override; + void load_pixel_shader(); + void load_vertex_shader(); + void build_vertex_buffer(int vertex_count); + void build_constant_buffer(); - std::span lock_vertex_buffer(); - void unlock_vertex_buffer(); + void set_vertex(const std::span& in_vertexes); - void set_projection_matrix(const Eigen::Matrix4f& matrix); + aorii_constant_buffer_type* lock_constant_buffer(); + void unlock_constant_buffer(); private: static std::vector load_shader(const std::string& shader_name); ID3D11VertexShader* vertex_shader = nullptr; ID3D11PixelShader* pixel_shader = nullptr; - // ID3D11ComputeShader* compute_shader = nullptr; + ID3D11InputLayout* input_layout = nullptr; - dx_buffer* vertex_buffer = nullptr; - dx_buffer* constant_buffer = nullptr; + dx_buffer* vertex_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 eb4d944..d590897 100644 --- a/src/renderer/backend/dx/dx_window.cpp +++ b/src/renderer/backend/dx/dx_window.cpp @@ -6,6 +6,14 @@ #include "misc/scope_exit.h" using namespace aorii; +dx_pipeline pipeline; + +void dx_pipeline_begin_frame(const Eigen::Matrix4f& projection_matrix) { + auto constant_buffer = pipeline.lock_constant_buffer(); + constant_buffer->projection_matrix = projection_matrix; + pipeline.unlock_constant_buffer(); + pipeline.use(); +} bool dx_window::create_surface(GLFWwindow* in_window) { const auto d3d_device = aorii::get_renderer()->get_d3d_device(); @@ -67,8 +75,8 @@ bool dx_window::create_surface(GLFWwindow* in_window) { return false; } - pipeline.set_pixel_shader("default_shader_pixel"); - pipeline.set_vertex_shader("default_shader_vertex"); + pipeline.load_pixel_shader(); + pipeline.load_vertex_shader(); return true; } @@ -81,25 +89,23 @@ void dx_window::begin_frame() { d3d_context->OMSetRenderTargets(1, &render_target_view, nullptr); d3d_context->ClearRenderTargetView(render_target_view, clear_color); - pipeline.use(); + dx_pipeline_begin_frame(projection_matrix); - float random_r = static_cast(rand()) / RAND_MAX; - float random_g = static_cast(rand()) / RAND_MAX; - float random_b = static_cast(rand()) / RAND_MAX; - linear_color random_color = {random_r, random_g, random_b, 1.0f}; - - const vertex_type v1 {{0.f, 0.f}, random_color }; // 左上角 - const vertex_type v2 {{100.f, 0.f}, random_color }; // 右上角 - const vertex_type v3 {{0.f, 100.f}, random_color }; // 左下角 - const vertex_type v4 {{100.f, 100.f}, random_color }; // 右下角 - const vertex_type vertices[] = { - v1, v2, v3, v4 - }; - - auto vertex_buffer = pipeline.lock_vertex_buffer(); - std::ranges::copy(vertices, vertex_buffer.begin()); - pipeline.unlock_vertex_buffer(); + { + float random_r = static_cast(rand()) / RAND_MAX; + float random_g = static_cast(rand()) / RAND_MAX; + float random_b = static_cast(rand()) / RAND_MAX; + linear_color random_color = {random_r, random_g, random_b, 1.0f}; + const aorii_vertex_type v1 {{0.f, 0.f}, random_color }; // 左上角 + const aorii_vertex_type v2 {{100.f, 0.f}, random_color }; // 右上角 + const aorii_vertex_type v3 {{0.f, 100.f}, random_color }; // 左下角 + const aorii_vertex_type v4 {{100.f, 100.f}, random_color }; // 右下角 + const std::array vertices = { + v1, v2, v3, v4 + }; + pipeline.set_vertex({vertices.data(), vertices.size()}); + } pipeline.draw(); swap_chain->Present(1, 0); @@ -156,8 +162,7 @@ HRESULT dx_window::build_render_target_view() { viewport.TopLeftY = 0.0f; d3d_context->RSSetViewports(1, &viewport); - const auto& projection_matrix = get_projection_matrix().transpose(); - pipeline.set_projection_matrix(projection_matrix); + projection_matrix = get_projection_matrix().transpose(); return hr; } diff --git a/src/renderer/backend/dx/dx_window.h b/src/renderer/backend/dx/dx_window.h index 70eda24..0b93c7b 100644 --- a/src/renderer/backend/dx/dx_window.h +++ b/src/renderer/backend/dx/dx_window.h @@ -17,6 +17,5 @@ protected: private: IDXGISwapChain1* swap_chain = nullptr; ID3D11RenderTargetView* render_target_view = nullptr; - - dx_pipeline pipeline; + Eigen::Matrix4f projection_matrix; }; diff --git a/src/renderer/core/pipeline/pipeline.h b/src/renderer/core/pipeline/pipeline.h index 0ae0530..4a0d51d 100644 --- a/src/renderer/core/pipeline/pipeline.h +++ b/src/renderer/core/pipeline/pipeline.h @@ -4,6 +4,8 @@ #include "misc/color.h" +#define AORII_DEFAULT_PIXEL_SHADER_NAME "default_shader_pixel" +#define AORII_DEFAULT_VERTEX_SHADER_NAME "default_shader_vertex" struct aorii_vertex { Eigen::Vector2f position; @@ -12,8 +14,8 @@ struct aorii_vertex { struct aorii_constant_buffer { Eigen::Matrix4f projection_matrix; }; -using vertex_type = aorii_vertex; -using constant_buffer_type = aorii_constant_buffer; +using aorii_vertex_type = aorii_vertex; +using aorii_constant_buffer_type = aorii_constant_buffer; class pipeline { public: @@ -22,10 +24,29 @@ public: virtual void use() = 0; virtual void draw() = 0; - - virtual void set_vertex_shader(const std::string& shader_name) = 0; - virtual void set_pixel_shader(const std::string& shader_name) = 0; - virtual void build_vertex_buffer(int vertex_count) = 0; - virtual void build_constant_buffer() = 0; - // virtual void set_compute_shader(const std::string& shader_name) = 0; +}; + +class custom_pipeline : public pipeline { +public: +private: + +}; + +/** + * 后期处理管线 + * 后期处理管线是一个特殊的管线,它只有一个计算着色器,并固定了输入输出 + * 输入最少有一个纹理,输出固定为一个纹理 + * 输入纹理为当前区域内的渲染结果,输出纹理为经过后期处理的渲染结果 + */ +class post_process_pipeline : public pipeline { +public: + virtual void set_compute_shader(const std::string& shader_name) = 0; + template + T* get_constant_buffer() { + return static_cast(get_constant_buffer()); + } + + virtual void* get_constant_buffer() = 0; +private: + }; diff --git a/src/renderer/core/renderer/renderer_buffer.h b/src/renderer/core/renderer/renderer_buffer.h index 71e278b..ec7c783 100644 --- a/src/renderer/core/renderer/renderer_buffer.h +++ b/src/renderer/core/renderer/renderer_buffer.h @@ -10,27 +10,27 @@ enum class buffer_type { template class renderer_buffer { public: - explicit renderer_buffer(buffer_type in_type, const int count = 1) : count(count), type(in_type) {} + explicit renderer_buffer(buffer_type in_type, const int count = 1) : size(count), type(in_type) {} virtual ~renderer_buffer() = default; virtual DataType* lock() = 0; virtual void unlock() = 0; - void resize(const int new_count) { - if (new_count == count) + void resize(const int new_size) { + if (new_size == size) return; - count = new_count; - on_resize(new_count); + size = new_size; + on_resize(new_size); } - [[nodiscard]] int get_count() const { - return count; + [[nodiscard]] int get_size() const { + return size; } [[nodiscard]] buffer_type get_type() const { return type; } [[nodiscard]] virtual void* get_native_handle() = 0; protected: - virtual void on_resize(int new_count) = 0; - int count; + virtual void on_resize(int new_size) = 0; + int size; const buffer_type type; };