dx_pipeline新增index_buffer

This commit is contained in:
Nanako 2024-10-31 14:53:38 +08:00
parent 804d336bca
commit 3b201e14d1
4 changed files with 35 additions and 28 deletions

View File

@ -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<dx_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<ID3D11Buffer*>(vertex_buffer->get_native_handle());
auto* i_buffer = static_cast<ID3D11Buffer*>(constant_buffer->get_native_handle());
auto* c_buffer = static_cast<ID3D11Buffer*>(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<dx_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<aorii_vertex_type>(buffer_type::vertex, vertex_count);
}
void dx_pipeline::build_index_buffer(int triangle_count) {
delete triangle_buffer;
triangle_buffer = new dx_buffer<aorii_triangle_type>(buffer_type::index, triangle_count);
}
void dx_pipeline::build_constant_buffer() {
constant_buffer = new dx_buffer<aorii_constant_buffer_type>(buffer_type::constant, 1);
}
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::set_triangle(const std::span<const aorii_vertex_type>& in_vertexes,
const std::span<const aorii_triangle_type>& 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<char> dx_pipeline::load_shader(const std::string& shader_name) {
auto file_pathname = aorii::get_shader_path(shader_name).generic_string() + ".dxbc";

View File

@ -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<const aorii_vertex_type>& in_vertexes);
void set_triangle(const std::span<const aorii_vertex_type>& in_vertexes, const std::span<const aorii_triangle_type>& in_triangles);
aorii_constant_buffer_type* lock_constant_buffer();
void unlock_constant_buffer();
@ -32,6 +33,7 @@ private:
ID3D11InputLayout* input_layout = nullptr;
dx_buffer<aorii_vertex_type>* vertex_buffer = nullptr;
dx_buffer<aorii_triangle_type>* triangle_buffer = nullptr;
dx_buffer<aorii_constant_buffer_type>* constant_buffer = nullptr;
int vertex_count = 0;
};

View File

@ -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();

View File

@ -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 {