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