diff --git a/Makefile.am b/Makefile.am index c5838e6f..2c36d703 100644 --- a/Makefile.am +++ b/Makefile.am @@ -87,6 +87,7 @@ vkd3d_shader_tests = \ tests/hlsl/cross.shader_test \ tests/hlsl/d3dcolor-to-ubyte4.shader_test \ tests/hlsl/ddxddy.shader_test \ + tests/hlsl/depth-out.shader_test \ tests/hlsl/determinant.shader_test \ tests/hlsl/discard.shader_test \ tests/hlsl/distance.shader_test \ diff --git a/tests/d3d12_test_utils.h b/tests/d3d12_test_utils.h index 77b9d42e..e24b3fa5 100644 --- a/tests/d3d12_test_utils.h +++ b/tests/d3d12_test_utils.h @@ -1249,6 +1249,12 @@ static inline D3D12_CPU_DESCRIPTOR_HANDLE get_cpu_rtv_handle(struct test_context return get_cpu_handle(context->device, heap, D3D12_DESCRIPTOR_HEAP_TYPE_RTV, offset); } +static inline D3D12_CPU_DESCRIPTOR_HANDLE get_cpu_dsv_handle(struct test_context *context, + ID3D12DescriptorHeap *heap, unsigned int offset) +{ + return get_cpu_handle(context->device, heap, D3D12_DESCRIPTOR_HEAP_TYPE_DSV, offset); +} + static inline D3D12_GPU_DESCRIPTOR_HANDLE get_gpu_descriptor_handle(struct test_context *context, ID3D12DescriptorHeap *heap, unsigned int offset) { diff --git a/tests/hlsl/depth-out.shader_test b/tests/hlsl/depth-out.shader_test new file mode 100644 index 00000000..7acd05b4 --- /dev/null +++ b/tests/hlsl/depth-out.shader_test @@ -0,0 +1,35 @@ +[require] +shader model >= 4.0 + +[dsv] +size (2d, 640, 480) + +[pixel shader] +float depth; + +float main() : SV_Depth +{ + return depth; +} + +[test] +uniform 0 float 0.0 +clear dsv 1.0 +depth less +todo(sm>=6 | glsl) draw quad +probe dsv all r (0.0) + +uniform 0 float 0.75 +clear dsv 1.0 +todo(sm>=6 | glsl) draw quad +probe dsv all r (0.75) + +clear dsv 0.5 +depth greater +todo(sm>=6 | glsl) draw quad +probe dsv all r (0.75) + +depth less +clear dsv 0.5 +todo(sm>=6 | glsl) draw quad +probe dsv all r (0.5) diff --git a/tests/shader_runner.c b/tests/shader_runner.c index 2dc0b0df..98e87115 100644 --- a/tests/shader_runner.c +++ b/tests/shader_runner.c @@ -385,6 +385,35 @@ static DXGI_FORMAT parse_format(const char *line, enum texture_data_type *data_t fatal_error("Unknown format '%s'.\n", line); } +static D3D12_COMPARISON_FUNC parse_comparison_func(const char *line) +{ + static const struct + { + const char *string; + D3D12_COMPARISON_FUNC func; + } + funcs[] = + { + {"less equal", D3D12_COMPARISON_FUNC_LESS_EQUAL}, + {"not equal", D3D12_COMPARISON_FUNC_NOT_EQUAL}, + {"greater equal", D3D12_COMPARISON_FUNC_GREATER_EQUAL}, + {"never", D3D12_COMPARISON_FUNC_NEVER}, + {"less", D3D12_COMPARISON_FUNC_LESS}, + {"equal", D3D12_COMPARISON_FUNC_EQUAL}, + {"greater", D3D12_COMPARISON_FUNC_GREATER}, + {"always", D3D12_COMPARISON_FUNC_ALWAYS}, + }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(funcs); ++i) + { + if (match_string(line, funcs[i].string, &line)) + return funcs[i].func; + } + + fatal_error("Unknown comparison func '%s'.\n", line); +} + static D3D12_TEXTURE_ADDRESS_MODE parse_sampler_address_mode(const char *line, const char **rest) { if (match_string(line, "border", rest)) @@ -444,35 +473,9 @@ static void parse_sampler_directive(struct sampler *sampler, const char *line) } else if (match_string(line, "comparison", &line)) { - static const struct - { - const char *string; - D3D12_COMPARISON_FUNC func; - } - funcs[] = - { - {"less equal", D3D12_COMPARISON_FUNC_LESS_EQUAL}, - {"not equal", D3D12_COMPARISON_FUNC_NOT_EQUAL}, - {"greater equal", D3D12_COMPARISON_FUNC_GREATER_EQUAL}, - {"never", D3D12_COMPARISON_FUNC_NEVER}, - {"less", D3D12_COMPARISON_FUNC_LESS}, - {"equal", D3D12_COMPARISON_FUNC_EQUAL}, - {"greater", D3D12_COMPARISON_FUNC_GREATER}, - {"always", D3D12_COMPARISON_FUNC_ALWAYS}, - }; - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(funcs); ++i) - { - if (match_string(line, funcs[i].string, &line)) - { - sampler->filter |= D3D12_FILTER_REDUCTION_TYPE_COMPARISON << D3D12_FILTER_REDUCTION_TYPE_SHIFT; - sampler->func = funcs[i].func; - return; - } - } - - fatal_error("Unknown sampler func '%s'.\n", line); + sampler->filter |= D3D12_FILTER_REDUCTION_TYPE_COMPARISON << D3D12_FILTER_REDUCTION_TYPE_SHIFT; + sampler->func = parse_comparison_func(line); + return; } else { @@ -627,6 +630,12 @@ struct resource *shader_runner_get_resource(struct shader_runner *runner, enum r return NULL; } +static bool shader_runner_has_target(struct shader_runner *runner) +{ + return shader_runner_get_resource(runner, RESOURCE_TYPE_RENDER_TARGET, 0) + || shader_runner_get_resource(runner, RESOURCE_TYPE_DEPTH_STENCIL, 0); +} + static void set_resource(struct shader_runner *runner, const struct resource_params *params) { struct resource *resource; @@ -825,6 +834,22 @@ static void parse_test_directive(struct shader_runner *runner, const char *line) runner->last_render_failed = !runner->ops->dispatch(runner, x, y, z); } + else if (match_string(line, "clear dsv", &line)) + { + struct resource *resource; + struct vec4 v = {0}; + + if (!sscanf(line, "%f", &v.x)) + fatal_error("Malformed dsv clear arguments '%s'.\n", line); + + if (!(resource = shader_runner_get_resource(runner, RESOURCE_TYPE_DEPTH_STENCIL, 0))) + fatal_error("Resource not found.\n"); + runner->ops->clear(runner, resource, &v); + } + else if (match_string(line, "depth", &line)) + { + runner->depth_func = parse_comparison_func(line); + } else if (match_string(line, "draw quad", &line)) { struct resource_params params; @@ -848,7 +873,7 @@ static void parse_test_directive(struct shader_runner *runner, const char *line) if (!runner->hs_source != !runner->ds_source) fatal_error("Have a domain or hull shader but not both.\n"); - if (!shader_runner_get_resource(runner, RESOURCE_TYPE_RENDER_TARGET, 0)) + if (!shader_runner_has_target(runner)) { memset(¶ms, 0, sizeof(params)); params.slot = 0; @@ -901,7 +926,7 @@ static void parse_test_directive(struct shader_runner *runner, const char *line) if (!runner->hs_source != !runner->ds_source) fatal_error("Have a domain or hull shader but not both.\n"); - if (!shader_runner_get_resource(runner, RESOURCE_TYPE_RENDER_TARGET, 0)) + if (!shader_runner_has_target(runner)) { memset(¶ms, 0, sizeof(params)); params.slot = 0; @@ -970,6 +995,10 @@ static void parse_test_directive(struct shader_runner *runner, const char *line) resource = shader_runner_get_resource(runner, RESOURCE_TYPE_RENDER_TARGET, slot); } + else if (match_string(line, "dsv", &line)) + { + resource = shader_runner_get_resource(runner, RESOURCE_TYPE_DEPTH_STENCIL, 0); + } else { resource = shader_runner_get_resource(runner, RESOURCE_TYPE_RENDER_TARGET, 0); @@ -1813,6 +1842,20 @@ void run_shader_tests(struct shader_runner *runner, const struct shader_runner_c current_resource.texel_size = 16; current_resource.level_count = 1; } + else if (!strcmp(line, "[dsv]\n")) + { + state = STATE_RESOURCE; + + memset(¤t_resource, 0, sizeof(current_resource)); + + current_resource.slot = 0; + current_resource.type = RESOURCE_TYPE_DEPTH_STENCIL; + current_resource.format = DXGI_FORMAT_D32_FLOAT; + current_resource.is_shadow = true; + current_resource.data_type = TEXTURE_DATA_FLOAT; + current_resource.texel_size = 4; + current_resource.level_count = 1; + } else if (sscanf(line, "[srv %u]\n", &index)) { state = STATE_RESOURCE; diff --git a/tests/shader_runner.h b/tests/shader_runner.h index 73cec54d..ef0b753b 100644 --- a/tests/shader_runner.h +++ b/tests/shader_runner.h @@ -71,6 +71,7 @@ struct sampler enum resource_type { RESOURCE_TYPE_RENDER_TARGET, + RESOURCE_TYPE_DEPTH_STENCIL, RESOURCE_TYPE_TEXTURE, RESOURCE_TYPE_UAV, RESOURCE_TYPE_VERTEX_BUFFER, @@ -186,12 +187,15 @@ struct shader_runner size_t input_element_count, input_element_capacity; unsigned int compile_options; + + D3D12_COMPARISON_FUNC depth_func; }; struct shader_runner_ops { struct resource *(*create_resource)(struct shader_runner *runner, const struct resource_params *params); void (*destroy_resource)(struct shader_runner *runner, struct resource *resource); + void (*clear)(struct shader_runner *runner, struct resource *resource, const struct vec4 *clear_value); bool (*draw)(struct shader_runner *runner, D3D_PRIMITIVE_TOPOLOGY primitive_topology, unsigned int vertex_count); bool (*dispatch)(struct shader_runner *runner, unsigned int x, unsigned int y, unsigned int z); struct resource_readback *(*get_resource_readback)(struct shader_runner *runner, struct resource *resource); diff --git a/tests/shader_runner_d3d11.c b/tests/shader_runner_d3d11.c index 395b7ef2..a8ab80e0 100644 --- a/tests/shader_runner_d3d11.c +++ b/tests/shader_runner_d3d11.c @@ -47,6 +47,7 @@ struct d3d11_resource ID3D11Buffer *buffer; ID3D11Texture2D *texture; ID3D11RenderTargetView *rtv; + ID3D11DepthStencilView *dsv; ID3D11ShaderResourceView *srv; ID3D11UnorderedAccessView *uav; bool is_uav_counter; @@ -391,6 +392,8 @@ static bool init_resource_2d(struct d3d11_shader_runner *runner, struct d3d11_re desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS; else if (params->type == RESOURCE_TYPE_RENDER_TARGET) desc.BindFlags = D3D11_BIND_RENDER_TARGET; + else if (params->type == RESOURCE_TYPE_DEPTH_STENCIL) + desc.BindFlags = D3D11_BIND_DEPTH_STENCIL; else desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; @@ -424,6 +427,8 @@ static bool init_resource_2d(struct d3d11_shader_runner *runner, struct d3d11_re hr = ID3D11Device_CreateUnorderedAccessView(device, resource->resource, NULL, &resource->uav); else if (params->type == RESOURCE_TYPE_RENDER_TARGET) hr = ID3D11Device_CreateRenderTargetView(device, resource->resource, NULL, &resource->rtv); + else if (params->type == RESOURCE_TYPE_DEPTH_STENCIL) + hr = ID3D11Device_CreateDepthStencilView(device, resource->resource, NULL, &resource->dsv); else hr = ID3D11Device_CreateShaderResourceView(device, resource->resource, NULL, &resource->srv); ok(hr == S_OK, "Failed to create view, hr %#lx.\n", hr); @@ -481,6 +486,7 @@ static struct resource *d3d11_runner_create_resource(struct shader_runner *r, co switch (params->type) { case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_TEXTURE: if (params->dimension == RESOURCE_DIMENSION_BUFFER) init_resource_srv_buffer(runner, resource, params); @@ -511,6 +517,8 @@ static void d3d11_runner_destroy_resource(struct shader_runner *r, struct resour ID3D11Resource_Release(resource->resource); if (resource->rtv) ID3D11RenderTargetView_Release(resource->rtv); + if (resource->dsv) + ID3D11DepthStencilView_Release(resource->dsv); if (resource->srv) ID3D11ShaderResourceView_Release(resource->srv); if (resource->uav) @@ -580,6 +588,7 @@ static bool d3d11_runner_dispatch(struct shader_runner *r, unsigned int x, unsig break; case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_VERTEX_BUFFER: break; } @@ -603,6 +612,23 @@ static bool d3d11_runner_dispatch(struct shader_runner *r, unsigned int x, unsig return true; } +static void d3d11_runner_clear(struct shader_runner *r, struct resource *res, const struct vec4 *clear_value) +{ + struct d3d11_shader_runner *runner = d3d11_shader_runner(r); + ID3D11DeviceContext *context = runner->immediate_context; + struct d3d11_resource *resource = d3d11_resource(res); + + switch (resource->r.type) + { + case RESOURCE_TYPE_DEPTH_STENCIL: + ID3D11DeviceContext_ClearDepthStencilView(context, resource->dsv, D3D11_CLEAR_DEPTH, clear_value->x, 0); + break; + + default: + fatal_error("Clears are not implemented for resource type %u.\n", resource->r.type); + } +} + static bool d3d11_runner_draw(struct shader_runner *r, D3D_PRIMITIVE_TOPOLOGY primitive_topology, unsigned int vertex_count) { @@ -612,7 +638,10 @@ static bool d3d11_runner_draw(struct shader_runner *r, struct d3d11_shader_runner *runner = d3d11_shader_runner(r); ID3D11DeviceContext *context = runner->immediate_context; unsigned int min_uav_slot = ARRAY_SIZE(uavs); + ID3D11DepthStencilState *ds_state = NULL; + D3D11_DEPTH_STENCIL_DESC ds_desc = {0}; ID3D11Device *device = runner->device; + ID3D11DepthStencilView *dsv = NULL; unsigned int rtv_count = 0; ID3D11Buffer *cb = NULL; ID3D11VertexShader *vs; @@ -703,6 +732,16 @@ static bool d3d11_runner_draw(struct shader_runner *r, rtv_count = max(rtv_count, resource->r.slot + 1); break; + case RESOURCE_TYPE_DEPTH_STENCIL: + dsv = resource->dsv; + ds_desc.DepthEnable = TRUE; + ds_desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; + ds_desc.DepthFunc = runner->r.depth_func; + hr = ID3D11Device_CreateDepthStencilState(device, &ds_desc, &ds_state); + ok(hr == S_OK, "Failed to create depth/stencil state, hr %#lx.\n", hr); + ID3D11DeviceContext_OMSetDepthStencilState(context, ds_state, 0); + break; + case RESOURCE_TYPE_TEXTURE: ID3D11DeviceContext_PSSetShaderResources(context, resource->r.slot, 1, &resource->srv); break; @@ -719,7 +758,7 @@ static bool d3d11_runner_draw(struct shader_runner *r, } } - ID3D11DeviceContext_OMSetRenderTargetsAndUnorderedAccessViews(context, rtv_count, rtvs, NULL, + ID3D11DeviceContext_OMSetRenderTargetsAndUnorderedAccessViews(context, rtv_count, rtvs, dsv, min_uav_slot, ARRAY_SIZE(uavs) - min_uav_slot, &uavs[min_uav_slot], NULL); for (i = 0; i < runner->r.sampler_count; ++i) @@ -779,6 +818,8 @@ static bool d3d11_runner_draw(struct shader_runner *r, ID3D11DomainShader_Release(ds); if (cb) ID3D11Buffer_Release(cb); + if (ds_state) + ID3D11DepthStencilState_Release(ds_state); return true; } @@ -805,6 +846,7 @@ static struct resource_readback *d3d11_runner_get_resource_readback(struct shade switch (resource->r.type) { case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_UAV: if (resource->r.dimension == RESOURCE_DIMENSION_BUFFER) { @@ -879,6 +921,7 @@ static const struct shader_runner_ops d3d11_runner_ops = .create_resource = d3d11_runner_create_resource, .destroy_resource = d3d11_runner_destroy_resource, .dispatch = d3d11_runner_dispatch, + .clear = d3d11_runner_clear, .draw = d3d11_runner_draw, .get_resource_readback = d3d11_runner_get_resource_readback, .release_readback = d3d11_runner_release_readback, diff --git a/tests/shader_runner_d3d12.c b/tests/shader_runner_d3d12.c index eab3c61f..bb4157d4 100644 --- a/tests/shader_runner_d3d12.c +++ b/tests/shader_runner_d3d12.c @@ -46,7 +46,7 @@ struct d3d12_shader_runner struct test_context test_context; - ID3D12DescriptorHeap *heap, *rtv_heap; + ID3D12DescriptorHeap *heap, *rtv_heap, *dsv_heap; ID3D12CommandQueue *compute_queue; ID3D12CommandAllocator *compute_allocator; @@ -145,6 +145,16 @@ static struct resource *d3d12_runner_create_resource(struct shader_runner *r, co NULL, get_cpu_rtv_handle(test_context, runner->rtv_heap, resource->r.slot)); break; + case RESOURCE_TYPE_DEPTH_STENCIL: + if (!runner->dsv_heap) + runner->dsv_heap = create_cpu_descriptor_heap(device, D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1); + + resource->resource = create_default_texture2d(device, params->width, params->height, 1, params->level_count, + params->format, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, D3D12_RESOURCE_STATE_DEPTH_WRITE); + ID3D12Device_CreateDepthStencilView(device, resource->resource, + NULL, get_cpu_dsv_handle(test_context, runner->dsv_heap, 0)); + break; + case RESOURCE_TYPE_TEXTURE: if (!runner->heap) runner->heap = create_gpu_descriptor_heap(device, @@ -325,6 +335,7 @@ static ID3D12RootSignature *d3d12_runner_create_root_signature(struct d3d12_shad break; case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_VERTEX_BUFFER: break; } @@ -422,6 +433,7 @@ static bool d3d12_runner_dispatch(struct shader_runner *r, unsigned int x, unsig break; case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_VERTEX_BUFFER: break; } @@ -443,6 +455,36 @@ static bool d3d12_runner_dispatch(struct shader_runner *r, unsigned int x, unsig return true; } +static void d3d12_runner_clear(struct shader_runner *r, struct resource *resource, const struct vec4 *clear_value) +{ + struct d3d12_shader_runner *runner = d3d12_shader_runner(r); + struct test_context *test_context = &runner->test_context; + + ID3D12GraphicsCommandList *command_list = test_context->list; + ID3D12CommandQueue *queue = test_context->queue; + ID3D12Device *device = test_context->device; + D3D12_CPU_DESCRIPTOR_HANDLE view; + HRESULT hr; + + switch (resource->type) + { + case RESOURCE_TYPE_DEPTH_STENCIL: + view = get_cpu_dsv_handle(test_context, runner->dsv_heap, 0); + ID3D12GraphicsCommandList_ClearDepthStencilView(command_list, view, + D3D12_CLEAR_FLAG_DEPTH, clear_value->x, 0, 0, NULL); + break; + + default: + fatal_error("Clears are not implemented for resource type %u.\n", resource->type); + } + + hr = ID3D12GraphicsCommandList_Close(command_list); + ok(hr == S_OK, "Failed to close command list, hr %#x.\n", hr); + exec_command_list(queue, command_list); + wait_queue_idle(device, queue); + reset_command_list(command_list, test_context->allocator); +} + static bool d3d12_runner_draw(struct shader_runner *r, D3D_PRIMITIVE_TOPOLOGY primitive_topology, unsigned int vertex_count) { @@ -452,12 +494,12 @@ static bool d3d12_runner_draw(struct shader_runner *r, D3D12_CPU_DESCRIPTOR_HANDLE rtvs[D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT] = {0}; ID3D10Blob *vs_code, *ps_code, *hs_code = NULL, *ds_code = NULL; ID3D12GraphicsCommandList *command_list = test_context->list; + unsigned int uniform_index, sample_count, rtv_count = 0; D3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc = {0}; ID3D12CommandQueue *queue = test_context->queue; D3D12_INPUT_ELEMENT_DESC *input_element_descs; ID3D12Device *device = test_context->device; - unsigned int uniform_index, sample_count; - unsigned int rtv_count = 0; + D3D12_CPU_DESCRIPTOR_HANDLE dsv = {0}; ID3D12PipelineState *pso; bool succeeded; HRESULT hr; @@ -511,6 +553,14 @@ static bool d3d12_runner_draw(struct shader_runner *r, if (resource->r.sample_count) sample_count = resource->r.sample_count; } + else if (resource->r.type == RESOURCE_TYPE_DEPTH_STENCIL) + { + assert(!resource->r.slot); + pso_desc.DSVFormat = resource->r.format; + pso_desc.DepthStencilState.DepthEnable = true; + pso_desc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + pso_desc.DepthStencilState.DepthFunc = runner->r.depth_func; + } } pso_desc.VS.pShaderBytecode = ID3D10Blob_GetBufferPointer(vs_code); @@ -586,6 +636,10 @@ static bool d3d12_runner_draw(struct shader_runner *r, rtv_count = max(rtv_count, resource->r.slot + 1); break; + case RESOURCE_TYPE_DEPTH_STENCIL: + dsv = get_cpu_dsv_handle(test_context, runner->dsv_heap, 0); + break; + case RESOURCE_TYPE_TEXTURE: if (resource->descriptor_range.NumDescriptors) ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(command_list, resource->root_index, @@ -608,7 +662,7 @@ static bool d3d12_runner_draw(struct shader_runner *r, } } - ID3D12GraphicsCommandList_OMSetRenderTargets(command_list, rtv_count, rtvs, false, NULL); + ID3D12GraphicsCommandList_OMSetRenderTargets(command_list, rtv_count, rtvs, false, dsv.ptr ? &dsv : NULL); ID3D12GraphicsCommandList_RSSetScissorRects(command_list, 1, &test_context->scissor_rect); ID3D12GraphicsCommandList_RSSetViewports(command_list, 1, &test_context->viewport); @@ -636,6 +690,8 @@ static struct resource_readback *d3d12_runner_get_resource_readback(struct shade if (resource->r.type == RESOURCE_TYPE_RENDER_TARGET) state = D3D12_RESOURCE_STATE_RENDER_TARGET; + else if (resource->r.type == RESOURCE_TYPE_DEPTH_STENCIL) + state = D3D12_RESOURCE_STATE_DEPTH_WRITE; else state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; @@ -659,6 +715,7 @@ static const struct shader_runner_ops d3d12_runner_ops = .create_resource = d3d12_runner_create_resource, .destroy_resource = d3d12_runner_destroy_resource, .dispatch = d3d12_runner_dispatch, + .clear = d3d12_runner_clear, .draw = d3d12_runner_draw, .get_resource_readback = d3d12_runner_get_resource_readback, .release_readback = d3d12_runner_release_readback, @@ -737,5 +794,7 @@ void run_shader_tests_d3d12(void *dxc_compiler) ID3D12DescriptorHeap_Release(runner.heap); if (runner.rtv_heap) ID3D12DescriptorHeap_Release(runner.rtv_heap); + if (runner.dsv_heap) + ID3D12DescriptorHeap_Release(runner.dsv_heap); destroy_test_context(&runner.test_context); } diff --git a/tests/shader_runner_d3d9.c b/tests/shader_runner_d3d9.c index e4e1c643..f61a2305 100644 --- a/tests/shader_runner_d3d9.c +++ b/tests/shader_runner_d3d9.c @@ -224,6 +224,10 @@ static struct resource *d3d9_runner_create_resource(struct shader_runner *r, con ok(hr == D3D_OK, "Failed to create render target, hr %#lx.\n", hr); break; + case RESOURCE_TYPE_DEPTH_STENCIL: + fatal_error("DSVs are not supported.\n"); + break; + case RESOURCE_TYPE_TEXTURE: { if (params->dimension == RESOURCE_DIMENSION_BUFFER) @@ -320,6 +324,11 @@ static bool d3d9_runner_dispatch(struct shader_runner *r, unsigned int x, unsign fatal_error("Compute shaders are not supported.\n"); } +static void d3d9_runner_clear(struct shader_runner *r, struct resource *resource, const struct vec4 *clear_value) +{ + fatal_error("Clears are not implemented.\n"); +} + static bool d3d9_runner_draw(struct shader_runner *r, D3D_PRIMITIVE_TOPOLOGY primitive_topology, unsigned int vertex_count) { @@ -382,6 +391,9 @@ static bool d3d9_runner_draw(struct shader_runner *r, ok(hr == D3D_OK, "Failed to set render target, hr %#lx.\n", hr); break; + case RESOURCE_TYPE_DEPTH_STENCIL: + vkd3d_unreachable(); + case RESOURCE_TYPE_TEXTURE: assert(resource->r.dimension != RESOURCE_DIMENSION_BUFFER); @@ -528,6 +540,7 @@ static const struct shader_runner_ops d3d9_runner_ops = .create_resource = d3d9_runner_create_resource, .destroy_resource = d3d9_runner_destroy_resource, .dispatch = d3d9_runner_dispatch, + .clear = d3d9_runner_clear, .draw = d3d9_runner_draw, .get_resource_readback = d3d9_runner_get_resource_readback, .release_readback = d3d9_runner_release_readback, diff --git a/tests/shader_runner_gl.c b/tests/shader_runner_gl.c index 3b0a77e8..7e93763f 100644 --- a/tests/shader_runner_gl.c +++ b/tests/shader_runner_gl.c @@ -335,6 +335,7 @@ static const struct format_info *get_format_info(enum DXGI_FORMAT format, bool i {DXGI_FORMAT_R32G32_SINT, 2, true, false, GL_RG32I, GL_RG_INTEGER, GL_INT}, {DXGI_FORMAT_R32_FLOAT, 1, false, false, GL_R32F, GL_RED, GL_FLOAT}, {DXGI_FORMAT_R32_FLOAT, 1, false, true, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT}, + {DXGI_FORMAT_D32_FLOAT, 1, false, true, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT}, {DXGI_FORMAT_R32_UINT, 1, true, false, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT}, {DXGI_FORMAT_R32_SINT, 1, true, false, GL_R32I, GL_RED_INTEGER, GL_INT}, }; @@ -410,6 +411,7 @@ static struct resource *gl_runner_create_resource(struct shader_runner *r, const switch (params->type) { case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_TEXTURE: case RESOURCE_TYPE_UAV: if (params->dimension == RESOURCE_DIMENSION_BUFFER) @@ -435,6 +437,7 @@ static void gl_runner_destroy_resource(struct shader_runner *r, struct resource switch (resource->r.type) { case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_TEXTURE: case RESOURCE_TYPE_UAV: if (res->dimension == RESOURCE_DIMENSION_BUFFER) @@ -721,6 +724,7 @@ static bool gl_runner_dispatch(struct shader_runner *r, unsigned int x, unsigned switch (resource->r.type) { case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_VERTEX_BUFFER: case RESOURCE_TYPE_TEXTURE: break; @@ -932,6 +936,31 @@ static GLuint compile_graphics_shader_program(struct gl_runner *runner, ID3D10Bl return program_id; } +static void gl_runner_clear(struct shader_runner *r, struct resource *res, const struct vec4 *clear_value) +{ + struct gl_resource *resource = gl_resource(res); + struct gl_runner *runner = gl_runner(r); + + if (!runner->fbo_id) + glGenFramebuffers(1, &runner->fbo_id); + glBindFramebuffer(GL_FRAMEBUFFER, runner->fbo_id); + + switch (resource->r.type) + { + case RESOURCE_TYPE_DEPTH_STENCIL: + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, resource->id, 0); + glDepthMask(GL_TRUE); + glClearDepthf(clear_value->x); + break; + + default: + fatal_error("Clears are not implemented for resource type %u.\n", resource->r.type); + } + + glScissor(0, 0, res->width, res->height); + glClear(GL_DEPTH_BUFFER_BIT); +} + static bool gl_runner_draw(struct shader_runner *r, D3D_PRIMITIVE_TOPOLOGY topology, unsigned int vertex_count) { @@ -1040,6 +1069,13 @@ static bool gl_runner_draw(struct shader_runner *r, rt_count = resource->r.slot + 1; break; + case RESOURCE_TYPE_DEPTH_STENCIL: + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, resource->id, 0); + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glDepthFunc(get_compare_op_gl(runner->r.depth_func)); + break; + case RESOURCE_TYPE_TEXTURE: break; @@ -1135,7 +1171,8 @@ static struct resource_readback *gl_runner_get_resource_readback(struct shader_r struct gl_resource *resource = gl_resource(res); struct resource_readback *rb; - if (resource->r.type != RESOURCE_TYPE_RENDER_TARGET && resource->r.type != RESOURCE_TYPE_UAV) + if (resource->r.type != RESOURCE_TYPE_RENDER_TARGET && resource->r.type != RESOURCE_TYPE_DEPTH_STENCIL + && resource->r.type != RESOURCE_TYPE_UAV) fatal_error("Unhandled resource type %#x.\n", resource->r.type); rb = malloc(sizeof(*rb)); @@ -1172,6 +1209,7 @@ static const struct shader_runner_ops gl_runner_ops = .create_resource = gl_runner_create_resource, .destroy_resource = gl_runner_destroy_resource, .dispatch = gl_runner_dispatch, + .clear = gl_runner_clear, .draw = gl_runner_draw, .get_resource_readback = gl_runner_get_resource_readback, .release_readback = gl_runner_release_readback, diff --git a/tests/shader_runner_vulkan.c b/tests/shader_runner_vulkan.c index cedecce3..8a3e9163 100644 --- a/tests/shader_runner_vulkan.c +++ b/tests/shader_runner_vulkan.c @@ -120,7 +120,7 @@ static void end_command_buffer(struct vulkan_shader_runner *runner) } static void transition_image_layout(struct vulkan_shader_runner *runner, - VkImage image, VkImageLayout src_layout, VkImageLayout dst_layout) + VkImage image, VkImageAspectFlags aspect_mask, VkImageLayout src_layout, VkImageLayout dst_layout) { VkImageMemoryBarrier barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; @@ -131,7 +131,7 @@ static void transition_image_layout(struct vulkan_shader_runner *runner, barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barrier.image = image; - barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + barrier.subresourceRange.aspectMask = aspect_mask; barrier.subresourceRange.baseMipLevel = 0; barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS; barrier.subresourceRange.baseArrayLayer = 0; @@ -240,7 +240,8 @@ static VkImage create_2d_image(const struct vulkan_shader_runner *runner, uint32 return image; } -static VkImageView create_2d_image_view(const struct vulkan_shader_runner *runner, VkImage image, VkFormat format) +static VkImageView create_2d_image_view(const struct vulkan_shader_runner *runner, VkImage image, VkFormat format, + VkImageAspectFlags aspect_mask) { VkImageViewCreateInfo view_info = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; VkImageView view; @@ -252,7 +253,7 @@ static VkImageView create_2d_image_view(const struct vulkan_shader_runner *runne view_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; view_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; view_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; - view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + view_info.subresourceRange.aspectMask = aspect_mask; view_info.subresourceRange.baseMipLevel = 0; view_info.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS; view_info.subresourceRange.baseArrayLayer = 0; @@ -282,12 +283,12 @@ static void resource_init_2d(struct vulkan_shader_runner *runner, struct vulkan_ resource->image = create_2d_image(runner, params->width, params->height, params->level_count, usage, format, &resource->memory); - resource->image_view = create_2d_image_view(runner, resource->image, format); + resource->image_view = create_2d_image_view(runner, resource->image, format, VK_IMAGE_ASPECT_COLOR_BIT); if (!params->data) { begin_command_buffer(runner); - transition_image_layout(runner, resource->image, + transition_image_layout(runner, resource->image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, layout); end_command_buffer(runner); return; @@ -301,7 +302,7 @@ static void resource_init_2d(struct vulkan_shader_runner *runner, struct vulkan_ begin_command_buffer(runner); - transition_image_layout(runner, resource->image, + transition_image_layout(runner, resource->image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); for (unsigned int level = 0; level < params->level_count; ++level) @@ -323,7 +324,7 @@ static void resource_init_2d(struct vulkan_shader_runner *runner, struct vulkan_ buffer_offset += level_width * level_height * params->texel_size; } - transition_image_layout(runner, resource->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, layout); + transition_image_layout(runner, resource->image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, layout); end_command_buffer(runner); @@ -375,14 +376,28 @@ static struct resource *vulkan_runner_create_resource(struct shader_runner *r, c resource->image = create_2d_image(runner, params->width, params->height, params->level_count, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, format, &resource->memory); - resource->image_view = create_2d_image_view(runner, resource->image, format); + resource->image_view = create_2d_image_view(runner, resource->image, format, VK_IMAGE_ASPECT_COLOR_BIT); begin_command_buffer(runner); - transition_image_layout(runner, resource->image, + transition_image_layout(runner, resource->image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); end_command_buffer(runner); break; + case RESOURCE_TYPE_DEPTH_STENCIL: + format = vkd3d_get_vk_format(params->format); + + resource->image = create_2d_image(runner, params->width, params->height, params->level_count, + VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, format, + &resource->memory); + resource->image_view = create_2d_image_view(runner, resource->image, format, VK_IMAGE_ASPECT_DEPTH_BIT); + + begin_command_buffer(runner); + transition_image_layout(runner, resource->image, VK_IMAGE_ASPECT_DEPTH_BIT, + VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + end_command_buffer(runner); + break; + case RESOURCE_TYPE_TEXTURE: case RESOURCE_TYPE_UAV: if (params->dimension == RESOURCE_DIMENSION_BUFFER) @@ -541,6 +556,7 @@ static bool compile_shader(struct vulkan_shader_runner *runner, const char *sour switch (resource->r.type) { case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_VERTEX_BUFFER: break; @@ -669,6 +685,31 @@ static VkPipelineLayout create_pipeline_layout(const struct vulkan_shader_runner return pipeline_layout; } +static enum VkCompareOp vk_compare_op_from_d3d12(D3D12_COMPARISON_FUNC op) +{ + switch (op) + { + case D3D12_COMPARISON_FUNC_NEVER: + return VK_COMPARE_OP_NEVER; + case D3D12_COMPARISON_FUNC_LESS: + return VK_COMPARE_OP_LESS; + case D3D12_COMPARISON_FUNC_EQUAL: + return VK_COMPARE_OP_EQUAL; + case D3D12_COMPARISON_FUNC_LESS_EQUAL: + return VK_COMPARE_OP_LESS_OR_EQUAL; + case D3D12_COMPARISON_FUNC_GREATER: + return VK_COMPARE_OP_GREATER; + case D3D12_COMPARISON_FUNC_NOT_EQUAL: + return VK_COMPARE_OP_NOT_EQUAL; + case D3D12_COMPARISON_FUNC_GREATER_EQUAL: + return VK_COMPARE_OP_GREATER_OR_EQUAL; + case D3D12_COMPARISON_FUNC_ALWAYS: + return VK_COMPARE_OP_ALWAYS; + default: + fatal_error("Unhandled compare op %#x.\n", op); + } +} + static VkPipeline create_graphics_pipeline(struct vulkan_shader_runner *runner, VkRenderPass render_pass, VkPipelineLayout pipeline_layout, D3D_PRIMITIVE_TOPOLOGY primitive_topology) { @@ -685,6 +726,7 @@ static VkPipeline create_graphics_pipeline(struct vulkan_shader_runner *runner, VkPipelineColorBlendAttachmentState attachment_desc[MAX_RESOURCES] = {0}; VkPipelineTessellationStateCreateInfo tessellation_info; VkVertexInputAttributeDescription input_attributes[32]; + VkPipelineDepthStencilStateCreateInfo ds_desc = {0}; VkVertexInputBindingDescription input_bindings[32]; VkPipelineShaderStageCreateInfo stage_desc[4]; struct vkd3d_shader_code vs_dxbc; @@ -752,6 +794,20 @@ static VkPipeline create_graphics_pipeline(struct vulkan_shader_runner *runner, | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; break; + case RESOURCE_TYPE_DEPTH_STENCIL: + ds_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; + ds_desc.pNext = NULL; + ds_desc.flags = 0; + ds_desc.depthTestEnable = VK_TRUE; + ds_desc.depthWriteEnable = VK_TRUE; + ds_desc.depthCompareOp = vk_compare_op_from_d3d12(runner->r.depth_func); + ds_desc.depthBoundsTestEnable = VK_FALSE; + ds_desc.stencilTestEnable = VK_FALSE; + ds_desc.minDepthBounds = 0.0f; + ds_desc.maxDepthBounds = 1.0f; + pipeline_desc.pDepthStencilState = &ds_desc; + break; + case RESOURCE_TYPE_VERTEX_BUFFER: { VkVertexInputBindingDescription *binding = &input_bindings[input_desc.vertexBindingDescriptionCount++]; @@ -858,31 +914,6 @@ static VkSamplerAddressMode vk_address_mode_from_d3d12(D3D12_TEXTURE_ADDRESS_MOD } } -static enum VkCompareOp vk_compare_op_from_d3d12(D3D12_COMPARISON_FUNC op) -{ - switch (op) - { - case D3D12_COMPARISON_FUNC_NEVER: - return VK_COMPARE_OP_NEVER; - case D3D12_COMPARISON_FUNC_LESS: - return VK_COMPARE_OP_LESS; - case D3D12_COMPARISON_FUNC_EQUAL: - return VK_COMPARE_OP_EQUAL; - case D3D12_COMPARISON_FUNC_LESS_EQUAL: - return VK_COMPARE_OP_LESS_OR_EQUAL; - case D3D12_COMPARISON_FUNC_GREATER: - return VK_COMPARE_OP_GREATER; - case D3D12_COMPARISON_FUNC_NOT_EQUAL: - return VK_COMPARE_OP_NOT_EQUAL; - case D3D12_COMPARISON_FUNC_GREATER_EQUAL: - return VK_COMPARE_OP_GREATER_OR_EQUAL; - case D3D12_COMPARISON_FUNC_ALWAYS: - return VK_COMPARE_OP_ALWAYS; - default: - fatal_error("Unhandled compare op %#x.\n", op); - } -} - static VkDescriptorSetLayout create_descriptor_set_layout(struct vulkan_shader_runner *runner) { VkDescriptorSetLayoutCreateInfo set_desc = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; @@ -904,6 +935,7 @@ static VkDescriptorSetLayout create_descriptor_set_layout(struct vulkan_shader_r switch (resource->r.type) { case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: case RESOURCE_TYPE_VERTEX_BUFFER: break; @@ -1035,6 +1067,7 @@ static void bind_resources(struct vulkan_shader_runner *runner, VkPipelineBindPo break; case RESOURCE_TYPE_RENDER_TARGET: + case RESOURCE_TYPE_DEPTH_STENCIL: break; } } @@ -1053,44 +1086,56 @@ static void create_render_pass_and_framebuffer(struct vulkan_shader_runner *runn { VkRenderPassCreateInfo render_pass_desc = {.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO}; VkFramebufferCreateInfo fb_desc = {.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO}; + VkAttachmentReference ds_ref = {0}, color_refs[MAX_RESOURCES] = {0}; VkAttachmentDescription attachment_descs[MAX_RESOURCES] = {0}; - VkAttachmentReference color_refs[MAX_RESOURCES] = {0}; + unsigned int i, color_ref_count = 0, view_count = 0; VkSubpassDescription subpass_desc = {0}; - VkImageView rtvs[MAX_RESOURCES]; - unsigned int rt_count = 0; - unsigned int i; + VkImageView views[MAX_RESOURCES]; + VkImageLayout layout; + bool is_ds; for (i = 0; i < runner->r.resource_count; ++i) { const struct vulkan_resource *resource = vulkan_resource(runner->r.resources[i]); - VkAttachmentDescription *attachment_desc = &attachment_descs[rt_count]; - VkAttachmentReference *color_ref = &color_refs[rt_count]; + VkAttachmentDescription *attachment_desc = &attachment_descs[view_count]; + VkAttachmentReference *color_ref = &color_refs[color_ref_count]; - if (resource->r.type != RESOURCE_TYPE_RENDER_TARGET) + if (resource->r.type != RESOURCE_TYPE_RENDER_TARGET && resource->r.type != RESOURCE_TYPE_DEPTH_STENCIL) continue; + is_ds = resource->r.type == RESOURCE_TYPE_DEPTH_STENCIL; + layout = is_ds ? VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + attachment_desc->format = vkd3d_get_vk_format(resource->r.format); attachment_desc->samples = VK_SAMPLE_COUNT_1_BIT; attachment_desc->loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; attachment_desc->storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachment_desc->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachment_desc->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachment_desc->initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - attachment_desc->finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + attachment_desc->initialLayout = layout; + attachment_desc->finalLayout = layout; - color_ref->attachment = rt_count; - color_ref->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + if (is_ds) + { + ds_ref.attachment = view_count; + ds_ref.layout = layout; + subpass_desc.pDepthStencilAttachment = &ds_ref; + } + else + { + color_ref->attachment = view_count; + color_ref->layout = layout; + ++color_ref_count; + } - rtvs[rt_count] = resource->image_view; - - ++rt_count; + views[view_count++] = resource->image_view; } subpass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; - subpass_desc.colorAttachmentCount = rt_count; + subpass_desc.colorAttachmentCount = color_ref_count; subpass_desc.pColorAttachments = color_refs; - render_pass_desc.attachmentCount = rt_count; + render_pass_desc.attachmentCount = view_count; render_pass_desc.pAttachments = attachment_descs; render_pass_desc.subpassCount = 1; render_pass_desc.pSubpasses = &subpass_desc; @@ -1098,8 +1143,8 @@ static void create_render_pass_and_framebuffer(struct vulkan_shader_runner *runn VK_CALL(vkCreateRenderPass(runner->device, &render_pass_desc, NULL, render_pass)); fb_desc.renderPass = *render_pass; - fb_desc.attachmentCount = rt_count; - fb_desc.pAttachments = rtvs; + fb_desc.attachmentCount = view_count; + fb_desc.pAttachments = views; fb_desc.width = RENDER_TARGET_WIDTH; fb_desc.height = RENDER_TARGET_HEIGHT; fb_desc.layers = 1; @@ -1150,6 +1195,91 @@ out: return ret; } +static void vulkan_runner_clear(struct shader_runner *r, struct resource *res, const struct vec4 *clear_value) +{ + struct vulkan_shader_runner *runner = vulkan_shader_runner(r); + struct vulkan_resource *resource = vulkan_resource(res); + + size_t width = resource->r.width, height = resource->r.height; + VkSubpassDescription sub_pass_desc = {0}; + VkAttachmentDescription attachment_desc; + VkRenderPassCreateInfo pass_desc = {0}; + VkAttachmentReference attachment_ref; + VkDevice device = runner->device; + VkRenderPassBeginInfo begin_desc; + VkFramebufferCreateInfo fb_desc; + VkClearValue vk_clear_value; + VkRenderPass render_pass; + VkFramebuffer fb; + + attachment_desc.flags = 0; + attachment_desc.format = vkd3d_get_vk_format(resource->r.format); + attachment_desc.samples = VK_SAMPLE_COUNT_1_BIT; + attachment_desc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + attachment_desc.storeOp = VK_ATTACHMENT_STORE_OP_STORE; + /* TODO: formats with a stencil component would a clear op here. */ + attachment_desc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + attachment_desc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + + sub_pass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; + + switch (resource->r.type) + { + case RESOURCE_TYPE_DEPTH_STENCIL: + attachment_desc.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + sub_pass_desc.pDepthStencilAttachment = &attachment_ref; + vk_clear_value.depthStencil.depth = clear_value->x; + vk_clear_value.depthStencil.stencil = 0; + break; + + default: + fatal_error("Clears are not implemented for resource type %u.\n", resource->r.type); + } + + attachment_desc.finalLayout = attachment_desc.initialLayout; + + attachment_ref.attachment = 0; + attachment_ref.layout = attachment_desc.initialLayout; + + begin_command_buffer(runner); + + pass_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; + pass_desc.attachmentCount = 1; + pass_desc.pAttachments = &attachment_desc; + pass_desc.subpassCount = 1; + pass_desc.pSubpasses = &sub_pass_desc; + VK_CALL(vkCreateRenderPass(device, &pass_desc, NULL, &render_pass)); + + fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; + fb_desc.pNext = NULL; + fb_desc.flags = 0; + fb_desc.renderPass = render_pass; + fb_desc.attachmentCount = 1; + fb_desc.pAttachments = &resource->image_view; + fb_desc.width = width; + fb_desc.height = height; + fb_desc.layers = 1; + VK_CALL(vkCreateFramebuffer(device, &fb_desc, NULL, &fb)); + + begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; + begin_desc.pNext = NULL; + begin_desc.renderPass = render_pass; + begin_desc.framebuffer = fb; + begin_desc.clearValueCount = 1; + begin_desc.pClearValues = &vk_clear_value; + begin_desc.renderArea.offset.x = 0; + begin_desc.renderArea.offset.y = 0; + begin_desc.renderArea.extent.width = width; + begin_desc.renderArea.extent.height = height; + VK_CALL(vkCmdBeginRenderPass(runner->cmd_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE)); + VK_CALL(vkCmdEndRenderPass(runner->cmd_buffer)); + + end_command_buffer(runner); + + VK_CALL(vkDestroyRenderPass(device, render_pass, NULL)); + VK_CALL(vkDestroyFramebuffer(device, fb, NULL)); +} + static bool vulkan_runner_draw(struct shader_runner *r, D3D_PRIMITIVE_TOPOLOGY primitive_topology, unsigned int vertex_count) { @@ -1222,6 +1352,7 @@ static struct resource_readback *vulkan_runner_get_resource_readback(struct shad struct vulkan_resource_readback *rb = malloc(sizeof(*rb)); struct vulkan_resource *resource = vulkan_resource(res); VkDevice device = runner->device; + VkImageAspectFlags aspect_mask; VkBufferImageCopy region = {0}; VkImageLayout layout; @@ -1245,16 +1376,21 @@ static struct resource_readback *vulkan_runner_get_resource_readback(struct shad } else { + aspect_mask = (resource->r.type == RESOURCE_TYPE_DEPTH_STENCIL) + ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT; + if (resource->r.type == RESOURCE_TYPE_RENDER_TARGET) layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + else if (resource->r.type == RESOURCE_TYPE_DEPTH_STENCIL) + layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; else layout = VK_IMAGE_LAYOUT_GENERAL; begin_command_buffer(runner); - transition_image_layout(runner, resource->image, layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + transition_image_layout(runner, resource->image, aspect_mask, layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); - region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + region.imageSubresource.aspectMask = aspect_mask; region.imageSubresource.layerCount = 1; region.imageExtent.width = resource->r.width; region.imageExtent.height = resource->r.height; @@ -1263,7 +1399,7 @@ static struct resource_readback *vulkan_runner_get_resource_readback(struct shad VK_CALL(vkCmdCopyImageToBuffer(runner->cmd_buffer, resource->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, rb->buffer, 1, ®ion)); - transition_image_layout(runner, resource->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, layout); + transition_image_layout(runner, resource->image, aspect_mask, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, layout); end_command_buffer(runner); @@ -1291,6 +1427,7 @@ static const struct shader_runner_ops vulkan_runner_ops = .create_resource = vulkan_runner_create_resource, .destroy_resource = vulkan_runner_destroy_resource, .dispatch = vulkan_runner_dispatch, + .clear = vulkan_runner_clear, .draw = vulkan_runner_draw, .get_resource_readback = vulkan_runner_get_resource_readback, .release_readback = vulkan_runner_release_readback,