From: Zebediah Figura Subject: [PATCH vkd3d v2 3/7] tests: Make texture allocation and destruction into separate shader_runner ops. Message-Id: <20220114233308.49491-3-zfigura@codeweavers.com> Date: Fri, 14 Jan 2022 17:33:04 -0600 In-Reply-To: <20220114233308.49491-1-zfigura@codeweavers.com> References: <20220114233308.49491-1-zfigura@codeweavers.com> Signed-off-by: Zebediah Figura --- tests/shader_runner.c | 60 ++++++++++++++++------------------ tests/shader_runner.h | 11 +++++-- tests/shader_runner_d3d12.c | 65 ++++++++++++++++++++++--------------- 3 files changed, 75 insertions(+), 61 deletions(-) diff --git a/tests/shader_runner.c b/tests/shader_runner.c index 76823e165..c1684b8fb 100644 --- a/tests/shader_runner.c +++ b/tests/shader_runner.c @@ -66,12 +66,6 @@ static void VKD3D_NORETURN VKD3D_PRINTF_FUNC(1, 2) fatal_error(const char *forma exit(1); } -static void free_texture(struct texture *texture) -{ - free(texture->data); - memset(texture, 0, sizeof(*texture)); -} - enum parse_state { STATE_NONE, @@ -99,7 +93,7 @@ static bool match_string(const char *line, const char *token, const char **const return true; } -static void parse_texture_format(struct texture *texture, const char *line) +static void parse_texture_format(struct texture_params *texture, const char *line) { static const struct { @@ -192,7 +186,7 @@ static void parse_sampler_directive(struct sampler *sampler, const char *line) } } -static void parse_texture_directive(struct texture *texture, const char *line) +static void parse_texture_directive(struct texture_params *texture, const char *line) { int ret; @@ -389,26 +383,29 @@ static struct sampler *get_sampler(struct shader_context *context, unsigned int return NULL; } -static struct texture *get_texture(struct shader_context *context, unsigned int slot) +static void set_texture(struct shader_context *context, struct texture *texture) { - struct texture *texture; size_t i; for (i = 0; i < context->texture_count; ++i) { - texture = &context->textures[i]; - if (texture->slot == slot) - return texture; + if (context->textures[i]->slot == texture->slot) + { + context->ops->destroy_texture(context, context->textures[i]); + context->textures[i] = texture; + return; + } } - return NULL; + context->textures = realloc(context->textures, (context->texture_count + 1) * sizeof(*context->textures)); + context->textures[context->texture_count++] = texture; } void run_shader_tests(struct shader_context *context, int argc, char **argv, const struct shader_runner_ops *ops) { size_t shader_source_size = 0, shader_source_len = 0; struct sampler *current_sampler = NULL; - struct texture *current_texture = NULL; + struct texture_params current_texture; enum parse_state state = STATE_NONE; unsigned int i, line_number = 0; const char *filename = NULL; @@ -453,7 +450,11 @@ void run_shader_tests(struct shader_context *context, int argc, char **argv, con case STATE_NONE: case STATE_SAMPLER: case STATE_TEST: + break; + case STATE_TEXTURE: + set_texture(context, context->ops->create_texture(context, ¤t_texture)); + free(current_texture.data); break; case STATE_SHADER_PIXEL: @@ -581,21 +582,12 @@ void run_shader_tests(struct shader_context *context, int argc, char **argv, con { state = STATE_TEXTURE; - if ((current_texture = get_texture(context, index))) - { - free_texture(current_texture); - } - else - { - context->textures = realloc(context->textures, - ++context->texture_count * sizeof(*context->textures)); - current_texture = &context->textures[context->texture_count - 1]; - memset(current_texture, 0, sizeof(*current_texture)); - } - current_texture->slot = index; - current_texture->format = DXGI_FORMAT_R32G32B32A32_FLOAT; - current_texture->data_type = TEXTURE_DATA_FLOAT; - current_texture->texel_size = 16; + memset(¤t_texture, 0, sizeof(current_texture)); + + current_texture.slot = index; + current_texture.format = DXGI_FORMAT_R32G32B32A32_FLOAT; + current_texture.data_type = TEXTURE_DATA_FLOAT; + current_texture.texel_size = 16; } else if (!strcmp(line, "[test]\n")) { @@ -638,7 +630,7 @@ void run_shader_tests(struct shader_context *context, int argc, char **argv, con break; case STATE_TEXTURE: - parse_texture_directive(current_texture, line); + parse_texture_directive(¤t_texture, line); break; case STATE_TEST: @@ -651,7 +643,11 @@ void run_shader_tests(struct shader_context *context, int argc, char **argv, con if (context->ps_code) ID3D10Blob_Release(context->ps_code); for (i = 0; i < context->texture_count; ++i) - free_texture(&context->textures[i]); + { + if (context->textures[i]) + context->ops->destroy_texture(context, context->textures[i]); + } + free(context->textures); fclose(f); } diff --git a/tests/shader_runner.h b/tests/shader_runner.h index 4d5242ea1..3bc67feee 100644 --- a/tests/shader_runner.h +++ b/tests/shader_runner.h @@ -39,7 +39,7 @@ struct sampler D3D12_TEXTURE_ADDRESS_MODE u_address, v_address, w_address; }; -struct texture +struct texture_params { unsigned int slot; @@ -49,6 +49,11 @@ struct texture unsigned int width, height; uint8_t *data; size_t data_size, data_capacity; +}; + +struct texture +{ + unsigned int slot; D3D12_DESCRIPTOR_RANGE descriptor_range; ID3D12DescriptorHeap *heap; @@ -65,7 +70,7 @@ struct shader_context uint32_t *uniforms; size_t uniform_count; - struct texture *textures; + struct texture **textures; size_t texture_count; struct sampler *samplers; @@ -75,6 +80,8 @@ struct shader_context struct shader_runner_ops { ID3D10Blob *(*compile_shader)(struct shader_context *context, const char *source); + struct texture *(*create_texture)(struct shader_context *context, const struct texture_params *params); + void (*destroy_texture)(struct shader_context *context, struct texture *texture); void (*draw_quad)(struct shader_context *context); void (*probe_vec4)(struct shader_context *context, const RECT *rect, const struct vec4 *v, unsigned int ulps); }; diff --git a/tests/shader_runner_d3d12.c b/tests/shader_runner_d3d12.c index e30e0f931..4a793c26c 100644 --- a/tests/shader_runner_d3d12.c +++ b/tests/shader_runner_d3d12.c @@ -51,6 +51,40 @@ static ID3D10Blob *d3d12_runner_compile_shader(struct shader_context *c, const c return blob; } +static struct texture *d3d12_runner_create_texture(struct shader_context *c, const struct texture_params *params) +{ + struct d3d12_shader_context *context = d3d12_shader_context(c); + struct test_context *test_context = &context->test_context; + ID3D12Device *device = test_context->device; + D3D12_SUBRESOURCE_DATA resource_data; + struct texture *texture; + + texture = calloc(1, sizeof(*texture)); + + texture->slot = params->slot; + + texture->heap = create_gpu_descriptor_heap(device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1); + texture->resource = create_default_texture(device, params->width, params->height, + params->format, 0, D3D12_RESOURCE_STATE_COPY_DEST); + resource_data.pData = params->data; + resource_data.SlicePitch = resource_data.RowPitch = params->width * params->texel_size; + upload_texture_data(texture->resource, &resource_data, 1, test_context->queue, test_context->list); + reset_command_list(test_context->list, test_context->allocator); + transition_resource_state(test_context->list, texture->resource, D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); + ID3D12Device_CreateShaderResourceView(device, texture->resource, + NULL, get_cpu_descriptor_handle(test_context, texture->heap, 0)); + + return texture; +} + +static void d3d12_runner_destroy_texture(struct shader_context *c, struct texture *texture) +{ + ID3D12DescriptorHeap_Release(texture->heap); + ID3D12Resource_Release(texture->resource); + free(texture); +} + static void d3d12_runner_draw_quad(struct shader_context *c) { struct d3d12_shader_context *context = d3d12_shader_context(c); @@ -88,10 +122,8 @@ static void d3d12_runner_draw_quad(struct shader_context *c) for (i = 0; i < context->c.texture_count; ++i) { - struct texture *texture = &context->c.textures[i]; - D3D12_SUBRESOURCE_DATA resource_data; + struct texture *texture = context->c.textures[i]; D3D12_DESCRIPTOR_RANGE *range; - ID3D12Resource *resource; range = &texture->descriptor_range; @@ -107,20 +139,6 @@ static void d3d12_runner_draw_quad(struct shader_context *c) range->BaseShaderRegister = texture->slot; range->RegisterSpace = 0; range->OffsetInDescriptorsFromTableStart = 0; - - texture->heap = create_gpu_descriptor_heap(device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1); - resource = create_default_texture(device, texture->width, texture->height, - texture->format, 0, D3D12_RESOURCE_STATE_COPY_DEST); - resource_data.pData = texture->data; - resource_data.SlicePitch = resource_data.RowPitch = texture->width * texture->texel_size; - upload_texture_data(resource, &resource_data, 1, queue, command_list); - reset_command_list(command_list, test_context->allocator); - transition_resource_state(command_list, resource, D3D12_RESOURCE_STATE_COPY_DEST, - D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); - ID3D12Device_CreateShaderResourceView(device, resource, - NULL, get_cpu_descriptor_handle(test_context, texture->heap, 0)); - - texture->resource = resource; } assert(root_signature_desc.NumParameters <= ARRAY_SIZE(root_params)); @@ -159,7 +177,7 @@ static void d3d12_runner_draw_quad(struct shader_context *c) context->c.uniform_count, context->c.uniforms, 0); for (i = 0; i < context->c.texture_count; ++i) { - struct texture *texture = &context->c.textures[i]; + struct texture *texture = context->c.textures[i]; ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(command_list, texture->root_index, get_gpu_descriptor_handle(test_context, texture->heap, 0)); @@ -181,15 +199,6 @@ static void d3d12_runner_draw_quad(struct shader_context *c) exec_command_list(queue, command_list); wait_queue_idle(device, queue); reset_command_list(command_list, test_context->allocator); - - for (i = 0; i < context->c.texture_count; ++i) - { - struct texture *texture = &context->c.textures[i]; - - ID3D12DescriptorHeap_Release(texture->heap); - ID3D12Resource_Release(texture->resource); - free(texture); - } } static void d3d12_runner_probe_vec4(struct shader_context *c, @@ -209,6 +218,8 @@ static void d3d12_runner_probe_vec4(struct shader_context *c, static const struct shader_runner_ops d3d12_runner_ops = { .compile_shader = d3d12_runner_compile_shader, + .create_texture = d3d12_runner_create_texture, + .destroy_texture = d3d12_runner_destroy_texture, .draw_quad = d3d12_runner_draw_quad, .probe_vec4 = d3d12_runner_probe_vec4, }; -- 2.34.1