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"
27 #include "ir_uniform.h" /* for gl_uniform_storage */
29 /* Utility methods shared between the GLSL IR and the NIR */
31 /* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources:
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."
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
,
47 /* If the uniform is not a shader storage buffer or is not an array return
50 if (!uniform
->is_shader_storage
|| top_level_array_size_in_bytes
== 0)
53 int after_top_level_array
= top_level_array_base_offset
+
54 top_level_array_size_in_bytes
;
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.
59 if (block_index
!= uniform
->block_index
||
60 uniform
->offset
>= after_top_level_array
||
61 uniform
->offset
< second_element_offset
) {
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
)
75 /* If resource already exists, do not add it again. */
76 if (_mesa_set_search(resource_set
, data
))
79 prog
->data
->ProgramResourceList
=
81 prog
->data
->ProgramResourceList
,
83 prog
->data
->NumProgramResourceList
+ 1);
85 if (!prog
->data
->ProgramResourceList
) {
86 linker_error(prog
, "Out of memory during linking.\n");
90 struct gl_program_resource
*res
=
91 &prog
->data
->ProgramResourceList
[prog
->data
->NumProgramResourceList
];
95 res
->StageReferences
= stages
;
97 prog
->data
->NumProgramResourceList
++;
99 _mesa_set_add(resource_set
, data
);
105 * Search through the list of empty blocks to find one that fits the current
109 link_util_find_empty_block(struct gl_shader_program
*prog
,
110 struct gl_uniform_storage
*uniform
)
112 const unsigned entries
= MAX2(1, uniform
->array_elements
);
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
);
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
;
137 link_util_update_empty_uniform_locations(struct gl_shader_program
*prog
)
139 struct empty_uniform_block
*current_block
= NULL
;
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 ¤t_block
->link
);
152 /* The current block continues, so we simply increment its slots */
153 current_block
->slots
++;