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