anv: pCreateInfo->pApplicationInfo parameter to vkCreateInstance may be NULL
[mesa.git] / src / vulkan / anv_nir_apply_pipeline_layout.c
index 1b196cd62b782b14ff0ea771dcf0399323e463ca..ee93e40e76c278e79a6b51a124df3e4c7fce9e13 100644 (file)
@@ -22,7 +22,8 @@
  */
 
 #include "anv_nir.h"
-#include "glsl/nir/nir_builder.h"
+#include "program/prog_parameter.h"
+#include "nir/nir_builder.h"
 
 struct apply_pipeline_layout_state {
    nir_shader *shader;
@@ -57,21 +58,18 @@ get_surface_index(unsigned set, unsigned binding,
 }
 
 static uint32_t
-get_sampler_index(unsigned set, unsigned binding, nir_texop tex_op,
+get_sampler_index(unsigned set, unsigned binding,
                   struct apply_pipeline_layout_state *state)
 {
    assert(set < state->layout->num_sets);
    struct anv_descriptor_set_layout *set_layout =
       state->layout->set[set].layout;
 
-   assert(binding < set_layout->binding_count);
-
    gl_shader_stage stage = state->shader->stage;
 
-   if (set_layout->binding[binding].stage[stage].sampler_index < 0) {
-      assert(tex_op == nir_texop_txf);
-      return 0;
-   }
+   assert(binding < set_layout->binding_count);
+
+   assert(set_layout->binding[binding].stage[stage].sampler_index >= 0);
 
    uint32_t sampler_index =
       state->layout->set[set].stage[stage].sampler_start +
@@ -82,6 +80,29 @@ get_sampler_index(unsigned set, unsigned binding, nir_texop tex_op,
    return sampler_index;
 }
 
+static uint32_t
+get_image_index(unsigned set, unsigned binding,
+                struct apply_pipeline_layout_state *state)
+{
+   assert(set < state->layout->num_sets);
+   struct anv_descriptor_set_layout *set_layout =
+      state->layout->set[set].layout;
+
+   assert(binding < set_layout->binding_count);
+
+   gl_shader_stage stage = state->shader->stage;
+
+   assert(set_layout->binding[binding].stage[stage].image_index >= 0);
+
+   uint32_t image_index =
+      state->layout->set[set].stage[stage].image_start +
+      set_layout->binding[binding].stage[stage].image_index;
+
+   assert(image_index < state->layout->stage[stage].image_count);
+
+   return image_index;
+}
+
 static void
 lower_res_index_intrinsic(nir_intrinsic_instr *intrin,
                           struct apply_pipeline_layout_state *state)
@@ -90,8 +111,8 @@ lower_res_index_intrinsic(nir_intrinsic_instr *intrin,
 
    b->cursor = nir_before_instr(&intrin->instr);
 
-   uint32_t set = intrin->const_index[0];
-   uint32_t binding = intrin->const_index[1];
+   uint32_t set = nir_intrinsic_desc_set(intrin);
+   uint32_t binding = nir_intrinsic_binding(intrin);
 
    uint32_t surface_index = get_surface_index(set, binding, state);
 
@@ -164,24 +185,30 @@ static void
 lower_tex(nir_tex_instr *tex, struct apply_pipeline_layout_state *state)
 {
    /* No one should have come by and lowered it already */
-   assert(tex->sampler);
+   assert(tex->texture);
 
-   nir_deref_var *tex_deref = tex->texture ? tex->texture : tex->sampler;
    tex->texture_index =
-      get_surface_index(tex_deref->var->data.descriptor_set,
-                        tex_deref->var->data.binding, state);
-   lower_tex_deref(tex, tex_deref, &tex->texture_index,
+      get_surface_index(tex->texture->var->data.descriptor_set,
+                        tex->texture->var->data.binding, state);
+   lower_tex_deref(tex, tex->texture, &tex->texture_index,
                    nir_tex_src_texture_offset, state);
 
-   tex->sampler_index =
-      get_sampler_index(tex->sampler->var->data.descriptor_set,
-                        tex->sampler->var->data.binding, tex->op, state);
-   lower_tex_deref(tex, tex->sampler, &tex->sampler_index,
-                   nir_tex_src_sampler_offset, state);
+   if (tex->sampler) {
+      tex->sampler_index =
+         get_sampler_index(tex->sampler->var->data.descriptor_set,
+                           tex->sampler->var->data.binding, state);
+      lower_tex_deref(tex, tex->sampler, &tex->sampler_index,
+                      nir_tex_src_sampler_offset, state);
+   }
+
+   /* The backend only ever uses this to mark used surfaces.  We don't care
+    * about that little optimization so it just needs to be non-zero.
+    */
+   tex->texture_array_size = 1;
 
-   if (tex->texture)
-      cleanup_tex_deref(tex, tex->texture);
-   cleanup_tex_deref(tex, tex->sampler);
+   cleanup_tex_deref(tex, tex->texture);
+   if (tex->sampler)
+      cleanup_tex_deref(tex, tex->sampler);
    tex->texture = NULL;
    tex->sampler = NULL;
 }
@@ -214,8 +241,23 @@ apply_pipeline_layout_block(nir_block *block, void *void_state)
    return true;
 }
 
+static void
+setup_vec4_uniform_value(const union gl_constant_value **params,
+                         const union gl_constant_value *values,
+                         unsigned n)
+{
+   static const gl_constant_value zero = { 0 };
+
+   for (unsigned i = 0; i < n; ++i)
+      params[i] = &values[i];
+
+   for (unsigned i = n; i < 4; ++i)
+      params[i] = &zero;
+}
+
 bool
 anv_nir_apply_pipeline_layout(nir_shader *shader,
+                              struct brw_stage_prog_data *prog_data,
                               const struct anv_pipeline_layout *layout)
 {
    struct apply_pipeline_layout_state state = {
@@ -223,14 +265,56 @@ anv_nir_apply_pipeline_layout(nir_shader *shader,
       .layout = layout,
    };
 
-   nir_foreach_overload(shader, overload) {
-      if (overload->impl) {
-         nir_builder_init(&state.builder, overload->impl);
-         nir_foreach_block(overload->impl, apply_pipeline_layout_block, &state);
-         nir_metadata_preserve(overload->impl, nir_metadata_block_index |
+   nir_foreach_function(shader, function) {
+      if (function->impl) {
+         nir_builder_init(&state.builder, function->impl);
+         nir_foreach_block(function->impl, apply_pipeline_layout_block, &state);
+         nir_metadata_preserve(function->impl, nir_metadata_block_index |
                                                nir_metadata_dominance);
       }
    }
 
+   if (layout->stage[shader->stage].image_count > 0) {
+      nir_foreach_variable(var, &shader->uniforms) {
+         if (glsl_type_is_image(var->type) ||
+             (glsl_type_is_array(var->type) &&
+              glsl_type_is_image(glsl_get_array_element(var->type)))) {
+            /* Images are represented as uniform push constants and the actual
+             * information required for reading/writing to/from the image is
+             * storred in the uniform.
+             */
+            unsigned image_index = get_image_index(var->data.descriptor_set,
+                                                   var->data.binding, &state);
+
+            var->data.driver_location = shader->num_uniforms +
+                                        image_index * BRW_IMAGE_PARAM_SIZE * 4;
+         }
+      }
+
+      struct anv_push_constants *null_data = NULL;
+      const gl_constant_value **param = prog_data->param + shader->num_uniforms;
+      const struct brw_image_param *image_param = null_data->images;
+      for (uint32_t i = 0; i < layout->stage[shader->stage].image_count; i++) {
+         setup_vec4_uniform_value(param + BRW_IMAGE_PARAM_SURFACE_IDX_OFFSET,
+            (const union gl_constant_value *)&image_param->surface_idx, 1);
+         setup_vec4_uniform_value(param + BRW_IMAGE_PARAM_OFFSET_OFFSET,
+            (const union gl_constant_value *)image_param->offset, 2);
+         setup_vec4_uniform_value(param + BRW_IMAGE_PARAM_SIZE_OFFSET,
+            (const union gl_constant_value *)image_param->size, 3);
+         setup_vec4_uniform_value(param + BRW_IMAGE_PARAM_STRIDE_OFFSET,
+            (const union gl_constant_value *)image_param->stride, 4);
+         setup_vec4_uniform_value(param + BRW_IMAGE_PARAM_TILING_OFFSET,
+            (const union gl_constant_value *)image_param->tiling, 3);
+         setup_vec4_uniform_value(param + BRW_IMAGE_PARAM_SWIZZLING_OFFSET,
+            (const union gl_constant_value *)image_param->swizzling, 2);
+
+         param += BRW_IMAGE_PARAM_SIZE;
+         image_param ++;
+      }
+
+      shader->num_uniforms += layout->stage[shader->stage].image_count *
+                              BRW_IMAGE_PARAM_SIZE * 4;
+   }
+
    return state.progress;
 }