使用自定义着色器绘制
This commit is contained in:
68
src/main.cpp
68
src/main.cpp
@@ -11,6 +11,9 @@
|
||||
#include "SDL3_ttf/SDL_ttf.h"
|
||||
|
||||
#include <d3d12.h>
|
||||
#include <random>
|
||||
|
||||
#include "test_layout.h"
|
||||
|
||||
void EnableD3D12DebugLayer() {
|
||||
ID3D12Debug* debugController;
|
||||
@@ -52,6 +55,28 @@ int main(int argc, char* argv[]) {
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
test_shader::vertex_t v1{};
|
||||
test_shader::vertex_t v2{};
|
||||
test_shader::vertex_t v3{};
|
||||
v1.position[0] = 0.f; v1.position[1] = 1.f; // 上
|
||||
v1.uv[0] = 0.0f; v1.uv[1] = 1.0f;
|
||||
|
||||
v2.position[0] = -1.f; v2.position[1] = -1.f; // 左
|
||||
v2.uv[0] = 1.0f; v2.uv[1] = 1.0f;
|
||||
|
||||
v3.position[0] = 1.f; v3.position[1] = -1.f; // 右
|
||||
v3.uv[0] = 0.0f; v3.uv[1] = 0.0f;
|
||||
std::array vertexs = {
|
||||
v1, v2, v3
|
||||
};
|
||||
auto vertex_buffer = test_shader::create_vertex_buffer(gpu_device, vertexs.data(), vertexs.size());
|
||||
if (!vertex_buffer) {
|
||||
SDL_Log("Failed to create vertex buffer: %s", SDL_GetError());
|
||||
shader_handle.clear(gpu_device);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TTF_Init();
|
||||
|
||||
@@ -96,8 +121,22 @@ int main(int argc, char* argv[]) {
|
||||
SDL_GetTextureSize(texture, &text_width, &text_height);
|
||||
SDL_FRect dest_rect = { 0, 0, text_width, text_height };
|
||||
|
||||
SDL_Event e;
|
||||
bool running = true;
|
||||
SDL_GPURenderStateDesc desc{};
|
||||
SDL_INIT_INTERFACE(&desc);
|
||||
desc.fragment_shader = shader_handle.get_fragment_shader();
|
||||
desc.num_sampler_bindings = 0;
|
||||
desc.num_storage_textures = 0;
|
||||
desc.num_storage_buffers = 0;
|
||||
auto render_state = SDL_CreateGPURenderState(renderer, &desc);
|
||||
|
||||
SDL_FRect shader_rect = { 0, text_height, text_width, text_height };
|
||||
|
||||
|
||||
// 生成随机数
|
||||
std::random_device rd;
|
||||
|
||||
SDL_Event e;
|
||||
bool running = true;
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_EVENT_QUIT) {
|
||||
@@ -106,11 +145,28 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
// 正确的渲染顺序
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // 黑色背景
|
||||
SDL_RenderClear(renderer); // 先清屏
|
||||
SDL_RenderTexture(renderer, texture, nullptr, &dest_rect); // 然后绘制文字
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
// 清空屏幕
|
||||
SDL_RenderClear(renderer);
|
||||
// 绘制文字
|
||||
SDL_RenderTexture(renderer, texture, nullptr, &dest_rect);
|
||||
|
||||
test_shader::ParamBuffer param_buffer{};
|
||||
// 随机颜色
|
||||
param_buffer.color[0] = static_cast<float>(rd() % 256) / 255.0f; // R
|
||||
param_buffer.color[1] = static_cast<float>(rd() % 256) / 255.0f; // G
|
||||
param_buffer.color[2] = static_cast<float>(rd() % 256) / 255.0f; // B
|
||||
param_buffer.color[3] = 1.0f; // A
|
||||
|
||||
SDL_SetGPURenderStateFragmentUniforms(render_state, 0, ¶m_buffer, sizeof(param_buffer));
|
||||
// 绘制自定义图形
|
||||
SDL_SetRenderGPUState(renderer, render_state);
|
||||
// 使用自定义着色器绘制一个面片
|
||||
SDL_RenderFillRect(renderer, &shader_rect);
|
||||
// 重置GPUState
|
||||
SDL_SetRenderGPUState(renderer, nullptr);
|
||||
|
||||
SDL_RenderPresent(renderer); // 最后呈现
|
||||
SDL_RenderLines(renderer, nullptr, 0); // 确保没有多余的线条
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(16)); // ~60 FPS
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user