glsl: simplify resource list building code
[mesa.git] / src / compiler / glsl / linker_util.cpp
1 /*
2 * Copyright © 2018 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 */
24 #include "main/mtypes.h"
25 #include "linker_util.h"
26 #include "util/set.h"
27 #include "ir_uniform.h" /* for gl_uniform_storage */
28
29 /* Utility methods shared between the GLSL IR and the NIR */
30
31 /* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources:
32 *
33 * "For an active shader storage block member declared as an array of an
34 * aggregate type, an entry will be generated only for the first array
35 * element, regardless of its type. Such block members are referred to as
36 * top-level arrays. If the block member is an aggregate type, the
37 * enumeration rules are then applied recursively."
38 */
39 bool
40 link_util_should_add_buffer_variable(struct gl_shader_program *prog,
41 struct gl_uniform_storage *uniform,
42 int top_level_array_base_offset,
43 int top_level_array_size_in_bytes,
44 int second_element_offset,
45 int block_index)
46 {
47 /* If the uniform is not a shader storage buffer or is not an array return
48 * true.
49 */
50 if (!uniform->is_shader_storage || top_level_array_size_in_bytes == 0)
51 return true;
52
53 int after_top_level_array = top_level_array_base_offset +
54 top_level_array_size_in_bytes;
55
56 /* Check for a new block, or that we are not dealing with array elements of
57 * a top member array other than the first element.
58 */
59 if (block_index != uniform->block_index ||
60 uniform->offset >= after_top_level_array ||
61 uniform->offset < second_element_offset) {
62 return true;
63 }
64
65 return false;
66 }
67
68 bool
69 link_util_add_program_resource(struct gl_shader_program *prog,
70 struct set *resource_set,
71 GLenum type, const void *data, uint8_t stages)
72 {
73 assert(data);
74
75 /* If resource already exists, do not add it again. */
76 if (_mesa_set_search(resource_set, data))
77 return true;
78
79 prog->data->ProgramResourceList =
80 reralloc(prog->data,
81 prog->data->ProgramResourceList,
82 gl_program_resource,
83 prog->data->NumProgramResourceList + 1);
84
85 if (!prog->data->ProgramResourceList) {
86 linker_error(prog, "Out of memory during linking.\n");
87 return false;
88 }
89
90 struct gl_program_resource *res =
91 &prog->data->ProgramResourceList[prog->data->NumProgramResourceList];
92
93 res->Type = type;
94 res->Data = data;
95 res->StageReferences = stages;
96
97 prog->data->NumProgramResourceList++;
98
99 _mesa_set_add(resource_set, data);
100
101 return true;
102 }
103
104 /**
105 * Search through the list of empty blocks to find one that fits the current
106 * uniform.
107 */
108 int
109 link_util_find_empty_block(struct gl_shader_program *prog,
110 struct gl_uniform_storage *uniform)
111 {
112 const unsigned entries = MAX2(1, uniform->array_elements);
113
114 foreach_list_typed(struct empty_uniform_block, block, link,
115 &prog->EmptyUniformLocations) {
116 /* Found a block with enough slots to fit the uniform */
117 if (block->slots == entries) {
118 unsigned start = block->start;
119 exec_node_remove(&block->link);
120 ralloc_free(block);
121
122 return start;
123 /* Found a block with more slots than needed. It can still be used. */
124 } else if (block->slots > entries) {
125 unsigned start = block->start;
126 block->start += entries;
127 block->slots -= entries;
128
129 return start;
130 }
131 }
132
133 return -1;
134 }
135
136 void
137 link_util_update_empty_uniform_locations(struct gl_shader_program *prog)
138 {
139 struct empty_uniform_block *current_block = NULL;
140
141 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
142 /* We found empty space in UniformRemapTable. */
143 if (prog->UniformRemapTable[i] == NULL) {
144 /* We've found the beginning of a new continous block of empty slots */
145 if (!current_block || current_block->start + current_block->slots != i) {
146 current_block = rzalloc(prog, struct empty_uniform_block);
147 current_block->start = i;
148 exec_list_push_tail(&prog->EmptyUniformLocations,
149 &current_block->link);
150 }
151
152 /* The current block continues, so we simply increment its slots */
153 current_block->slots++;
154 }
155 }
156 }