1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2009 Nicolai Haehnle.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
7bf215546Sopenharmony_ci * a copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial
16bf215546Sopenharmony_ci * portions of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22bf215546Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23bf215546Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24bf215546Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "radeon_code.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <stdlib.h>
31bf215546Sopenharmony_ci#include <stdio.h>
32bf215546Sopenharmony_ci#include <string.h>
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "radeon_program.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_civoid rc_constants_init(struct rc_constant_list * c)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci	memset(c, 0, sizeof(*c));
39bf215546Sopenharmony_ci}
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci/**
42bf215546Sopenharmony_ci * Copy a constants structure, assuming that the destination structure
43bf215546Sopenharmony_ci * is not initialized.
44bf215546Sopenharmony_ci */
45bf215546Sopenharmony_civoid rc_constants_copy(struct rc_constant_list * dst, struct rc_constant_list * src)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci	dst->Constants = malloc(sizeof(struct rc_constant) * src->Count);
48bf215546Sopenharmony_ci	memcpy(dst->Constants, src->Constants, sizeof(struct rc_constant) * src->Count);
49bf215546Sopenharmony_ci	dst->Count = src->Count;
50bf215546Sopenharmony_ci	dst->_Reserved = src->Count;
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_civoid rc_constants_destroy(struct rc_constant_list * c)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci	free(c->Constants);
56bf215546Sopenharmony_ci	memset(c, 0, sizeof(*c));
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ciunsigned rc_constants_add(struct rc_constant_list * c, struct rc_constant * constant)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci	unsigned index = c->Count;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci	if (c->Count >= c->_Reserved) {
64bf215546Sopenharmony_ci		struct rc_constant * newlist;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci		c->_Reserved = c->_Reserved * 2;
67bf215546Sopenharmony_ci		if (!c->_Reserved)
68bf215546Sopenharmony_ci			c->_Reserved = 16;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci		newlist = malloc(sizeof(struct rc_constant) * c->_Reserved);
71bf215546Sopenharmony_ci		memcpy(newlist, c->Constants, sizeof(struct rc_constant) * c->Count);
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci		free(c->Constants);
74bf215546Sopenharmony_ci		c->Constants = newlist;
75bf215546Sopenharmony_ci	}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci	c->Constants[index] = *constant;
78bf215546Sopenharmony_ci	c->Count++;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci	return index;
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci/**
85bf215546Sopenharmony_ci * Add a state vector to the constant list, while trying to avoid duplicates.
86bf215546Sopenharmony_ci */
87bf215546Sopenharmony_ciunsigned rc_constants_add_state(struct rc_constant_list * c, unsigned state0, unsigned state1)
88bf215546Sopenharmony_ci{
89bf215546Sopenharmony_ci	unsigned index;
90bf215546Sopenharmony_ci	struct rc_constant constant;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci	for(index = 0; index < c->Count; ++index) {
93bf215546Sopenharmony_ci		if (c->Constants[index].Type == RC_CONSTANT_STATE) {
94bf215546Sopenharmony_ci			if (c->Constants[index].u.State[0] == state0 &&
95bf215546Sopenharmony_ci			    c->Constants[index].u.State[1] == state1)
96bf215546Sopenharmony_ci				return index;
97bf215546Sopenharmony_ci		}
98bf215546Sopenharmony_ci	}
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci	memset(&constant, 0, sizeof(constant));
101bf215546Sopenharmony_ci	constant.Type = RC_CONSTANT_STATE;
102bf215546Sopenharmony_ci	constant.Size = 4;
103bf215546Sopenharmony_ci	constant.u.State[0] = state0;
104bf215546Sopenharmony_ci	constant.u.State[1] = state1;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci	return rc_constants_add(c, &constant);
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci/**
111bf215546Sopenharmony_ci * Add an immediate vector to the constant list, while trying to avoid
112bf215546Sopenharmony_ci * duplicates.
113bf215546Sopenharmony_ci */
114bf215546Sopenharmony_ciunsigned rc_constants_add_immediate_vec4(struct rc_constant_list * c, const float * data)
115bf215546Sopenharmony_ci{
116bf215546Sopenharmony_ci	unsigned index;
117bf215546Sopenharmony_ci	struct rc_constant constant;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci	for(index = 0; index < c->Count; ++index) {
120bf215546Sopenharmony_ci		if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {
121bf215546Sopenharmony_ci			if (!memcmp(c->Constants[index].u.Immediate, data, sizeof(float)*4))
122bf215546Sopenharmony_ci				return index;
123bf215546Sopenharmony_ci		}
124bf215546Sopenharmony_ci	}
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci	memset(&constant, 0, sizeof(constant));
127bf215546Sopenharmony_ci	constant.Type = RC_CONSTANT_IMMEDIATE;
128bf215546Sopenharmony_ci	constant.Size = 4;
129bf215546Sopenharmony_ci	memcpy(constant.u.Immediate, data, sizeof(float) * 4);
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci	return rc_constants_add(c, &constant);
132bf215546Sopenharmony_ci}
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci/**
136bf215546Sopenharmony_ci * Add an immediate scalar to the constant list, while trying to avoid
137bf215546Sopenharmony_ci * duplicates.
138bf215546Sopenharmony_ci */
139bf215546Sopenharmony_ciunsigned rc_constants_add_immediate_scalar(struct rc_constant_list * c, float data, unsigned * swizzle)
140bf215546Sopenharmony_ci{
141bf215546Sopenharmony_ci	unsigned index;
142bf215546Sopenharmony_ci	int free_index = -1;
143bf215546Sopenharmony_ci	struct rc_constant constant;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci	for(index = 0; index < c->Count; ++index) {
146bf215546Sopenharmony_ci		if (c->Constants[index].Type == RC_CONSTANT_IMMEDIATE) {
147bf215546Sopenharmony_ci			unsigned comp;
148bf215546Sopenharmony_ci			for(comp = 0; comp < c->Constants[index].Size; ++comp) {
149bf215546Sopenharmony_ci				if (c->Constants[index].u.Immediate[comp] == data) {
150bf215546Sopenharmony_ci					*swizzle = RC_MAKE_SWIZZLE_SMEAR(comp);
151bf215546Sopenharmony_ci					return index;
152bf215546Sopenharmony_ci				}
153bf215546Sopenharmony_ci			}
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci			if (c->Constants[index].Size < 4)
156bf215546Sopenharmony_ci				free_index = index;
157bf215546Sopenharmony_ci		}
158bf215546Sopenharmony_ci	}
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci	if (free_index >= 0) {
161bf215546Sopenharmony_ci		unsigned comp = c->Constants[free_index].Size++;
162bf215546Sopenharmony_ci		c->Constants[free_index].u.Immediate[comp] = data;
163bf215546Sopenharmony_ci		*swizzle = RC_MAKE_SWIZZLE_SMEAR(comp);
164bf215546Sopenharmony_ci		return free_index;
165bf215546Sopenharmony_ci	}
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci	memset(&constant, 0, sizeof(constant));
168bf215546Sopenharmony_ci	constant.Type = RC_CONSTANT_IMMEDIATE;
169bf215546Sopenharmony_ci	constant.Size = 1;
170bf215546Sopenharmony_ci	constant.u.Immediate[0] = data;
171bf215546Sopenharmony_ci	*swizzle = RC_SWIZZLE_XXXX;
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci	return rc_constants_add(c, &constant);
174bf215546Sopenharmony_ci}
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_civoid rc_constants_print(struct rc_constant_list * c)
177bf215546Sopenharmony_ci{
178bf215546Sopenharmony_ci	unsigned int i;
179bf215546Sopenharmony_ci	for(i = 0; i < c->Count; i++) {
180bf215546Sopenharmony_ci		if (c->Constants[i].Type == RC_CONSTANT_IMMEDIATE) {
181bf215546Sopenharmony_ci			float * values = c->Constants[i].u.Immediate;
182bf215546Sopenharmony_ci			fprintf(stderr, "CONST[%u] = "
183bf215546Sopenharmony_ci				"{ %10.4f %10.4f %10.4f %10.4f }\n",
184bf215546Sopenharmony_ci				i, values[0],values[1], values[2], values[3]);
185bf215546Sopenharmony_ci		}
186bf215546Sopenharmony_ci	}
187bf215546Sopenharmony_ci}
188