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 "main/mtypes.h"
29 #include "shader/prog_instruction.h"
31 #include "radeon_code.h"
33 void rc_constants_init(struct rc_constant_list
* c
)
35 memset(c
, 0, sizeof(*c
));
39 * Copy a constants structure, assuming that the destination structure
42 void rc_constants_copy(struct rc_constant_list
* dst
, struct rc_constant_list
* src
)
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
;
50 void rc_constants_destroy(struct rc_constant_list
* c
)
53 memset(c
, 0, sizeof(*c
));
56 unsigned rc_constants_add(struct rc_constant_list
* c
, struct rc_constant
* constant
)
58 unsigned index
= c
->Count
;
60 if (c
->Count
>= c
->_Reserved
) {
61 struct rc_constant
* newlist
;
63 c
->_Reserved
= c
->_Reserved
* 2;
67 newlist
= malloc(sizeof(struct rc_constant
) * c
->_Reserved
);
68 memcpy(newlist
, c
->Constants
, sizeof(struct rc_constant
) * c
->Count
);
71 c
->Constants
= newlist
;
74 c
->Constants
[index
] = *constant
;
82 * Add a state vector to the constant list, while trying to avoid duplicates.
84 unsigned rc_constants_add_state(struct rc_constant_list
* c
, unsigned state0
, unsigned state1
)
87 struct rc_constant constant
;
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
)
97 memset(&constant
, 0, sizeof(constant
));
98 constant
.Type
= RC_CONSTANT_STATE
;
100 constant
.u
.State
[0] = state0
;
101 constant
.u
.State
[1] = state1
;
103 return rc_constants_add(c
, &constant
);
108 * Add an immediate vector to the constant list, while trying to avoid
111 unsigned rc_constants_add_immediate_vec4(struct rc_constant_list
* c
, const float * data
)
114 struct rc_constant constant
;
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))
123 memset(&constant
, 0, sizeof(constant
));
124 constant
.Type
= RC_CONSTANT_IMMEDIATE
;
126 memcpy(constant
.u
.Immediate
, data
, sizeof(float) * 4);
128 return rc_constants_add(c
, &constant
);
133 * Add an immediate scalar to the constant list, while trying to avoid
136 unsigned rc_constants_add_immediate_scalar(struct rc_constant_list
* c
, float data
, unsigned * swizzle
)
140 struct rc_constant constant
;
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
);
151 if (c
->Constants
[index
].Size
< 4)
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
);
163 memset(&constant
, 0, sizeof(constant
));
164 constant
.Type
= RC_CONSTANT_IMMEDIATE
;
166 constant
.u
.Immediate
[0] = data
;
167 *swizzle
= SWIZZLE_XXXX
;
169 return rc_constants_add(c
, &constant
);