• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
32 #include <string.h>
33 
34 #include "radeon_program.h"
35 
rc_constants_init(struct rc_constant_list * c)36 void rc_constants_init(struct rc_constant_list * c)
37 {
38 	memset(c, 0, sizeof(*c));
39 }
40 
41 /**
42  * Copy a constants structure, assuming that the destination structure
43  * is not initialized.
44  */
rc_constants_copy(struct rc_constant_list * dst,struct rc_constant_list * src)45 void rc_constants_copy(struct rc_constant_list * dst, struct rc_constant_list * src)
46 {
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;
51 }
52 
rc_constants_destroy(struct rc_constant_list * c)53 void rc_constants_destroy(struct rc_constant_list * c)
54 {
55 	free(c->Constants);
56 	memset(c, 0, sizeof(*c));
57 }
58 
rc_constants_add(struct rc_constant_list * c,struct rc_constant * constant)59 unsigned rc_constants_add(struct rc_constant_list * c, struct rc_constant * constant)
60 {
61 	unsigned index = c->Count;
62 
63 	if (c->Count >= c->_Reserved) {
64 		struct rc_constant * newlist;
65 
66 		c->_Reserved = c->_Reserved * 2;
67 		if (!c->_Reserved)
68 			c->_Reserved = 16;
69 
70 		newlist = malloc(sizeof(struct rc_constant) * c->_Reserved);
71 		memcpy(newlist, c->Constants, sizeof(struct rc_constant) * c->Count);
72 
73 		free(c->Constants);
74 		c->Constants = newlist;
75 	}
76 
77 	c->Constants[index] = *constant;
78 	c->Count++;
79 
80 	return index;
81 }
82 
83 
84 /**
85  * Add a state vector to the constant list, while trying to avoid duplicates.
86  */
rc_constants_add_state(struct rc_constant_list * c,unsigned state0,unsigned state1)87 unsigned rc_constants_add_state(struct rc_constant_list * c, unsigned state0, unsigned state1)
88 {
89 	unsigned index;
90 	struct rc_constant constant;
91 
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)
96 				return index;
97 		}
98 	}
99 
100 	memset(&constant, 0, sizeof(constant));
101 	constant.Type = RC_CONSTANT_STATE;
102 	constant.Size = 4;
103 	constant.u.State[0] = state0;
104 	constant.u.State[1] = state1;
105 
106 	return rc_constants_add(c, &constant);
107 }
108 
109 
110 /**
111  * Add an immediate vector to the constant list, while trying to avoid
112  * duplicates.
113  */
rc_constants_add_immediate_vec4(struct rc_constant_list * c,const float * data)114 unsigned rc_constants_add_immediate_vec4(struct rc_constant_list * c, const float * data)
115 {
116 	unsigned index;
117 	struct rc_constant constant;
118 
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))
122 				return index;
123 		}
124 	}
125 
126 	memset(&constant, 0, sizeof(constant));
127 	constant.Type = RC_CONSTANT_IMMEDIATE;
128 	constant.Size = 4;
129 	memcpy(constant.u.Immediate, data, sizeof(float) * 4);
130 
131 	return rc_constants_add(c, &constant);
132 }
133 
134 
135 /**
136  * Add an immediate scalar to the constant list, while trying to avoid
137  * duplicates.
138  */
rc_constants_add_immediate_scalar(struct rc_constant_list * c,float data,unsigned * swizzle)139 unsigned rc_constants_add_immediate_scalar(struct rc_constant_list * c, float data, unsigned * swizzle)
140 {
141 	unsigned index;
142 	int free_index = -1;
143 	struct rc_constant constant;
144 
145 	for(index = 0; index < c->Count; ++index) {
146 		if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {
147 			unsigned comp;
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);
151 					return index;
152 				}
153 			}
154 
155 			if (c->Constants[index].Size < 4)
156 				free_index = index;
157 		}
158 	}
159 
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);
164 		return free_index;
165 	}
166 
167 	memset(&constant, 0, sizeof(constant));
168 	constant.Type = RC_CONSTANT_IMMEDIATE;
169 	constant.Size = 1;
170 	constant.u.Immediate[0] = data;
171 	*swizzle = RC_SWIZZLE_XXXX;
172 
173 	return rc_constants_add(c, &constant);
174 }
175 
rc_constants_print(struct rc_constant_list * c)176 void rc_constants_print(struct rc_constant_list * c)
177 {
178 	unsigned int i;
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]);
185 		}
186 	}
187 }
188