1a3d8bb641284352fc86217d9221370352670c20
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_code.c
1 /*
2 * Copyright (C) 2009 Nicolai Haehnle.
3 *
4 * All Rights Reserved.
5 *
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:
13 *
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.
17 *
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.
25 *
26 */
27
28 #include "radeon_code.h"
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "radeon_program.h"
34
35 void rc_constants_init(struct rc_constant_list * c)
36 {
37 memset(c, 0, sizeof(*c));
38 }
39
40 /**
41 * Copy a constants structure, assuming that the destination structure
42 * is not initialized.
43 */
44 void rc_constants_copy(struct rc_constant_list * dst, struct rc_constant_list * src)
45 {
46 dst->Constants = malloc(sizeof(struct rc_constant) * src->Count);
47 memcpy(dst->Constants, src->Constants, sizeof(struct rc_constant) * src->Count);
48 dst->Count = src->Count;
49 dst->_Reserved = src->Count;
50 }
51
52 void rc_constants_destroy(struct rc_constant_list * c)
53 {
54 free(c->Constants);
55 memset(c, 0, sizeof(*c));
56 }
57
58 unsigned rc_constants_add(struct rc_constant_list * c, struct rc_constant * constant)
59 {
60 unsigned index = c->Count;
61
62 if (c->Count >= c->_Reserved) {
63 struct rc_constant * newlist;
64
65 c->_Reserved = c->_Reserved * 2;
66 if (!c->_Reserved)
67 c->_Reserved = 16;
68
69 newlist = malloc(sizeof(struct rc_constant) * c->_Reserved);
70 memcpy(newlist, c->Constants, sizeof(struct rc_constant) * c->Count);
71
72 free(c->Constants);
73 c->Constants = newlist;
74 }
75
76 c->Constants[index] = *constant;
77 c->Count++;
78
79 return index;
80 }
81
82
83 /**
84 * Add a state vector to the constant list, while trying to avoid duplicates.
85 */
86 unsigned rc_constants_add_state(struct rc_constant_list * c, unsigned state0, unsigned state1)
87 {
88 unsigned index;
89 struct rc_constant constant;
90
91 for(index = 0; index < c->Count; ++index) {
92 if (c->Constants[index].Type == RC_CONSTANT_STATE) {
93 if (c->Constants[index].u.State[0] == state0 &&
94 c->Constants[index].u.State[1] == state1)
95 return index;
96 }
97 }
98
99 memset(&constant, 0, sizeof(constant));
100 constant.Type = RC_CONSTANT_STATE;
101 constant.Size = 4;
102 constant.u.State[0] = state0;
103 constant.u.State[1] = state1;
104
105 return rc_constants_add(c, &constant);
106 }
107
108
109 /**
110 * Add an immediate vector to the constant list, while trying to avoid
111 * duplicates.
112 */
113 unsigned rc_constants_add_immediate_vec4(struct rc_constant_list * c, const float * data)
114 {
115 unsigned index;
116 struct rc_constant constant;
117
118 for(index = 0; index < c->Count; ++index) {
119 if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {
120 if (!memcmp(c->Constants[index].u.Immediate, data, sizeof(float)*4))
121 return index;
122 }
123 }
124
125 memset(&constant, 0, sizeof(constant));
126 constant.Type = RC_CONSTANT_IMMEDIATE;
127 constant.Size = 4;
128 memcpy(constant.u.Immediate, data, sizeof(float) * 4);
129
130 return rc_constants_add(c, &constant);
131 }
132
133
134 /**
135 * Add an immediate scalar to the constant list, while trying to avoid
136 * duplicates.
137 */
138 unsigned rc_constants_add_immediate_scalar(struct rc_constant_list * c, float data, unsigned * swizzle)
139 {
140 unsigned index;
141 int free_index = -1;
142 struct rc_constant constant;
143
144 for(index = 0; index < c->Count; ++index) {
145 if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {
146 for(unsigned comp = 0; comp < c->Constants[index].Size; ++comp) {
147 if (c->Constants[index].u.Immediate[comp] == data) {
148 *swizzle = RC_MAKE_SWIZZLE(comp, comp, comp, comp);
149 return index;
150 }
151 }
152
153 if (c->Constants[index].Size < 4)
154 free_index = index;
155 }
156 }
157
158 if (free_index >= 0) {
159 unsigned comp = c->Constants[free_index].Size++;
160 c->Constants[free_index].u.Immediate[comp] = data;
161 *swizzle = RC_MAKE_SWIZZLE(comp, comp, comp, comp);
162 return free_index;
163 }
164
165 memset(&constant, 0, sizeof(constant));
166 constant.Type = RC_CONSTANT_IMMEDIATE;
167 constant.Size = 1;
168 constant.u.Immediate[0] = data;
169 *swizzle = RC_SWIZZLE_XXXX;
170
171 return rc_constants_add(c, &constant);
172 }