[Mesa-dev] [PATCH] r600g/compute: Use a constant buffer to store kernel parameters v2

Tom Stellard tom at stellard.net
Thu Apr 25 10:45:03 PDT 2013


From: Tom Stellard <thomas.stellard at amd.com>

v2:
  - Fix usage of set_constant_buffer()
  - Fix typo in comment
---
 src/gallium/drivers/r600/evergreen_compute.c       | 46 +++++++++++++++-------
 .../drivers/r600/evergreen_compute_internal.h      |  2 +-
 2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/src/gallium/drivers/r600/evergreen_compute.c b/src/gallium/drivers/r600/evergreen_compute.c
index 05e5e3d..774b8b6 100644
--- a/src/gallium/drivers/r600/evergreen_compute.c
+++ b/src/gallium/drivers/r600/evergreen_compute.c
@@ -102,6 +102,24 @@ static void evergreen_cs_set_vertex_buffer(
 	state->atom.dirty = true;
 }
 
+static void evergreen_cs_set_constant_buffer(
+	struct r600_context * rctx,
+	unsigned cb_index,
+	unsigned offset,
+	unsigned size,
+	struct pipe_resource * buffer)
+{
+	struct r600_constbuf_state *state =
+				&rctx->constbuf_state[PIPE_SHADER_COMPUTE];
+	struct pipe_constant_buffer cb;
+	cb.buffer_size = size;
+	cb.buffer_offset = offset;
+	cb.buffer = buffer;
+	cb.user_buffer = NULL;
+
+	rctx->context.set_constant_buffer(&rctx->context, PIPE_SHADER_COMPUTE, cb_index, &cb);
+}
+
 static const struct u_resource_vtbl r600_global_buffer_vtbl =
 {
 	u_default_resource_get_handle, /* get_handle */
@@ -189,7 +207,10 @@ void evergreen_compute_upload_input(
 	struct r600_context *ctx = (struct r600_context *)ctx_;
 	struct r600_pipe_compute *shader = ctx->cs_shader_state.shader;
 	int i;
-	unsigned kernel_parameters_offset_bytes = 36;
+	/* We need to reserve 9 dwords (36 bytes) for implicit kernel
+	 * parameters.
+	 */
+	unsigned input_size = shader->input_size + 36;
 	uint32_t * num_work_groups_start;
 	uint32_t * global_size_start;
 	uint32_t * local_size_start;
@@ -200,12 +221,9 @@ void evergreen_compute_upload_input(
 	}
 
 	if (!shader->kernel_param) {
-		unsigned buffer_size = shader->input_size;
-
 		/* Add space for the grid dimensions */
-		buffer_size += kernel_parameters_offset_bytes;
 		shader->kernel_param = r600_compute_buffer_alloc_vram(
-						ctx->screen, buffer_size);
+						ctx->screen, input_size);
 	}
 
 	num_work_groups_start = r600_buffer_mmap_sync_with_rings(ctx, shader->kernel_param, PIPE_TRANSFER_WRITE);
@@ -227,20 +245,16 @@ void evergreen_compute_upload_input(
 	/* Copy the kernel inputs */
 	memcpy(kernel_parameters_start, input, shader->input_size);
 
-	for (i = 0; i < (kernel_parameters_offset_bytes / 4) +
-					(shader->input_size / 4); i++) {
+	for (i = 0; i < (input_size / 4); i++) {
 		COMPUTE_DBG(ctx->screen, "input %i : %i\n", i,
 			((unsigned*)num_work_groups_start)[i]);
 	}
 
 	ctx->ws->buffer_unmap(shader->kernel_param->cs_buf);
 
-	///ID=0 is reserved for the parameters
-	evergreen_cs_set_vertex_buffer(ctx, 0, 0,
+	/* ID=0 is reserved for the parameters */
+	evergreen_cs_set_constant_buffer(ctx, 0, 0, input_size,
 			(struct pipe_resource*)shader->kernel_param);
-	///ID=0 is reserved for parameters
-	evergreen_set_const_cache(shader, 0, shader->kernel_param,
-						shader->input_size, 0);
 }
 
 static void evergreen_emit_direct_dispatch(
@@ -369,6 +383,9 @@ static void compute_emit_cs(struct r600_context *ctx, const uint *block_layout,
 	ctx->cs_vertex_buffer_state.atom.num_dw = 12 * util_bitcount(ctx->cs_vertex_buffer_state.dirty_mask);
 	r600_emit_atom(ctx, &ctx->cs_vertex_buffer_state.atom);
 
+	/* Emit constant buffer state */
+	r600_emit_atom(ctx, &ctx->constbuf_state[PIPE_SHADER_COMPUTE].atom);
+
 	/* Emit compute shader state */
 	r600_emit_atom(ctx, &ctx->cs_shader_state.atom);
 
@@ -783,10 +800,9 @@ void evergreen_init_compute_state_functions(struct r600_context *ctx)
 	ctx->context.set_global_binding = evergreen_set_global_binding;
 	ctx->context.launch_grid = evergreen_launch_grid;
 
-	/* We always use at least two vertex buffers for compute, one for
-         * parameters and one for global memory */
+	/* We always use at least one vertex buffer for parameters (id = 1)*/
 	ctx->cs_vertex_buffer_state.enabled_mask =
-	ctx->cs_vertex_buffer_state.dirty_mask = 1 | 2;
+	ctx->cs_vertex_buffer_state.dirty_mask = 0x2;
 }
 
 
diff --git a/src/gallium/drivers/r600/evergreen_compute_internal.h b/src/gallium/drivers/r600/evergreen_compute_internal.h
index b1a180f..463cc7d 100644
--- a/src/gallium/drivers/r600/evergreen_compute_internal.h
+++ b/src/gallium/drivers/r600/evergreen_compute_internal.h
@@ -113,7 +113,7 @@ void evergreen_set_loop_const(struct r600_pipe_compute *pipe, int id, int count,
 void evergreen_set_tmp_ring(struct r600_pipe_compute *pipe, struct r600_resource* bo, int offset, int size, int se);
 void evergreen_set_tex_resource(struct r600_pipe_compute *pipe, struct r600_pipe_sampler_view* view, int id);
 void evergreen_set_sampler_resource(struct r600_pipe_compute *pipe, struct compute_sampler_state *sampler, int id);
-void evergreen_set_const_cache(struct r600_pipe_compute *pipe, int cache_id, struct r600_resource* cbo, int size, int offset);
+void evergreen_set_const_cache(struct r600_pipe_compute *pipe, int cache_id, struct r600_resource* cbo, int size);
 
 struct r600_resource* r600_compute_buffer_alloc_vram(struct r600_screen *screen, unsigned size);
 
-- 
1.7.11.4



More information about the mesa-dev mailing list