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.

nir_lower_atomics.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright © 2014 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. * Authors:
  24. * Connor Abbott (cwabbott0@gmail.com)
  25. *
  26. */
  27. #include "compiler/glsl/ir_uniform.h"
  28. #include "nir.h"
  29. #include "main/config.h"
  30. #include <assert.h>
  31. typedef struct {
  32. const struct gl_shader_program *shader_program;
  33. nir_shader *shader;
  34. } lower_atomic_state;
  35. /*
  36. * replace atomic counter intrinsics that use a variable with intrinsics
  37. * that directly store the buffer index and byte offset
  38. */
  39. static void
  40. lower_instr(nir_intrinsic_instr *instr,
  41. lower_atomic_state *state)
  42. {
  43. nir_intrinsic_op op;
  44. switch (instr->intrinsic) {
  45. case nir_intrinsic_atomic_counter_read_var:
  46. op = nir_intrinsic_atomic_counter_read;
  47. break;
  48. case nir_intrinsic_atomic_counter_inc_var:
  49. op = nir_intrinsic_atomic_counter_inc;
  50. break;
  51. case nir_intrinsic_atomic_counter_dec_var:
  52. op = nir_intrinsic_atomic_counter_dec;
  53. break;
  54. default:
  55. return;
  56. }
  57. if (instr->variables[0]->var->data.mode != nir_var_uniform &&
  58. instr->variables[0]->var->data.mode != nir_var_shader_storage)
  59. return; /* atomics passed as function arguments can't be lowered */
  60. void *mem_ctx = ralloc_parent(instr);
  61. unsigned uniform_loc = instr->variables[0]->var->data.location;
  62. nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(mem_ctx, op);
  63. nir_intrinsic_set_base(new_instr,
  64. state->shader_program->UniformStorage[uniform_loc].opaque[state->shader->stage].index);
  65. nir_load_const_instr *offset_const = nir_load_const_instr_create(mem_ctx, 1);
  66. offset_const->value.u[0] = instr->variables[0]->var->data.offset;
  67. nir_instr_insert_before(&instr->instr, &offset_const->instr);
  68. nir_ssa_def *offset_def = &offset_const->def;
  69. nir_deref *tail = &instr->variables[0]->deref;
  70. while (tail->child != NULL) {
  71. assert(tail->child->deref_type == nir_deref_type_array);
  72. nir_deref_array *deref_array = nir_deref_as_array(tail->child);
  73. tail = tail->child;
  74. unsigned child_array_elements = tail->child != NULL ?
  75. glsl_get_aoa_size(tail->type) : 1;
  76. offset_const->value.u[0] += deref_array->base_offset *
  77. child_array_elements * ATOMIC_COUNTER_SIZE;
  78. if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
  79. nir_load_const_instr *atomic_counter_size =
  80. nir_load_const_instr_create(mem_ctx, 1);
  81. atomic_counter_size->value.u[0] = child_array_elements * ATOMIC_COUNTER_SIZE;
  82. nir_instr_insert_before(&instr->instr, &atomic_counter_size->instr);
  83. nir_alu_instr *mul = nir_alu_instr_create(mem_ctx, nir_op_imul);
  84. nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
  85. mul->dest.write_mask = 0x1;
  86. nir_src_copy(&mul->src[0].src, &deref_array->indirect, mul);
  87. mul->src[1].src.is_ssa = true;
  88. mul->src[1].src.ssa = &atomic_counter_size->def;
  89. nir_instr_insert_before(&instr->instr, &mul->instr);
  90. nir_alu_instr *add = nir_alu_instr_create(mem_ctx, nir_op_iadd);
  91. nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
  92. add->dest.write_mask = 0x1;
  93. add->src[0].src.is_ssa = true;
  94. add->src[0].src.ssa = &mul->dest.dest.ssa;
  95. add->src[1].src.is_ssa = true;
  96. add->src[1].src.ssa = offset_def;
  97. nir_instr_insert_before(&instr->instr, &add->instr);
  98. offset_def = &add->dest.dest.ssa;
  99. }
  100. }
  101. new_instr->src[0].is_ssa = true;
  102. new_instr->src[0].ssa = offset_def;
  103. if (instr->dest.is_ssa) {
  104. nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,
  105. instr->dest.ssa.num_components, NULL);
  106. nir_ssa_def_rewrite_uses(&instr->dest.ssa,
  107. nir_src_for_ssa(&new_instr->dest.ssa));
  108. } else {
  109. nir_dest_copy(&new_instr->dest, &instr->dest, mem_ctx);
  110. }
  111. nir_instr_insert_before(&instr->instr, &new_instr->instr);
  112. nir_instr_remove(&instr->instr);
  113. }
  114. static bool
  115. lower_block(nir_block *block, void *state)
  116. {
  117. nir_foreach_instr_safe(block, instr) {
  118. if (instr->type == nir_instr_type_intrinsic)
  119. lower_instr(nir_instr_as_intrinsic(instr),
  120. (lower_atomic_state *) state);
  121. }
  122. return true;
  123. }
  124. void
  125. nir_lower_atomics(nir_shader *shader,
  126. const struct gl_shader_program *shader_program)
  127. {
  128. lower_atomic_state state = {
  129. .shader = shader,
  130. .shader_program = shader_program,
  131. };
  132. nir_foreach_function(shader, function) {
  133. if (function->impl) {
  134. nir_foreach_block(function->impl, lower_block, (void *) &state);
  135. nir_metadata_preserve(function->impl, nir_metadata_block_index |
  136. nir_metadata_dominance);
  137. }
  138. }
  139. }