2 * Copyright (C) 2009 Nicolai Haehnle.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "radeon_code.h"
34 #include "radeon_program.h"
36 void rc_constants_init(struct rc_constant_list * c)
38 memset(c, 0, sizeof(*c));
42 * Copy a constants structure, assuming that the destination structure
45 void rc_constants_copy(struct rc_constant_list * dst, struct rc_constant_list * src)
47 dst->Constants = malloc(sizeof(struct rc_constant) * src->Count);
48 memcpy(dst->Constants, src->Constants, sizeof(struct rc_constant) * src->Count);
49 dst->Count = src->Count;
50 dst->_Reserved = src->Count;
53 void rc_constants_destroy(struct rc_constant_list * c)
56 memset(c, 0, sizeof(*c));
59 unsigned rc_constants_add(struct rc_constant_list * c, struct rc_constant * constant)
61 unsigned index = c->Count;
63 if (c->Count >= c->_Reserved) {
64 struct rc_constant * newlist;
66 c->_Reserved = c->_Reserved * 2;
70 newlist = malloc(sizeof(struct rc_constant) * c->_Reserved);
71 memcpy(newlist, c->Constants, sizeof(struct rc_constant) * c->Count);
74 c->Constants = newlist;
77 c->Constants[index] = *constant;
85 * Add a state vector to the constant list, while trying to avoid duplicates.
87 unsigned rc_constants_add_state(struct rc_constant_list * c, unsigned state0, unsigned state1)
90 struct rc_constant constant;
92 for(index = 0; index < c->Count; ++index) {
93 if (c->Constants[index].Type == RC_CONSTANT_STATE) {
94 if (c->Constants[index].u.State[0] == state0 &&
95 c->Constants[index].u.State[1] == state1)
100 memset(&constant, 0, sizeof(constant));
101 constant.Type = RC_CONSTANT_STATE;
103 constant.u.State[0] = state0;
104 constant.u.State[1] = state1;
106 return rc_constants_add(c, &constant);
111 * Add an immediate vector to the constant list, while trying to avoid
114 unsigned rc_constants_add_immediate_vec4(struct rc_constant_list * c, const float * data)
117 struct rc_constant constant;
119 for(index = 0; index < c->Count; ++index) {
120 if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {
121 if (!memcmp(c->Constants[index].u.Immediate, data, sizeof(float)*4))
126 memset(&constant, 0, sizeof(constant));
127 constant.Type = RC_CONSTANT_IMMEDIATE;
129 memcpy(constant.u.Immediate, data, sizeof(float) * 4);
131 return rc_constants_add(c, &constant);
136 * Add an immediate scalar to the constant list, while trying to avoid
139 unsigned rc_constants_add_immediate_scalar(struct rc_constant_list * c, float data, unsigned * swizzle)
143 struct rc_constant constant;
145 for(index = 0; index < c->Count; ++index) {
146 if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {
148 for(comp = 0; comp < c->Constants[index].Size; ++comp) {
149 if (c->Constants[index].u.Immediate[comp] == data) {
150 *swizzle = RC_MAKE_SWIZZLE_SMEAR(comp);
155 if (c->Constants[index].Size < 4)
160 if (free_index >= 0) {
161 unsigned comp = c->Constants[free_index].Size++;
162 c->Constants[free_index].u.Immediate[comp] = data;
163 *swizzle = RC_MAKE_SWIZZLE_SMEAR(comp);
167 memset(&constant, 0, sizeof(constant));
168 constant.Type = RC_CONSTANT_IMMEDIATE;
170 constant.u.Immediate[0] = data;
171 *swizzle = RC_SWIZZLE_XXXX;
173 return rc_constants_add(c, &constant);
176 void rc_constants_print(struct rc_constant_list * c)
179 for(i = 0; i < c->Count; i++) {
180 if (c->Constants[i].Type == RC_CONSTANT_IMMEDIATE) {
181 float * values = c->Constants[i].u.Immediate;
182 fprintf(stderr, "CONST[%u] = "
183 "{ %10.4f %10.4f %10.4f %10.4f }\n",
184 i, values[0],values[1], values[2], values[3]);