命名管理,将pipeline拆分

This commit is contained in:
Nanako 2024-10-31 11:30:43 +08:00
parent 6c8db0d783
commit 804d336bca
8 changed files with 99 additions and 82 deletions

View File

@ -2,7 +2,8 @@ struct MatrixBuffer
{
matrix transform;
};
ParameterBlock<MatrixBuffer> matrix_buffer;
ParameterBlock<MatrixBuffer> matrix_buffer : register(b0);
SamplerState texture_sampler : register(s0);
struct VSInput {
float2 position : POSITION;

View File

@ -8,7 +8,7 @@
template<typename DataType>
class dx_buffer : public renderer_buffer<DataType> {
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<typename DataType>
dx_buffer<DataType>::dx_buffer(buffer_type in_type, const int count): renderer_buffer<DataType>(in_type, count) {
dx_buffer<DataType>::dx_buffer(buffer_type in_type, const int in_count): renderer_buffer<DataType>(in_type, in_count) {
const auto d3d_device = aorii::get_renderer<dx_renderer>()->get_d3d_device();
const auto d3d_context = aorii::get_renderer<dx_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;

View File

@ -32,7 +32,7 @@ void dx_pipeline::draw() {
auto d3d_context = aorii::get_renderer<dx_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<ID3D11Buffer*>(vertex_buffer->get_native_handle());
auto* c_buffer = static_cast<ID3D11Buffer*>(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<dx_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<dx_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<vertex_type>(buffer_type::vertex, vertex_count);
vertex_buffer = new dx_buffer<aorii_vertex_type>(buffer_type::vertex, vertex_count);
}
void dx_pipeline::build_constant_buffer() {
constant_buffer = new dx_buffer<constant_buffer_type>(buffer_type::constant, 1);
constant_buffer = new dx_buffer<aorii_constant_buffer_type>(buffer_type::constant, 1);
}
std::span<vertex_type> dx_pipeline::lock_vertex_buffer() {
return {vertex_buffer->lock(), 4};
void dx_pipeline::set_vertex(const std::span<const aorii_vertex_type>& in_vertexes) {
if (vertex_buffer->get_size() > in_vertexes.size()) {
vertex_buffer->resize(in_vertexes.size());
}
void dx_pipeline::unlock_vertex_buffer() {
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<dx_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<char> dx_pipeline::load_shader(const std::string& shader_name) {
auto file_pathname = aorii::get_shader_path(shader_name).generic_string() + ".dxbc";

View File

@ -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<vertex_type> lock_vertex_buffer();
void unlock_vertex_buffer();
void set_vertex(const std::span<const aorii_vertex_type>& 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<char> 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_type>* vertex_buffer = nullptr;
dx_buffer<constant_buffer_type>* constant_buffer = nullptr;
dx_buffer<aorii_vertex_type>* vertex_buffer = nullptr;
dx_buffer<aorii_constant_buffer_type>* constant_buffer = nullptr;
int vertex_count = 0;
};

View File

@ -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<dx_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<float>(rand()) / RAND_MAX;
float random_g = static_cast<float>(rand()) / RAND_MAX;
float random_b = static_cast<float>(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[] = {
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
};
auto vertex_buffer = pipeline.lock_vertex_buffer();
std::ranges::copy(vertices, vertex_buffer.begin());
pipeline.unlock_vertex_buffer();
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;
}

View File

@ -17,6 +17,5 @@ protected:
private:
IDXGISwapChain1* swap_chain = nullptr;
ID3D11RenderTargetView* render_target_view = nullptr;
dx_pipeline pipeline;
Eigen::Matrix4f projection_matrix;
};

View File

@ -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<typename T>
T* get_constant_buffer() {
return static_cast<T*>(get_constant_buffer());
}
virtual void* get_constant_buffer() = 0;
private:
};

View File

@ -10,27 +10,27 @@ enum class buffer_type {
template<typename DataType>
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;
};