From e6a7254598b10dbff19693c57e28b88b9e9bd6a6 Mon Sep 17 00:00:00 2001 From: Nanako <469449812@qq.com> Date: Mon, 23 Dec 2024 21:01:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=8D=A2=E8=A1=8C=E6=83=85?= =?UTF-8?q?=E5=86=B5=EF=BC=8C=E4=BF=AE=E5=A4=8D=E7=BB=98=E5=88=B6=E4=B8=89?= =?UTF-8?q?=E8=A7=92=E5=BD=A2=E6=97=B6=E6=B2=A1=E6=9C=89=E6=8C=89=E7=85=A7?= =?UTF-8?q?=E9=A1=B6=E7=82=B9=E7=B4=A2=E5=BC=95=E7=BB=98=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/backend/dx/dx_window.cpp | 2 +- .../backend/dx/pipeline/dx_rect_pipeline.cpp | 6 +-- .../backend/dx/pipeline/dx_rect_pipeline.h | 2 +- .../dx/pipeline/dx_rounded_rect_pipeline.cpp | 6 +-- .../dx/pipeline/dx_rounded_rect_pipeline.h | 2 +- .../dx/pipeline/dx_sdf_text_pipeline.cpp | 6 +-- .../dx/pipeline/dx_sdf_text_pipeline.h | 2 +- .../dx/pipeline/dx_segment_pipeline.cpp | 6 +-- .../backend/dx/pipeline/dx_segment_pipeline.h | 2 +- .../dx/pipeline/dx_texture_pipeline.cpp | 6 +-- .../backend/dx/pipeline/dx_texture_pipeline.h | 2 +- src/renderer/core/pipeline/pipeline.h | 2 +- .../core/renderer/renderer_context.cpp | 41 ++++++++++--------- src/renderer/core/renderer/renderer_context.h | 2 +- src/renderer/shader/aorii_sdf_text.slang | 10 +++-- 15 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/renderer/backend/dx/dx_window.cpp b/src/renderer/backend/dx/dx_window.cpp index f58c54b..ed691df 100644 --- a/src/renderer/backend/dx/dx_window.cpp +++ b/src/renderer/backend/dx/dx_window.cpp @@ -112,7 +112,7 @@ void dx_window::begin_frame() { // context.draw_line( { 600, 600 }, { mouse_x, mouse_y }, { 1, 0, 1, 1 }, thickness); // if (test_texture) context.draw_texture({ 0.f, 0.f }, test_texture->size().cast(), test_texture); - context.draw_string({0, 0}, L"你好,世界!", 32); + context.draw_string({0, 0}, L"你好,世界!全是水群大师\n测试换行", 32); context.flush(); diff --git a/src/renderer/backend/dx/pipeline/dx_rect_pipeline.cpp b/src/renderer/backend/dx/pipeline/dx_rect_pipeline.cpp index 2f37502..756e5f2 100644 --- a/src/renderer/backend/dx/pipeline/dx_rect_pipeline.cpp +++ b/src/renderer/backend/dx/pipeline/dx_rect_pipeline.cpp @@ -12,7 +12,7 @@ void dx_rect_pipeline::use() { loader.use(); } -void dx_rect_pipeline::draw() { +void dx_rect_pipeline::draw(uint32_t in_index_count) { const auto d3d_context = aorii::get_renderer()->get_d3d_context(); // 设置顶点缓冲区 @@ -26,8 +26,8 @@ void dx_rect_pipeline::draw() { d3d_context->VSSetConstantBuffers(0, 1, &c_buffer); // 设置图元拓扑 - d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 绘制矩形 - d3d_context->Draw(vertex_buffer->get_size(), 0); + d3d_context->DrawIndexed(in_index_count, 0, 0); } diff --git a/src/renderer/backend/dx/pipeline/dx_rect_pipeline.h b/src/renderer/backend/dx/pipeline/dx_rect_pipeline.h index 75ce40f..d19717f 100644 --- a/src/renderer/backend/dx/pipeline/dx_rect_pipeline.h +++ b/src/renderer/backend/dx/pipeline/dx_rect_pipeline.h @@ -8,7 +8,7 @@ class dx_rect_pipeline : public rect_pipeline { public: bool init() override; void use() override; - void draw() override; + void draw(uint32_t in_index_count) override; private: dx_pipeline_loader loader; }; diff --git a/src/renderer/backend/dx/pipeline/dx_rounded_rect_pipeline.cpp b/src/renderer/backend/dx/pipeline/dx_rounded_rect_pipeline.cpp index 99d405c..da8c03a 100644 --- a/src/renderer/backend/dx/pipeline/dx_rounded_rect_pipeline.cpp +++ b/src/renderer/backend/dx/pipeline/dx_rounded_rect_pipeline.cpp @@ -11,7 +11,7 @@ void dx_rounded_rect_pipeline::use() { loader.use(); } -void dx_rounded_rect_pipeline::draw() { +void dx_rounded_rect_pipeline::draw(uint32_t in_index_count) { const auto d3d_context = aorii::get_renderer()->get_d3d_context(); // 设置顶点缓冲区 @@ -26,8 +26,8 @@ void dx_rounded_rect_pipeline::draw() { d3d_context->PSSetConstantBuffers(0, 1, &c_buffer); // 设置图元拓扑 - d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 绘制矩形 - d3d_context->Draw(vertex_buffer->get_size(), 0); + d3d_context->DrawIndexed(in_index_count, 0, 0); } diff --git a/src/renderer/backend/dx/pipeline/dx_rounded_rect_pipeline.h b/src/renderer/backend/dx/pipeline/dx_rounded_rect_pipeline.h index 593771a..a8a528e 100644 --- a/src/renderer/backend/dx/pipeline/dx_rounded_rect_pipeline.h +++ b/src/renderer/backend/dx/pipeline/dx_rounded_rect_pipeline.h @@ -8,7 +8,7 @@ class dx_rounded_rect_pipeline : public rounded_rect_pipeline { public: bool init() override; void use() override; - void draw() override; + void draw(uint32_t in_index_count) override; private: dx_pipeline_loader loader; }; diff --git a/src/renderer/backend/dx/pipeline/dx_sdf_text_pipeline.cpp b/src/renderer/backend/dx/pipeline/dx_sdf_text_pipeline.cpp index d6d7bdf..0fa59d6 100644 --- a/src/renderer/backend/dx/pipeline/dx_sdf_text_pipeline.cpp +++ b/src/renderer/backend/dx/pipeline/dx_sdf_text_pipeline.cpp @@ -26,7 +26,7 @@ void dx_sdf_text_pipeline::use() { loader.use(); } -void dx_sdf_text_pipeline::draw() { +void dx_sdf_text_pipeline::draw(uint32_t in_index_count) { if (!glyph_texture) return; const auto d3d_context = aorii::get_renderer()->get_d3d_context(); @@ -49,8 +49,8 @@ void dx_sdf_text_pipeline::draw() { d3d_context->PSSetShaderResources(0, 1, &srv); // 设置图元拓扑 - d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 绘制矩形 - d3d_context->Draw(vertex_buffer->get_size(), 0); + d3d_context->DrawIndexed(in_index_count, 0, 0); } diff --git a/src/renderer/backend/dx/pipeline/dx_sdf_text_pipeline.h b/src/renderer/backend/dx/pipeline/dx_sdf_text_pipeline.h index 3c49269..ec27d83 100644 --- a/src/renderer/backend/dx/pipeline/dx_sdf_text_pipeline.h +++ b/src/renderer/backend/dx/pipeline/dx_sdf_text_pipeline.h @@ -8,7 +8,7 @@ public: bool init() override; void use() override; - void draw() override; + void draw(uint32_t in_index_count) override; private: dx_pipeline_loader loader; diff --git a/src/renderer/backend/dx/pipeline/dx_segment_pipeline.cpp b/src/renderer/backend/dx/pipeline/dx_segment_pipeline.cpp index ead4e21..212c21f 100644 --- a/src/renderer/backend/dx/pipeline/dx_segment_pipeline.cpp +++ b/src/renderer/backend/dx/pipeline/dx_segment_pipeline.cpp @@ -12,7 +12,7 @@ void dx_segment_pipeline::use() { loader.use(); } -void dx_segment_pipeline::draw() { +void dx_segment_pipeline::draw(uint32_t in_index_count) { const auto d3d_context = aorii::get_renderer()->get_d3d_context(); // 设置顶点缓冲区 @@ -27,8 +27,8 @@ void dx_segment_pipeline::draw() { d3d_context->PSSetConstantBuffers(0, 1, &c_buffer); // 设置图元拓扑 - d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 绘制矩形 - d3d_context->Draw(vertex_buffer->get_size(), 0); + d3d_context->DrawIndexed(in_index_count, 0, 0); } diff --git a/src/renderer/backend/dx/pipeline/dx_segment_pipeline.h b/src/renderer/backend/dx/pipeline/dx_segment_pipeline.h index ae2f659..9b7dc4b 100644 --- a/src/renderer/backend/dx/pipeline/dx_segment_pipeline.h +++ b/src/renderer/backend/dx/pipeline/dx_segment_pipeline.h @@ -6,7 +6,7 @@ class dx_segment_pipeline : public segment_pipeline { public: bool init() override; void use() override; - void draw() override; + void draw(uint32_t in_index_count) override; private: dx_pipeline_loader loader; }; diff --git a/src/renderer/backend/dx/pipeline/dx_texture_pipeline.cpp b/src/renderer/backend/dx/pipeline/dx_texture_pipeline.cpp index 9e50af7..da74acc 100644 --- a/src/renderer/backend/dx/pipeline/dx_texture_pipeline.cpp +++ b/src/renderer/backend/dx/pipeline/dx_texture_pipeline.cpp @@ -24,7 +24,7 @@ void dx_texture_pipeline::use() { loader.use(); } -void dx_texture_pipeline::draw() { +void dx_texture_pipeline::draw(uint32_t in_index_count) { if (!texture) return; const auto d3d_context = aorii::get_renderer()->get_d3d_context(); @@ -42,8 +42,8 @@ void dx_texture_pipeline::draw() { d3d_context->PSSetShaderResources(0, 1, &srv); // 设置图元拓扑 - d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 绘制矩形 - d3d_context->Draw(vertex_buffer->get_size(), 0); + d3d_context->DrawIndexed(in_index_count, 0, 0); } diff --git a/src/renderer/backend/dx/pipeline/dx_texture_pipeline.h b/src/renderer/backend/dx/pipeline/dx_texture_pipeline.h index 16763b2..d32ac05 100644 --- a/src/renderer/backend/dx/pipeline/dx_texture_pipeline.h +++ b/src/renderer/backend/dx/pipeline/dx_texture_pipeline.h @@ -8,7 +8,7 @@ class dx_texture_pipeline : public texture_pipeline { public: bool init() override; void use() override; - void draw() override; + void draw(uint32_t in_index_count) override; private: dx_pipeline_loader loader; diff --git a/src/renderer/core/pipeline/pipeline.h b/src/renderer/core/pipeline/pipeline.h index fc6a47f..f6420e1 100644 --- a/src/renderer/core/pipeline/pipeline.h +++ b/src/renderer/core/pipeline/pipeline.h @@ -45,7 +45,7 @@ public: virtual void destroy(); virtual void use() = 0; - virtual void draw() = 0; + virtual void draw(uint32_t in_index_count) = 0; virtual void set_triangle(const std::span& in_vertexes, const std::span& in_triangles); protected: diff --git a/src/renderer/core/renderer/renderer_context.cpp b/src/renderer/core/renderer/renderer_context.cpp index e8ad176..d521aa3 100644 --- a/src/renderer/core/renderer/renderer_context.cpp +++ b/src/renderer/core/renderer/renderer_context.cpp @@ -7,8 +7,8 @@ aorii_text* text = nullptr; void renderer_context::init() { text = new aorii_text(2048); - text->initialize(LR"(C:\Windows\Fonts\msyh.ttc)", 32); - text->precache_common_characters(); + text->initialize(LR"(C:\Windows\Fonts\msyh.ttc)", 64); + // text->precache_common_characters(); } void renderer_context::draw_rectangle(const Eigen::Vector2f& in_pos, const Eigen::Vector2f& in_size, const linear_color& in_color) { @@ -43,9 +43,15 @@ void renderer_context::draw_string(const Eigen::Vector2f& in_pos, const std::wst to_text_pipeline(in_color); float cursor_x = in_pos.x(); + float cursor_y = in_pos.y(); const float scale = in_height / text->get_font_pixel_size(); wchar_t last_char = 0; for (const auto c : in_str) { + if (c == '\n') { + cursor_x = in_pos.x(); + cursor_y += text->get_ascent() * scale; + continue; + } cursor_x += last_char ? text->get_kerning(last_char, c) : 0; character_info info{}; @@ -54,7 +60,7 @@ void renderer_context::draw_string(const Eigen::Vector2f& in_pos, const std::wst // 根据in_height缩放字符大小 const float y_offset = (info.y_offset + text->get_ascent()) * scale; const Eigen::Vector2f size { info.width * scale, info.height * scale }; - const Eigen::Vector2f pos { cursor_x + info.x_offset * scale, in_pos.y() + y_offset }; + const Eigen::Vector2f pos { cursor_x + info.x_offset * scale, cursor_y + y_offset }; cursor_x += info.advance * scale; aorii_vertex_param param{}; @@ -145,31 +151,28 @@ void renderer_context::make_rect(const Eigen::Vector2f& in_pos, const Eigen::Vec aorii_vertex v4{ { in_pos.x() + in_size.x(), in_pos.y() + in_size.y() }, { 1, 1 }, in_color, in_param }; // 右下角 if (in_angle != 0) { - auto center = in_pos + in_size / 2; + auto center = in_pos + in_size * 0.5f; const Eigen::Affine2f transform = - Eigen::Translation(center) * // 平移到旋转中心 - Eigen::Rotation2D(in_angle) * // 旋转 - Eigen::Translation(-center); // 平移回原点 + Eigen::Translation2f(center) * // 平移到旋转中心 + Eigen::Rotation2Df(in_angle) * // 旋转 + Eigen::Translation2f(-center); // 平移回原点 - v1.position = transform * v1.position; - v2.position = transform * v2.position; - v3.position = transform * v3.position; - v4.position = transform * v4.position; + v1.position = transform * v1.position.homogeneous(); + v2.position = transform * v2.position.homogeneous(); + v3.position = transform * v3.position.homogeneous(); + v4.position = transform * v4.position.homogeneous(); } - const uint32_t index1 = vertices.size(); + const uint32_t base_index = vertices.size(); vertices.push_back(v1); - const uint32_t index2 = vertices.size(); vertices.push_back(v2); - const uint32_t index3 = vertices.size(); vertices.push_back(v3); - const uint32_t index4 = vertices.size(); + // vertices.push_back(v3); + // vertices.push_back(v2); vertices.push_back(v4); - const aorii_triangle t1 = { index1, index2, index3 }; - const aorii_triangle t2 = { index2, index3, index4 }; - triangles.push_back(t1); - triangles.push_back(t2); + triangles.push_back(aorii_triangle{ base_index + 0, base_index + 1, base_index + 2 }); + triangles.push_back(aorii_triangle{ base_index + 2, base_index + 1, base_index + 3 }); } void renderer_context::rotate_vertex(const float in_angle, const Eigen::Vector2f& in_center) { diff --git a/src/renderer/core/renderer/renderer_context.h b/src/renderer/core/renderer/renderer_context.h index e99c25a..036e4dc 100644 --- a/src/renderer/core/renderer/renderer_context.h +++ b/src/renderer/core/renderer/renderer_context.h @@ -77,7 +77,7 @@ public: if (vertices.empty() || triangles.empty()) return; if (current_pipeline) { current_pipeline->set_triangle(vertices, triangles); - current_pipeline->draw(); + current_pipeline->draw(triangles.size() * 3); } clear(); } diff --git a/src/renderer/shader/aorii_sdf_text.slang b/src/renderer/shader/aorii_sdf_text.slang index d2c0f31..8eba14e 100644 --- a/src/renderer/shader/aorii_sdf_text.slang +++ b/src/renderer/shader/aorii_sdf_text.slang @@ -23,13 +23,16 @@ struct Constants { ParameterBlock param_buffer : register(b0); PSInput vertex_main(VSInput input) { + float2 altas_uv = input.param_a.xy; + float2 char_size = input.param_b.xy; + PSInput output; output.position = mul(float4(input.position, 0.0f, 1.0f), param_buffer.transform); output.uv = input.uv; output.color = input.color; - output.altas_uv = float2(input.param_a.x, input.param_a.y); + output.altas_uv = altas_uv; output.altas_index = input.param_a.z; - output.char_size = float2(input.param_b.x, input.param_b.y); + output.char_size = char_size; return output; } @@ -40,7 +43,8 @@ float4 pixel_main(PSInput input) : SV_Target { float2 uv = input.altas_uv + input.char_size * input.uv; float distance = atlas_texture.Sample(sampler_state, float3(uv, input.altas_index)).r; // return float4(distance, distance, distance, 1.0); - float alpha = smoothstep(0.47, 0.53, distance); + float range = 0.07; + float alpha = smoothstep(0.5 - range, 0.5 + range, distance); float4 color = input.color; color.a *= alpha;