freedreno/ir3: split out regmask
[mesa.git] / src / freedreno / ir3 / ir3_image.c
index bc564aac402be8cf35d90bb4509905aa6d07d090..b89f74d5de91ca989ad5c339f6a6dde85f269665 100644 (file)
 
 #include "ir3_image.h"
 
-/* Images get mapped into SSBO/image state (for store/atomic) and texture
- * state block (for load).  To simplify things, invert the image id and
- * map it from end of state block, ie. image 0 becomes num-1, image 1
- * becomes num-2, etc.  This potentially avoids needing to re-emit texture
- * state when switching shaders.
- *
- * TODO is max # of samplers and SSBOs the same.  This shouldn't be hard-
- * coded.  Also, since all the gl shader stages (ie. everything but CS)
- * share the same SSBO/image state block, this might require some more
- * logic if we supported images in anything other than FS..
- */
-unsigned
-ir3_get_image_slot(struct ir3_context *ctx, nir_deref_instr *deref)
-{
-       unsigned int loc = 0;
-       unsigned inner_size = 1;
 
-       while (deref->deref_type != nir_deref_type_var) {
-               assert(deref->deref_type == nir_deref_type_array);
-               nir_const_value *const_index = nir_src_as_const_value(deref->arr.index);
-               assert(const_index);
+/*
+ * SSBO/Image to/from IBO/tex hw mapping table:
+ */
 
-               /* Go to the next instruction */
-               deref = nir_deref_instr_parent(deref);
+void
+ir3_ibo_mapping_init(struct ir3_ibo_mapping *mapping, unsigned num_textures)
+{
+       memset(mapping, IBO_INVALID, sizeof(*mapping));
+       mapping->num_tex = 0;
+       mapping->tex_base = num_textures;
+}
 
-               assert(glsl_type_is_array(deref->type));
-               const unsigned array_len = glsl_get_length(deref->type);
-               loc += MIN2(const_index->u32[0], array_len - 1) * inner_size;
+struct ir3_instruction *
+ir3_ssbo_to_ibo(struct ir3_context *ctx, nir_src src)
+{
+       if (ir3_bindless_resource(src)) {
+               ctx->so->bindless_ibo = true;
+               return ir3_get_src(ctx, &src)[0];
+       } else {
+               /* can this be non-const buffer_index?  how do we handle that? */
+               int ssbo_idx = nir_src_as_uint(src);
+               return create_immed(ctx->block, ssbo_idx);
+       }
+}
 
-               /* Update the inner size */
-               inner_size *= array_len;
+unsigned
+ir3_ssbo_to_tex(struct ir3_ibo_mapping *mapping, unsigned ssbo)
+{
+       if (mapping->ssbo_to_tex[ssbo] == IBO_INVALID) {
+               unsigned tex = mapping->num_tex++;
+               mapping->ssbo_to_tex[ssbo] = tex;
+               mapping->tex_to_image[tex] = IBO_SSBO | ssbo;
        }
+       return mapping->ssbo_to_tex[ssbo] + mapping->tex_base;
+}
 
-       loc += deref->var->data.driver_location;
+struct ir3_instruction *
+ir3_image_to_ibo(struct ir3_context *ctx, nir_src src)
+{
+       if (ir3_bindless_resource(src)) {
+               ctx->so->bindless_ibo = true;
+               return ir3_get_src(ctx, &src)[0];
+       } else {
+               /* can this be non-const buffer_index?  how do we handle that? */
+               int image_idx = nir_src_as_uint(src);
+               return create_immed(ctx->block, ctx->s->info.num_ssbos + image_idx);
+       }
+}
 
-       /* TODO figure out real limit per generation, and don't hardcode: */
-       const unsigned max_samplers = 16;
-       return max_samplers - loc - 1;
+unsigned
+ir3_image_to_tex(struct ir3_ibo_mapping *mapping, unsigned image)
+{
+       if (mapping->image_to_tex[image] == IBO_INVALID) {
+               unsigned tex = mapping->num_tex++;
+               mapping->image_to_tex[image] = tex;
+               mapping->tex_to_image[tex] = image;
+       }
+       return mapping->image_to_tex[image] + mapping->tex_base;
 }
 
 /* see tex_info() for equiv logic for texture instructions.. it would be
  * nice if this could be better unified..
  */
 unsigned
-ir3_get_image_coords(const nir_variable *var, unsigned *flagsp)
+ir3_get_image_coords(const nir_intrinsic_instr *instr, unsigned *flagsp)
 {
-       const struct glsl_type *type = glsl_without_array(var->type);
-       unsigned coords, flags = 0;
-
-       switch (glsl_get_sampler_dim(type)) {
-       case GLSL_SAMPLER_DIM_1D:
-       case GLSL_SAMPLER_DIM_BUF:
-               coords = 1;
-               break;
-       case GLSL_SAMPLER_DIM_2D:
-       case GLSL_SAMPLER_DIM_RECT:
-       case GLSL_SAMPLER_DIM_EXTERNAL:
-       case GLSL_SAMPLER_DIM_MS:
-               coords = 2;
-               break;
-       case GLSL_SAMPLER_DIM_3D:
-       case GLSL_SAMPLER_DIM_CUBE:
+       unsigned coords = nir_image_intrinsic_coord_components(instr);
+       unsigned flags = 0;
+
+       if (coords == 3)
                flags |= IR3_INSTR_3D;
-               coords = 3;
-               break;
-       default:
-               unreachable("bad sampler dim");
-               return 0;
-       }
 
-       if (glsl_sampler_type_is_array(type)) {
-               /* note: unlike tex_info(), adjust # of coords to include array idx: */
-               coords++;
+       if (nir_intrinsic_image_array(instr))
                flags |= IR3_INSTR_A;
-       }
 
        if (flagsp)
                *flagsp = flags;
@@ -109,19 +109,18 @@ ir3_get_image_coords(const nir_variable *var, unsigned *flagsp)
 }
 
 type_t
-ir3_get_image_type(const nir_variable *var)
+ir3_get_type_for_image_intrinsic(const nir_intrinsic_instr *instr)
 {
-       switch (glsl_get_sampler_result_type(glsl_without_array(var->type))) {
-       case GLSL_TYPE_UINT:
-               return TYPE_U32;
-       case GLSL_TYPE_INT:
-               return TYPE_S32;
-       case GLSL_TYPE_FLOAT:
-               return TYPE_F32;
-       default:
-               unreachable("bad sampler type.");
-               return 0;
-       }
+       const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
+       int bit_size = info->has_dest ? nir_dest_bit_size(instr->dest) : 32;
+       enum pipe_format format = nir_intrinsic_format(instr);
+
+       if (util_format_is_pure_uint(format))
+               return bit_size == 16 ? TYPE_U16 : TYPE_U32;
+       else if (util_format_is_pure_sint(format))
+               return bit_size == 16 ? TYPE_S16 : TYPE_S32;
+       else
+               return bit_size == 16 ? TYPE_F16 : TYPE_F32;
 }
 
 /* Returns the number of components for the different image formats
@@ -129,69 +128,10 @@ ir3_get_image_type(const nir_variable *var)
  * GL_NV_image_formats extension.
  */
 unsigned
-ir3_get_num_components_for_glformat(GLuint format)
+ir3_get_num_components_for_image_format(enum pipe_format format)
 {
-       switch (format) {
-       case GL_R32F:
-       case GL_R32I:
-       case GL_R32UI:
-       case GL_R16F:
-       case GL_R16I:
-       case GL_R16UI:
-       case GL_R16:
-       case GL_R16_SNORM:
-       case GL_R8I:
-       case GL_R8UI:
-       case GL_R8:
-       case GL_R8_SNORM:
-               return 1;
-
-       case GL_RG32F:
-       case GL_RG32I:
-       case GL_RG32UI:
-       case GL_RG16F:
-       case GL_RG16I:
-       case GL_RG16UI:
-       case GL_RG16:
-       case GL_RG16_SNORM:
-       case GL_RG8I:
-       case GL_RG8UI:
-       case GL_RG8:
-       case GL_RG8_SNORM:
-               return 2;
-
-       case GL_R11F_G11F_B10F:
-               return 3;
-
-       case GL_RGBA32F:
-       case GL_RGBA32I:
-       case GL_RGBA32UI:
-       case GL_RGBA16F:
-       case GL_RGBA16I:
-       case GL_RGBA16UI:
-       case GL_RGBA16:
-       case GL_RGBA16_SNORM:
-       case GL_RGBA8I:
-       case GL_RGBA8UI:
-       case GL_RGBA8:
-       case GL_RGBA8_SNORM:
-       case GL_RGB10_A2UI:
-       case GL_RGB10_A2:
-               return 4;
-
-       case GL_NONE:
-               /* Omitting the image format qualifier is allowed on desktop GL
-                * profiles. Assuming 4 components is always safe.
-                */
+       if (format == PIPE_FORMAT_NONE)
                return 4;
-
-       default:
-               /* Return 4 components also for all other formats we don't know
-                * about. The format should have been validated already by
-                * the higher level API, but drop a debug message just in case.
-                */
-               debug_printf("Unhandled GL format %u while emitting imageStore()\n",
-                                        format);
-               return 4;
-       }
+       else
+               return util_format_get_nr_components(format);
 }