2 * Copyright © 2018 Intel Corporation
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:
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
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
24 #include "main/mtypes.h"
25 #include "linker_util.h"
26 #include "util/bitscan.h"
28 #include "ir_uniform.h" /* for gl_uniform_storage */
30 /* Utility methods shared between the GLSL IR and the NIR */
32 /* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources:
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."
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
,
48 /* If the uniform is not a shader storage buffer or is not an array return
51 if (!uniform
->is_shader_storage
|| top_level_array_size_in_bytes
== 0)
54 int after_top_level_array
= top_level_array_base_offset
+
55 top_level_array_size_in_bytes
;
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.
60 if (block_index
!= uniform
->block_index
||
61 uniform
->offset
>= after_top_level_array
||
62 uniform
->offset
< second_element_offset
) {
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
)
76 /* If resource already exists, do not add it again. */
77 if (_mesa_set_search(resource_set
, data
))
80 prog
->data
->ProgramResourceList
=
82 prog
->data
->ProgramResourceList
,
84 prog
->data
->NumProgramResourceList
+ 1);
86 if (!prog
->data
->ProgramResourceList
) {
87 linker_error(prog
, "Out of memory during linking.\n");
91 struct gl_program_resource
*res
=
92 &prog
->data
->ProgramResourceList
[prog
->data
->NumProgramResourceList
];
96 res
->StageReferences
= stages
;
98 prog
->data
->NumProgramResourceList
++;
100 _mesa_set_add(resource_set
, data
);
106 * Search through the list of empty blocks to find one that fits the current
110 link_util_find_empty_block(struct gl_shader_program
*prog
,
111 struct gl_uniform_storage
*uniform
)
113 const unsigned entries
= MAX2(1, uniform
->array_elements
);
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
);
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
;
138 link_util_update_empty_uniform_locations(struct gl_shader_program
*prog
)
140 struct empty_uniform_block
*current_block
= NULL
;
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 ¤t_block
->link
);
153 /* The current block continues, so we simply increment its slots */
154 current_block
->slots
++;
160 link_util_check_subroutine_resources(struct gl_shader_program
*prog
)
162 unsigned mask
= prog
->data
->linked_stages
;
164 const int i
= u_bit_scan(&mask
);
165 struct gl_program
*p
= prog
->_LinkedShaders
[i
]->Program
;
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
));