summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/r300/compiler/r300_fragprog_swizzle.c
blob: 1b14cc3888b7f1e8a469badbd922a63c6b2cc72f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (C) 2008 Nicolai Haehnle.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/**
 * @file
 * Utilities to deal with the somewhat odd restriction on R300 fragment
 * program swizzles.
 */

#include "r300_fragprog_swizzle.h"

#include "../r300_reg.h"
#include "radeon_nqssadce.h"
#include "radeon_compiler.h"

#define MAKE_SWZ3(x, y, z) (MAKE_SWIZZLE4(SWIZZLE_##x, SWIZZLE_##y, SWIZZLE_##z, SWIZZLE_ZERO))

struct swizzle_data {
	GLuint hash; /**< swizzle value this matches */
	GLuint base; /**< base value for hw swizzle */
	GLuint stride; /**< difference in base between arg0/1/2 */
};

static const struct swizzle_data native_swizzles[] = {
	{MAKE_SWZ3(X, Y, Z), R300_ALU_ARGC_SRC0C_XYZ, 4},
	{MAKE_SWZ3(X, X, X), R300_ALU_ARGC_SRC0C_XXX, 4},
	{MAKE_SWZ3(Y, Y, Y), R300_ALU_ARGC_SRC0C_YYY, 4},
	{MAKE_SWZ3(Z, Z, Z), R300_ALU_ARGC_SRC0C_ZZZ, 4},
	{MAKE_SWZ3(W, W, W), R300_ALU_ARGC_SRC0A, 1},
	{MAKE_SWZ3(Y, Z, X), R300_ALU_ARGC_SRC0C_YZX, 1},
	{MAKE_SWZ3(Z, X, Y), R300_ALU_ARGC_SRC0C_ZXY, 1},
	{MAKE_SWZ3(W, Z, Y), R300_ALU_ARGC_SRC0CA_WZY, 1},
	{MAKE_SWZ3(ONE, ONE, ONE), R300_ALU_ARGC_ONE, 0},
	{MAKE_SWZ3(ZERO, ZERO, ZERO), R300_ALU_ARGC_ZERO, 0}
};

static const int num_native_swizzles = sizeof(native_swizzles)/sizeof(native_swizzles[0]);


/**
 * Find a native RGB swizzle that matches the given swizzle.
 * Returns 0 if none found.
 */
static const struct swizzle_data* lookup_native_swizzle(GLuint swizzle)
{
	int i, comp;

	for(i = 0; i < num_native_swizzles; ++i) {
		const struct swizzle_data* sd = &native_swizzles[i];
		for(comp = 0; comp < 3; ++comp) {
			GLuint swz = GET_SWZ(swizzle, comp);
			if (swz == SWIZZLE_NIL)
				continue;
			if (swz != GET_SWZ(sd->hash, comp))
				break;
		}
		if (comp == 3)
			return sd;
	}

	return 0;
}


/**
 * Check whether the given instruction supports the swizzle and negate
 * combinations in the given source register.
 */
GLboolean r300FPIsNativeSwizzle(GLuint opcode, struct prog_src_register reg)
{
	if (reg.Abs)
		reg.Negate = NEGATE_NONE;

	if (opcode == OPCODE_KIL ||
	    opcode == OPCODE_TEX ||
	    opcode == OPCODE_TXB ||
	    opcode == OPCODE_TXP) {
		int j;

		if (reg.Abs || reg.Negate)
			return GL_FALSE;

		for(j = 0; j < 4; ++j) {
			GLuint swz = GET_SWZ(reg.Swizzle, j);
			if (swz == SWIZZLE_NIL)
				continue;
			if (swz != j)
				return GL_FALSE;
		}

		return GL_TRUE;
	}

	GLuint relevant = 0;
	int j;

	for(j = 0; j < 3; ++j)
		if (GET_SWZ(reg.Swizzle, j) != SWIZZLE_NIL)
			relevant |= 1 << j;

	if ((reg.Negate & relevant) && ((reg.Negate & relevant) != relevant))
		return GL_FALSE;

	if (!lookup_native_swizzle(reg.Swizzle))
		return GL_FALSE;

	return GL_TRUE;
}


/**
 * Generate MOV dst, src using only native swizzles.
 */
void r300FPBuildSwizzle(struct nqssadce_state *s, struct prog_dst_register dst, struct prog_src_register src)
{
	if (src.Abs)
		src.Negate = NEGATE_NONE;

	while(dst.WriteMask) {
		const struct swizzle_data *best_swizzle = 0;
		GLuint best_matchcount = 0;
		GLuint best_matchmask = 0;
		int i, comp;

		for(i = 0; i < num_native_swizzles; ++i) {
			const struct swizzle_data *sd = &native_swizzles[i];
			GLuint matchcount = 0;
			GLuint matchmask = 0;
			for(comp = 0; comp < 3; ++comp) {
				if (!GET_BIT(dst.WriteMask, comp))
					continue;
				GLuint swz = GET_SWZ(src.Swizzle, comp);
				if (swz == SWIZZLE_NIL)
					continue;
				if (swz == GET_SWZ(sd->hash, comp)) {
					/* check if the negate bit of current component
					 * is the same for already matched components */
					if (matchmask && (!!(src.Negate & matchmask) != !!(src.Negate & (1 << comp))))
						continue;

					matchcount++;
					matchmask |= 1 << comp;
				}
			}
			if (matchcount > best_matchcount) {
				best_swizzle = sd;
				best_matchcount = matchcount;
				best_matchmask = matchmask;
				if (matchmask == (dst.WriteMask & WRITEMASK_XYZ))
					break;
			}
		}

		struct rc_instruction *inst = rc_insert_new_instruction(s->Compiler, s->IP->Prev);
		inst->I.Opcode = OPCODE_MOV;
		inst->I.DstReg = dst;
		inst->I.DstReg.WriteMask &= (best_matchmask | WRITEMASK_W);
		inst->I.SrcReg[0] = src;
		inst->I.SrcReg[0].Negate = (best_matchmask & src.Negate) ? NEGATE_XYZW : NEGATE_NONE;
		/* Note: We rely on NqSSA/DCE to set unused swizzle components to NIL */

		dst.WriteMask &= ~inst->I.DstReg.WriteMask;
	}
}


/**
 * Translate an RGB (XYZ) swizzle into the hardware code for the given
 * instruction source.
 */
GLuint r300FPTranslateRGBSwizzle(GLuint src, GLuint swizzle)
{
	const struct swizzle_data* sd = lookup_native_swizzle(swizzle);

	if (!sd) {
		_mesa_printf("Not a native swizzle: %08x\n", swizzle);
		return 0;
	}

	return sd->base + src*sd->stride;
}


/**
 * Translate an Alpha (W) swizzle into the hardware code for the given
 * instruction source.
 */
GLuint r300FPTranslateAlphaSwizzle(GLuint src, GLuint swizzle)
{
	if (swizzle < 3)
		return swizzle + 3*src;

	switch(swizzle) {
	case SWIZZLE_W: return R300_ALU_ARGA_SRC0A + src;
	case SWIZZLE_ONE: return R300_ALU_ARGA_ONE;
	case SWIZZLE_ZERO: return R300_ALU_ARGA_ZERO;
	default: return R300_ALU_ARGA_ONE;
	}
}