Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

anv_nir_apply_dynamic_offsets.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright © 2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include "anv_nir.h"
  24. #include "glsl/nir/nir_builder.h"
  25. struct apply_dynamic_offsets_state {
  26. nir_shader *shader;
  27. nir_builder builder;
  28. VkShaderStage stage;
  29. struct anv_pipeline_layout *layout;
  30. uint32_t indices_start;
  31. };
  32. static bool
  33. apply_dynamic_offsets_block(nir_block *block, void *void_state)
  34. {
  35. struct apply_dynamic_offsets_state *state = void_state;
  36. struct anv_descriptor_set_layout *set_layout;
  37. const struct anv_descriptor_slot *slot;
  38. nir_foreach_instr_safe(block, instr) {
  39. if (instr->type != nir_instr_type_intrinsic)
  40. continue;
  41. nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
  42. bool has_indirect = false;
  43. uint32_t set, binding;
  44. switch (intrin->intrinsic) {
  45. case nir_intrinsic_load_ubo_indirect:
  46. has_indirect = true;
  47. /* fallthrough */
  48. case nir_intrinsic_load_ubo: {
  49. set = intrin->const_index[0];
  50. nir_const_value *const_binding = nir_src_as_const_value(intrin->src[0]);
  51. if (const_binding) {
  52. binding = const_binding->u[0];
  53. } else {
  54. assert(0 && "need more info from the ir for this.");
  55. }
  56. break;
  57. }
  58. default:
  59. continue; /* the loop */
  60. }
  61. set_layout = state->layout->set[set].layout;
  62. slot = &set_layout->stage[state->stage].surface_start[binding];
  63. if (slot->dynamic_slot < 0)
  64. continue;
  65. uint32_t dynamic_index = state->layout->set[set].dynamic_offset_start +
  66. slot->dynamic_slot;
  67. state->builder.cursor = nir_before_instr(&intrin->instr);
  68. nir_intrinsic_instr *offset_load =
  69. nir_intrinsic_instr_create(state->shader, nir_intrinsic_load_uniform);
  70. offset_load->num_components = 1;
  71. offset_load->const_index[0] = state->indices_start + dynamic_index;
  72. offset_load->const_index[1] = 0;
  73. nir_ssa_dest_init(&offset_load->instr, &offset_load->dest, 1, NULL);
  74. nir_builder_instr_insert(&state->builder, &offset_load->instr);
  75. nir_ssa_def *offset = &offset_load->dest.ssa;
  76. if (has_indirect) {
  77. assert(intrin->src[1].is_ssa);
  78. offset = nir_iadd(&state->builder, intrin->src[1].ssa, offset);
  79. }
  80. assert(intrin->dest.is_ssa);
  81. nir_intrinsic_instr *new_load =
  82. nir_intrinsic_instr_create(state->shader,
  83. nir_intrinsic_load_ubo_indirect);
  84. new_load->num_components = intrin->num_components;
  85. new_load->const_index[0] = intrin->const_index[0];
  86. new_load->const_index[1] = intrin->const_index[1];
  87. nir_src_copy(&new_load->src[0], &intrin->src[0], &new_load->instr);
  88. new_load->src[1] = nir_src_for_ssa(offset);
  89. nir_ssa_dest_init(&new_load->instr, &new_load->dest,
  90. intrin->dest.ssa.num_components,
  91. intrin->dest.ssa.name);
  92. nir_builder_instr_insert(&state->builder, &new_load->instr);
  93. nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
  94. nir_src_for_ssa(&new_load->dest.ssa));
  95. nir_instr_remove(&intrin->instr);
  96. }
  97. return true;
  98. }
  99. void
  100. anv_nir_apply_dynamic_offsets(struct anv_pipeline *pipeline,
  101. nir_shader *shader,
  102. struct brw_stage_prog_data *prog_data)
  103. {
  104. struct apply_dynamic_offsets_state state = {
  105. .shader = shader,
  106. .stage = anv_vk_shader_stage_for_mesa_stage(shader->stage),
  107. .layout = pipeline->layout,
  108. .indices_start = shader->num_uniforms,
  109. };
  110. if (!state.layout || !state.layout->stage[state.stage].has_dynamic_offsets)
  111. return;
  112. nir_foreach_overload(shader, overload) {
  113. if (overload->impl) {
  114. nir_builder_init(&state.builder, overload->impl);
  115. nir_foreach_block(overload->impl, apply_dynamic_offsets_block, &state);
  116. nir_metadata_preserve(overload->impl, nir_metadata_block_index |
  117. nir_metadata_dominance);
  118. }
  119. }
  120. struct anv_push_constants *null_data = NULL;
  121. for (unsigned i = 0; i < MAX_DYNAMIC_BUFFERS; i++)
  122. prog_data->param[i + shader->num_uniforms] =
  123. (const gl_constant_value *)&null_data->dynamic_offsets[i];
  124. shader->num_uniforms += MAX_DYNAMIC_BUFFERS;
  125. }