glsl/linker: check for xfb_offset aliasing
[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 bool
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)
35 {
36 assert(data);
37
38 /* If resource already exists, do not add it again. */
39 if (_mesa_set_search(resource_set, data))
40 return true;
41
42 prog->data->ProgramResourceList =
43 reralloc(prog->data,
44 prog->data->ProgramResourceList,
45 gl_program_resource,
46 prog->data->NumProgramResourceList + 1);
47
48 if (!prog->data->ProgramResourceList) {
49 linker_error(prog, "Out of memory during linking.\n");
50 return false;
51 }
52
53 struct gl_program_resource *res =
54 &prog->data->ProgramResourceList[prog->data->NumProgramResourceList];
55
56 res->Type = type;
57 res->Data = data;
58 res->StageReferences = stages;
59
60 prog->data->NumProgramResourceList++;
61
62 _mesa_set_add(resource_set, data);
63
64 return true;
65 }
66
67 /**
68 * Search through the list of empty blocks to find one that fits the current
69 * uniform.
70 */
71 int
72 link_util_find_empty_block(struct gl_shader_program *prog,
73 struct gl_uniform_storage *uniform)
74 {
75 const unsigned entries = MAX2(1, uniform->array_elements);
76
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);
83 ralloc_free(block);
84
85 return start;
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;
91
92 return start;
93 }
94 }
95
96 return -1;
97 }
98
99 void
100 link_util_update_empty_uniform_locations(struct gl_shader_program *prog)
101 {
102 struct empty_uniform_block *current_block = NULL;
103
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 &current_block->link);
113 }
114
115 /* The current block continues, so we simply increment its slots */
116 current_block->slots++;
117 }
118 }
119 }