DX11着色器OK,DX12待修复

This commit is contained in:
daiqingshuang
2025-06-10 18:24:07 +08:00
parent 398ac7af53
commit 2c648fee11
9 changed files with 585 additions and 204 deletions

View File

@@ -33,4 +33,4 @@ SET(SRC_FILES)
retrieve_files(${CMAKE_CURRENT_SOURCE_DIR}/src SRC_FILES)
add_executable(learn ${SRC_FILES})
target_include_directories(learn PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(learn SDL3-static SDL3_ttf-static)
target_link_libraries(learn SDL3-static SDL3_ttf-static d3d12)

View File

@@ -10,7 +10,26 @@
#include "SDL3/SDL_gpu.h"
#include "SDL3_ttf/SDL_ttf.h"
#include <d3d12.h>
void EnableD3D12DebugLayer() {
ID3D12Debug* debugController;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) {
debugController->EnableDebugLayer();
// 启用 GPU 验证(可选,但有助于发现更多问题)
ID3D12Debug1* debugController1;
if (SUCCEEDED(debugController->QueryInterface(IID_PPV_ARGS(&debugController1)))) {
debugController1->SetEnableGPUBasedValidation(TRUE);
debugController1->Release();
}
debugController->Release();
}
}
int main(int argc, char* argv[]) {
EnableD3D12DebugLayer();
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Hello World", 640, 480, 0);
if (window == nullptr) {
@@ -27,6 +46,12 @@ int main(int argc, char* argv[]) {
SDL3GPU::test_shader_handle_t shader_handle;
auto result = shader_handle.init(gpu_device);
if (!result) {
SDL_Log("Failed to initialize shader handle: %s", result.error().c_str());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
return 1;
}
TTF_Init();
@@ -71,23 +96,6 @@ int main(int argc, char* argv[]) {
SDL_GetTextureSize(texture, &text_width, &text_height);
SDL_FRect dest_rect = { 0, 0, text_width, text_height };
SDL_GPUShaderCreateInfo shader_info;
shader_info.code_size = 10;
shader_info.code = nullptr; // 这里需要填入实际的着色器代码
shader_info.entrypoint = "main";
shader_info.format = SDL_GPU_SHADERFORMAT_DXIL;
shader_info.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
shader_info.num_samplers = 0;
shader_info.num_storage_textures = 0;
shader_info.num_uniform_buffers = 0;
shader_info.num_storage_buffers = 0;
auto shader = SDL_CreateGPUShader(gpu_device, &shader_info);
SDL_GPURenderStateDesc desc;
SDL_INIT_INTERFACE(&desc);
desc.fragment_shader = shader;
auto render_state = SDL_CreateGPURenderState(renderer, &desc);
SDL_Event e;
bool running = true;
while (running) {

View File

@@ -3,18 +3,19 @@
//
#include "shader_handle.h"
#include "tmpxm05mgxl.layout.h"
#include "test_layout.h"
SDL_GPUGraphicsPipeline* pixel_shader_handle_t::create_graphics_pipeline(SDL_GPUDevice* in_gpu_device) {
SDL_GPUGraphicsPipelineCreateInfo desc = {};
desc.vertex_shader = vertex_shader_;
desc.fragment_shader = fragment_shader_;
desc.vertex_input_state = g_vertexInputState;
desc.primitive_type = primitive_type_;
desc.rasterizer_state.fill_mode = fill_mode_;
desc.rasterizer_state.cull_mode = cull_mode_;
desc.rasterizer_state.front_face = front_face_;
desc.multisample_state.sample_count = sample_count_;
SDL_GPUGraphicsPipelineCreateInfo desc = {};
desc.vertex_shader = vertex_shader_;
desc.fragment_shader = fragment_shader_;
desc.vertex_input_state = test_shader::vertex_input_state;
desc.primitive_type = primitive_type_;
desc.rasterizer_state.fill_mode = fill_mode_;
desc.rasterizer_state.cull_mode = cull_mode_;
desc.rasterizer_state.front_face = front_face_;
desc.multisample_state.sample_count = sample_count_;
desc.depth_stencil_state.enable_depth_test = false;
return SDL_CreateGPUGraphicsPipeline(in_gpu_device, &desc);

View File

@@ -31,7 +31,8 @@ public:
graphics_pipeline_ = create_graphics_pipeline(in_gpu_device);
if (!graphics_pipeline_) {
clear(in_gpu_device);
return std::unexpected("Failed to create graphics pipeline");
const std::string& msg = "Failed to create graphics pipeline: ";
return std::unexpected(msg + SDL_GetError());
}
return true;
}

File diff suppressed because one or more lines are too long

View File

@@ -7,17 +7,17 @@
// Auto-generated vertex structure
namespace test_shader {
struct vertex_t {
// location: 0, semantic: POSITION, offset: 0, size: 8 bytes
// location: 0, semantic: TEXCOORD, offset: 0, size: 8 bytes
float position[2];
// location: 1, semantic: TEXCOORD, offset: 8, size: 8 bytes
// location: 1, semantic: TEXCOORD1, offset: 8, size: 8 bytes
float uv[2];
// location: 2, semantic: COLOR, offset: 16, size: 16 bytes
// location: 2, semantic: TEXCOORD2, offset: 16, size: 16 bytes
float color[4];
// location: 3, semantic: TEXCOORD1, offset: 32, size: 16 bytes
// location: 3, semantic: TEXCOORD3, offset: 32, size: 16 bytes
float param_a[4];
// location: 4, semantic: TEXCOORD2, offset: 48, size: 16 bytes
// location: 4, semantic: TEXCOORD4, offset: 48, size: 16 bytes
float param_b[4];
// location: 5, semantic: TEXCOORD3, offset: 64, size: 16 bytes
// location: 5, semantic: TEXCOORD5, offset: 64, size: 16 bytes
float param_c[4];
};
@@ -25,6 +25,11 @@ namespace test_shader {
static_assert(sizeof(vertex_t) == 80, "Vertex struct size mismatch");
// Uniform buffer structures
typedef struct ParamBuffer {
float transform[4][4]; // offset: 0, size: 64
} ParamBuffer;
// Binding: 0, Size: 64 bytes (aligned: 64)
// Vertex attribute descriptions
static constexpr uint32_t VERTEX_ATTRIBUTE_COUNT = 6;

View File

@@ -1,126 +0,0 @@
#pragma once
#include <SDL3/SDL.h>
#include <SDL3/SDL_gpu.h>
// Auto-generated from: C:\Users\46944\AppData\Local\Temp\tmpufk9_wv_.json
// Auto-generated vertex structure
typedef struct Vertex {
float position[2];
// location: 0, semantic: POSITION
float uv[2];
// location: 1, semantic: TEXCOORD
float color[4];
// location: 2, semantic: COLOR
float param_a[4];
// location: 3, semantic: TEXCOORD1
float param_b[4];
// location: 4, semantic: TEXCOORD2
float param_c[4];
// location: 5, semantic: TEXCOORD3
} Vertex;
// Uniform buffer structures
typedef struct ParamBuffer {
float transform[4][4];
} ParamBuffer;
// Binding: 0, Size: 64 bytes
// Vertex attribute descriptions
#define VERTEX_ATTRIBUTE_COUNT 6
static const SDL_GPUVertexAttribute g_vertexAttributes[] = {
{
.location = 0,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
.offset = 0
}, // position
{
.location = 1,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
.offset = 8
}, // uv
{
.location = 2,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.offset = 16
}, // color
{
.location = 3,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.offset = 32
}, // param_a
{
.location = 4,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.offset = 48
}, // param_b
{
.location = 5,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.offset = 64
} // param_c
};
// Vertex buffer description
static const SDL_GPUVertexBufferDescription g_vertexBufferDesc = {
.slot = 0,
.pitch = 80, // sizeof(Vertex)
.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX,
.instance_step_rate = 0
};
// Vertex input state
static const SDL_GPUVertexInputState g_vertexInputState = {
.vertex_buffer_descriptions = &g_vertexBufferDesc,
.num_vertex_buffers = 1,
.vertex_attributes = g_vertexAttributes,
.num_vertex_attributes = VERTEX_ATTRIBUTE_COUNT
};
// Helper functions
static SDL_GPUBuffer* createVertexBuffer(SDL_GPUDevice* device,
const Vertex* vertices,
Uint32 vertexCount) {
SDL_GPUBufferCreateInfo bufferInfo = {
.usage = SDL_GPU_BUFFERUSAGE_VERTEX,
.size = static_cast<Uint32>(sizeof(Vertex)) * vertexCount
};
SDL_GPUBuffer* buffer = SDL_CreateGPUBuffer(device, &bufferInfo);
SDL_GPUTransferBufferCreateInfo transferInfo = {
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = bufferInfo.size
};
// Upload vertex data
SDL_GPUTransferBuffer* transfer = SDL_CreateGPUTransferBuffer(device, &transferInfo);
void* mapped = SDL_MapGPUTransferBuffer(device, transfer, false);
SDL_memcpy(mapped, vertices, bufferInfo.size);
SDL_UnmapGPUTransferBuffer(device, transfer);
// Copy to GPU
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(device);
SDL_GPUCopyPass* copy = SDL_BeginGPUCopyPass(cmd);
SDL_GPUTransferBufferLocation src = {.transfer_buffer = transfer, .offset = 0};
SDL_GPUBufferRegion dst = {.buffer = buffer, .offset = 0, .size = bufferInfo.size};
SDL_UploadToGPUBuffer(copy, &src, &dst, false);
SDL_EndGPUCopyPass(copy);
SDL_SubmitGPUCommandBuffer(cmd);
SDL_ReleaseGPUTransferBuffer(device, transfer);
return buffer;
}