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 */
32 link_util_add_program_resource(struct gl_shader_program
*prog
,
33 struct set
*resource_set
,
34 GLenum type
, const void *data
, uint8_t stages
)
38 /* If resource already exists, do not add it again. */
39 if (_mesa_set_search(resource_set
, data
))
42 prog
->data
->ProgramResourceList
=
44 prog
->data
->ProgramResourceList
,
46 prog
->data
->NumProgramResourceList
+ 1);
48 if (!prog
->data
->ProgramResourceList
) {
49 linker_error(prog
, "Out of memory during linking.\n");
53 struct gl_program_resource
*res
=
54 &prog
->data
->ProgramResourceList
[prog
->data
->NumProgramResourceList
];
58 res
->StageReferences
= stages
;
60 prog
->data
->NumProgramResourceList
++;
62 _mesa_set_add(resource_set
, data
);
68 * Search through the list of empty blocks to find one that fits the current
72 link_util_find_empty_block(struct gl_shader_program
*prog
,
73 struct gl_uniform_storage
*uniform
)
75 const unsigned entries
= MAX2(1, uniform
->array_elements
);
77 foreach_list_typed(struct empty_uniform_block
, block
, link
,
78 &prog
->EmptyUniformLocations
) {
79 /* Found a block with enough slots to fit the uniform */
80 if (block
->slots
== entries
) {
81 unsigned start
= block
->start
;
82 exec_node_remove(&block
->link
);
86 /* Found a block with more slots than needed. It can still be used. */
87 } else if (block
->slots
> entries
) {
88 unsigned start
= block
->start
;
89 block
->start
+= entries
;
90 block
->slots
-= entries
;
100 link_util_update_empty_uniform_locations(struct gl_shader_program
*prog
)
102 struct empty_uniform_block
*current_block
= NULL
;
104 for (unsigned i
= 0; i
< prog
->NumUniformRemapTable
; i
++) {
105 /* We found empty space in UniformRemapTable. */
106 if (prog
->UniformRemapTable
[i
] == NULL
) {
107 /* We've found the beginning of a new continous block of empty slots */
108 if (!current_block
|| current_block
->start
+ current_block
->slots
!= i
) {
109 current_block
= rzalloc(prog
, struct empty_uniform_block
);
110 current_block
->start
= i
;
111 exec_list_push_tail(&prog
->EmptyUniformLocations
,
112 ¤t_block
->link
);
115 /* The current block continues, so we simply increment its slots */
116 current_block
->slots
++;