radeonsi: implement texture buffer objects
authorMarek Olšák <marek.olsak@amd.com>
Thu, 31 Oct 2013 14:08:49 +0000 (15:08 +0100)
committerMarek Olšák <marek.olsak@amd.com>
Mon, 4 Nov 2013 18:07:57 +0000 (19:07 +0100)
GLSL 1.40 is done.

src/gallium/drivers/radeonsi/radeonsi_pipe.c
src/gallium/drivers/radeonsi/radeonsi_shader.c
src/gallium/drivers/radeonsi/si_state.c

index f222f7d66bdb0f39794b78c8c24993678850a64c..b79a58ebb3ec982f5b170860ce4655f019d36857 100644 (file)
@@ -327,6 +327,7 @@ static int r600_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
         case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
        case PIPE_CAP_TGSI_INSTANCEID:
        case PIPE_CAP_COMPUTE:
+       case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
                return 1;
 
        case PIPE_CAP_TEXTURE_MULTISAMPLE:
@@ -342,7 +343,12 @@ static int r600_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
                return 256;
 
        case PIPE_CAP_GLSL_FEATURE_LEVEL:
-               return 130;
+               return 140;
+
+       case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
+               return 1;
+       case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
+               return MIN2(rscreen->b.info.vram_size, 0xFFFFFFFF);
 
        /* Unsupported features. */
        case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
@@ -355,9 +361,6 @@ static int r600_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
        case PIPE_CAP_USER_VERTEX_BUFFERS:
        case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
        case PIPE_CAP_CUBE_MAP_ARRAY:
-       case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
-       case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
-        case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
                return 0;
 
        case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK:
index 352ff194cb291192cb8b412e013ff9c8f0acff61..61e619ad810ba4a5fa2c404d9054275e3eec7f87 100644 (file)
@@ -1189,13 +1189,34 @@ static void tex_fetch_args(
        const struct tgsi_full_instruction * inst = emit_data->inst;
        unsigned opcode = inst->Instruction.Opcode;
        unsigned target = inst->Texture.Texture;
-       unsigned sampler_src, sampler_index;
        LLVMValueRef coords[4];
        LLVMValueRef address[16];
        int ref_pos;
        unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
        unsigned count = 0;
        unsigned chan;
+       unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
+       unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
+
+       if (target == TGSI_TEXTURE_BUFFER) {
+               LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
+               LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
+               LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
+               LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
+
+               /* Truncate v32i8 to v16i8. */
+               LLVMValueRef res = si_shader_ctx->resources[sampler_index];
+               res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
+               res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
+               res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
+
+               emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
+               emit_data->args[0] = res;
+               emit_data->args[1] = bld_base->uint_bld.zero;
+               emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
+               emit_data->arg_count = 3;
+               return;
+       }
 
        /* Fetch and project texture coordinates */
        coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
@@ -1267,9 +1288,6 @@ static void tex_fetch_args(
                                                 "");
        }
 
-       sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
-       sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
-
        /* Adjust the sample index according to FMASK.
         *
         * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
@@ -1430,6 +1448,15 @@ static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
        struct lp_build_context * base = &bld_base->base;
        char intr_name[127];
 
+       if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
+               emit_data->output[emit_data->chan] = build_intrinsic(
+                       base->gallivm->builder,
+                       "llvm.SI.vs.load.input", emit_data->dst_type,
+                       emit_data->args, emit_data->arg_count,
+                       LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
+               return;
+       }
+
        sprintf(intr_name, "%sv%ui32", action->intr_name,
                LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
 
@@ -1445,6 +1472,20 @@ static void txq_fetch_args(
 {
        struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
        const struct tgsi_full_instruction *inst = emit_data->inst;
+       struct gallivm_state *gallivm = bld_base->base.gallivm;
+
+       if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
+               LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
+               LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
+
+               /* Read the size from the buffer descriptor directly. */
+               LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
+               size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
+               size = LLVMBuildExtractElement(gallivm->builder, size,
+                                             lp_build_const_int32(gallivm, 2), "");
+               emit_data->args[0] = size;
+               return;
+       }
 
        /* Mip level */
        emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
@@ -1463,6 +1504,19 @@ static void txq_fetch_args(
                4);
 }
 
+static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
+                               struct lp_build_tgsi_context * bld_base,
+                               struct lp_build_emit_data * emit_data)
+{
+       if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
+               /* Just return the buffer size. */
+               emit_data->output[emit_data->chan] = emit_data->args[0];
+               return;
+       }
+
+       build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
+}
+
 #if HAVE_LLVM >= 0x0304
 
 static void si_llvm_emit_ddxy(
@@ -1569,7 +1623,7 @@ static const struct lp_build_tgsi_action txl_action = {
 
 static const struct lp_build_tgsi_action txq_action = {
        .fetch_args = txq_fetch_args,
-       .emit = build_tgsi_intrinsic_nomem,
+       .emit = build_txq_intrinsic,
        .intr_name = "llvm.SI.resinfo"
 };
 
index f6400d8c51a87e5e6a6f5694fc580bc70888dba5..36aff008057007043575912762d5b151d7017299 100644 (file)
@@ -1352,10 +1352,9 @@ static bool si_is_sampler_format_supported(struct pipe_screen *screen, enum pipe
                                      util_format_get_first_non_void_channel(format)) != ~0U;
 }
 
-static uint32_t si_translate_vertexformat(struct pipe_screen *screen,
-                                         enum pipe_format format,
-                                         const struct util_format_description *desc,
-                                         int first_non_void)
+static uint32_t si_translate_buffer_dataformat(struct pipe_screen *screen,
+                                              const struct util_format_description *desc,
+                                              int first_non_void)
 {
        unsigned type = desc->channel[first_non_void].type;
        int i;
@@ -1417,6 +1416,33 @@ static uint32_t si_translate_vertexformat(struct pipe_screen *screen,
        return V_008F0C_BUF_DATA_FORMAT_INVALID;
 }
 
+static uint32_t si_translate_buffer_numformat(struct pipe_screen *screen,
+                                             const struct util_format_description *desc,
+                                             int first_non_void)
+{
+       switch (desc->channel[first_non_void].type) {
+       case UTIL_FORMAT_TYPE_SIGNED:
+               if (desc->channel[first_non_void].normalized)
+                       return V_008F0C_BUF_NUM_FORMAT_SNORM;
+               else if (desc->channel[first_non_void].pure_integer)
+                       return V_008F0C_BUF_NUM_FORMAT_SINT;
+               else
+                       return V_008F0C_BUF_NUM_FORMAT_SSCALED;
+               break;
+       case UTIL_FORMAT_TYPE_UNSIGNED:
+               if (desc->channel[first_non_void].normalized)
+                       return V_008F0C_BUF_NUM_FORMAT_UNORM;
+               else if (desc->channel[first_non_void].pure_integer)
+                       return V_008F0C_BUF_NUM_FORMAT_UINT;
+               else
+                       return V_008F0C_BUF_NUM_FORMAT_USCALED;
+               break;
+       case UTIL_FORMAT_TYPE_FLOAT:
+       default:
+               return V_008F0C_BUF_NUM_FORMAT_FLOAT;
+       }
+}
+
 static bool si_is_vertex_format_supported(struct pipe_screen *screen, enum pipe_format format)
 {
        const struct util_format_description *desc;
@@ -1425,7 +1451,7 @@ static bool si_is_vertex_format_supported(struct pipe_screen *screen, enum pipe_
 
        desc = util_format_description(format);
        first_non_void = util_format_get_first_non_void_channel(format);
-       data_format = si_translate_vertexformat(screen, format, desc, first_non_void);
+       data_format = si_translate_buffer_dataformat(screen, desc, first_non_void);
        return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID;
 }
 
@@ -2335,10 +2361,34 @@ static struct pipe_sampler_view *si_create_sampler_view(struct pipe_context *ctx
        /* initialize base object */
        view->base = *state;
        view->base.texture = NULL;
-       pipe_reference(NULL, &texture->reference);
-       view->base.texture = texture;
+       pipe_resource_reference(&view->base.texture, texture);
        view->base.reference.count = 1;
        view->base.context = ctx;
+       view->resource = &tmp->resource;
+
+       /* Buffer resource. */
+       if (texture->target == PIPE_BUFFER) {
+               unsigned stride;
+
+               desc = util_format_description(state->format);
+               first_non_void = util_format_get_first_non_void_channel(state->format);
+               stride = desc->block.bits / 8;
+               va = r600_resource_va(ctx->screen, texture) + state->u.buf.first_element*stride;
+               format = si_translate_buffer_dataformat(ctx->screen, desc, first_non_void);
+               num_format = si_translate_buffer_numformat(ctx->screen, desc, first_non_void);
+
+               view->state[0] = va;
+               view->state[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
+                                S_008F04_STRIDE(stride);
+               view->state[2] = state->u.buf.last_element + 1 - state->u.buf.first_element;
+               view->state[3] = S_008F0C_DST_SEL_X(si_map_swizzle(desc->swizzle[0])) |
+                                S_008F0C_DST_SEL_Y(si_map_swizzle(desc->swizzle[1])) |
+                                S_008F0C_DST_SEL_Z(si_map_swizzle(desc->swizzle[2])) |
+                                S_008F0C_DST_SEL_W(si_map_swizzle(desc->swizzle[3])) |
+                                S_008F0C_NUM_FORMAT(num_format) |
+                                S_008F0C_DATA_FORMAT(format);
+               return &view->base;
+       }
 
        state_swizzle[0] = state->swizzle_r;
        state_swizzle[1] = state->swizzle_g;
@@ -2450,8 +2500,6 @@ static struct pipe_sampler_view *si_create_sampler_view(struct pipe_context *ctx
                format = 0;
        }
 
-       view->resource = &tmp->resource;
-
        /* not supported any more */
        //endian = si_colorformat_endian_swap(format);
 
@@ -2625,7 +2673,18 @@ static void si_set_sampler_views(struct pipe_context *ctx,
        assert(start == 0);
 
        for (i = 0; i < count; i++) {
-               if (views[i]) {
+               if (!views[i]) {
+                       samplers->depth_texture_mask &= ~(1 << i);
+                       samplers->compressed_colortex_mask &= ~(1 << i);
+                       si_set_sampler_view(rctx, shader, i, NULL, NULL);
+                       si_set_sampler_view(rctx, shader, FMASK_TEX_OFFSET + i,
+                                           NULL, NULL);
+                       continue;
+               }
+
+               si_set_sampler_view(rctx, shader, i, views[i], rviews[i]->state);
+
+               if (views[i]->texture->target != PIPE_BUFFER) {
                        struct r600_texture *rtex =
                                (struct r600_texture*)views[i]->texture;
 
@@ -2640,8 +2699,6 @@ static void si_set_sampler_views(struct pipe_context *ctx,
                                samplers->compressed_colortex_mask &= ~(1 << i);
                        }
 
-                       si_set_sampler_view(rctx, shader, i, views[i], rviews[i]->state);
-
                        if (rtex->fmask.size) {
                                si_set_sampler_view(rctx, shader, FMASK_TEX_OFFSET + i,
                                                    views[i], rviews[i]->fmask_state);
@@ -2649,12 +2706,6 @@ static void si_set_sampler_views(struct pipe_context *ctx,
                                si_set_sampler_view(rctx, shader, FMASK_TEX_OFFSET + i,
                                                    NULL, NULL);
                        }
-               } else {
-                       samplers->depth_texture_mask &= ~(1 << i);
-                       samplers->compressed_colortex_mask &= ~(1 << i);
-                       si_set_sampler_view(rctx, shader, i, NULL, NULL);
-                       si_set_sampler_view(rctx, shader, FMASK_TEX_OFFSET + i,
-                                           NULL, NULL);
                }
        }
        for (; i < samplers->n_views; i++) {
@@ -2827,33 +2878,8 @@ static void *si_create_vertex_elements(struct pipe_context *ctx,
 
                desc = util_format_description(elements[i].src_format);
                first_non_void = util_format_get_first_non_void_channel(elements[i].src_format);
-               data_format = si_translate_vertexformat(ctx->screen, elements[i].src_format,
-                                                       desc, first_non_void);
-
-               switch (desc->channel[first_non_void].type) {
-               case UTIL_FORMAT_TYPE_FIXED:
-                       num_format = V_008F0C_BUF_NUM_FORMAT_USCALED; /* XXX */
-                       break;
-               case UTIL_FORMAT_TYPE_SIGNED:
-                       if (desc->channel[first_non_void].normalized)
-                               num_format = V_008F0C_BUF_NUM_FORMAT_SNORM;
-                       else if (desc->channel[first_non_void].pure_integer)
-                               num_format = V_008F0C_BUF_NUM_FORMAT_SINT;
-                       else
-                               num_format = V_008F0C_BUF_NUM_FORMAT_SSCALED;
-                       break;
-               case UTIL_FORMAT_TYPE_UNSIGNED:
-                       if (desc->channel[first_non_void].normalized)
-                               num_format = V_008F0C_BUF_NUM_FORMAT_UNORM;
-                       else if (desc->channel[first_non_void].pure_integer)
-                               num_format = V_008F0C_BUF_NUM_FORMAT_UINT;
-                       else
-                               num_format = V_008F0C_BUF_NUM_FORMAT_USCALED;
-                       break;
-               case UTIL_FORMAT_TYPE_FLOAT:
-               default:
-                       num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
-               }
+               data_format = si_translate_buffer_dataformat(ctx->screen, desc, first_non_void);
+               num_format = si_translate_buffer_numformat(ctx->screen, desc, first_non_void);
 
                v->rsrc_word3[i] = S_008F0C_DST_SEL_X(si_map_swizzle(desc->swizzle[0])) |
                                   S_008F0C_DST_SEL_Y(si_map_swizzle(desc->swizzle[1])) |