1a3d8bb641284352fc86217d9221370352670c20
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"
33 #include "radeon_program.h"
35 void rc_constants_init(struct rc_constant_list
* c
)
37 memset(c
, 0, sizeof(*c
));
41 * Copy a constants structure, assuming that the destination structure
44 void rc_constants_copy(struct rc_constant_list
* dst
, struct rc_constant_list
* src
)
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
;
52 void rc_constants_destroy(struct rc_constant_list
* c
)
55 memset(c
, 0, sizeof(*c
));
58 unsigned rc_constants_add(struct rc_constant_list
* c
, struct rc_constant
* constant
)
60 unsigned index
= c
->Count
;
62 if (c
->Count
>= c
->_Reserved
) {
63 struct rc_constant
* newlist
;
65 c
->_Reserved
= c
->_Reserved
* 2;
69 newlist
= malloc(sizeof(struct rc_constant
) * c
->_Reserved
);
70 memcpy(newlist
, c
->Constants
, sizeof(struct rc_constant
) * c
->Count
);
73 c
->Constants
= newlist
;
76 c
->Constants
[index
] = *constant
;
84 * Add a state vector to the constant list, while trying to avoid duplicates.
86 unsigned rc_constants_add_state(struct rc_constant_list
* c
, unsigned state0
, unsigned state1
)
89 struct rc_constant constant
;
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
)
99 memset(&constant
, 0, sizeof(constant
));
100 constant
.Type
= RC_CONSTANT_STATE
;
102 constant
.u
.State
[0] = state0
;
103 constant
.u
.State
[1] = state1
;
105 return rc_constants_add(c
, &constant
);
110 * Add an immediate vector to the constant list, while trying to avoid
113 unsigned rc_constants_add_immediate_vec4(struct rc_constant_list
* c
, const float * data
)
116 struct rc_constant constant
;
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))
125 memset(&constant
, 0, sizeof(constant
));
126 constant
.Type
= RC_CONSTANT_IMMEDIATE
;
128 memcpy(constant
.u
.Immediate
, data
, sizeof(float) * 4);
130 return rc_constants_add(c
, &constant
);
135 * Add an immediate scalar to the constant list, while trying to avoid
138 unsigned rc_constants_add_immediate_scalar(struct rc_constant_list
* c
, float data
, unsigned * swizzle
)
142 struct rc_constant constant
;
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
);
153 if (c
->Constants
[index
].Size
< 4)
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
);
165 memset(&constant
, 0, sizeof(constant
));
166 constant
.Type
= RC_CONSTANT_IMMEDIATE
;
168 constant
.u
.Immediate
[0] = data
;
169 *swizzle
= RC_SWIZZLE_XXXX
;
171 return rc_constants_add(c
, &constant
);