summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/nouveau/nv04_state_frag.c
blob: 34ee2962023ca2ee6a6238a0831b35fe91857cea (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2009 Francisco Jerez.
 * 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.
 *
 */

#include "nouveau_driver.h"
#include "nouveau_context.h"
#include "nouveau_util.h"
#include "nouveau_class.h"
#include "nv04_driver.h"

#define COMBINER_SHIFT(in)						\
	(NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT##in##_SHIFT	\
	 - NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT0_SHIFT)
#define COMBINER_SOURCE(reg)					\
	NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT0_##reg
#define COMBINER_INVERT					\
	NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_INVERSE0
#define COMBINER_ALPHA					\
	NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ALPHA0

struct combiner_state {
	int unit;
	GLboolean alpha;

	/* GL state */
	GLenum mode;
	GLenum *source;
	GLenum *operand;
	GLuint logscale;

	/* Derived HW state */
	uint32_t hw;
};

#define __INIT_COMBINER_ALPHA_A GL_TRUE
#define __INIT_COMBINER_ALPHA_RGB GL_FALSE

/* Initialize a combiner_state struct from the texture unit
 * context. */
#define INIT_COMBINER(chan, rc, i) do {				\
		struct gl_tex_env_combine_state *c =		\
			ctx->Texture.Unit[i]._CurrentCombine;	\
		(rc)->alpha = __INIT_COMBINER_ALPHA_##chan;	\
		(rc)->unit = i;					\
		(rc)->mode = c->Mode##chan;			\
		(rc)->source = c->Source##chan;			\
		(rc)->operand = c->Operand##chan;		\
		(rc)->logscale = c->ScaleShift##chan;		\
		(rc)->hw = 0;					\
	} while (0)

/* Get the combiner source for the specified EXT_texture_env_combine
 * argument. */
static uint32_t
get_arg_source(struct combiner_state *rc, int arg)
{
	switch (rc->source[arg]) {
	case GL_TEXTURE:
		return rc->unit ? COMBINER_SOURCE(TEXTURE1) :
			COMBINER_SOURCE(TEXTURE0);

	case GL_TEXTURE0:
		return COMBINER_SOURCE(TEXTURE0);

	case GL_TEXTURE1:
		return COMBINER_SOURCE(TEXTURE1);

	case GL_CONSTANT:
		return COMBINER_SOURCE(CONSTANT);

	case GL_PRIMARY_COLOR:
		return COMBINER_SOURCE(PRIMARY_COLOR);

	case GL_PREVIOUS:
		return rc->unit ? COMBINER_SOURCE(PREVIOUS) :
			COMBINER_SOURCE(PRIMARY_COLOR);

	default:
		assert(0);
	}
}

/* Get the (possibly inverted) combiner input mapping for the
 * specified argument. */
#define INVERT 0x1

static uint32_t
get_arg_mapping(struct combiner_state *rc, int arg, int flags)
{
	int map = 0;

	switch (rc->operand[arg]) {
	case GL_SRC_COLOR:
	case GL_ONE_MINUS_SRC_COLOR:
		break;

	case GL_SRC_ALPHA:
	case GL_ONE_MINUS_SRC_ALPHA:
		map |= rc->alpha ? 0 : COMBINER_ALPHA;
		break;
	}

	switch (rc->operand[arg]) {
	case GL_SRC_COLOR:
	case GL_SRC_ALPHA:
		map |= flags & INVERT ? COMBINER_INVERT : 0;
		break;

	case GL_ONE_MINUS_SRC_COLOR:
	case GL_ONE_MINUS_SRC_ALPHA:
		map |= flags & INVERT ? 0 : COMBINER_INVERT;
		break;
	}

	return map;
}

/* Bind the combiner input <in> to the combiner source <src>,
 * possibly inverted. */
#define INPUT_SRC(rc, in, src, flags)					\
	(rc)->hw |= ((flags & INVERT ? COMBINER_INVERT : 0) |		\
		   COMBINER_SOURCE(src)) << COMBINER_SHIFT(in)

/* Bind the combiner input <in> to the EXT_texture_env_combine
 * argument <arg>, possibly inverted. */
#define INPUT_ARG(rc, in, arg, flags)					\
	(rc)->hw |= (get_arg_source(rc, arg) |				\
		     get_arg_mapping(rc, arg, flags)) << COMBINER_SHIFT(in)

#define UNSIGNED_OP(rc)							\
	(rc)->hw |= ((rc)->logscale ?					\
		     NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_SCALE2 :	\
		     NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_IDENTITY)
#define SIGNED_OP(rc)							\
	(rc)->hw |= ((rc)->logscale ?					\
		     NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_BIAS_SCALE2 : \
		     NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_BIAS)

static void
setup_combiner(struct combiner_state *rc)
{
	switch (rc->mode) {
	case GL_REPLACE:
		INPUT_ARG(rc, 0, 0, 0);
		INPUT_SRC(rc, 1, ZERO, INVERT);
		INPUT_SRC(rc, 2, ZERO, 0);
		INPUT_SRC(rc, 3, ZERO, 0);
		UNSIGNED_OP(rc);
		break;

	case GL_MODULATE:
		INPUT_ARG(rc, 0, 0, 0);
		INPUT_ARG(rc, 1, 1, 0);
		INPUT_SRC(rc, 2, ZERO, 0);
		INPUT_SRC(rc, 3, ZERO, 0);
		UNSIGNED_OP(rc);
		break;

	case GL_ADD:
		INPUT_ARG(rc, 0, 0, 0);
		INPUT_SRC(rc, 1, ZERO, INVERT);
		INPUT_ARG(rc, 2, 1, 0);
		INPUT_SRC(rc, 3, ZERO, INVERT);
		UNSIGNED_OP(rc);
		break;

	case GL_INTERPOLATE:
		INPUT_ARG(rc, 0, 0, 0);
		INPUT_ARG(rc, 1, 2, 0);
		INPUT_ARG(rc, 2, 1, 0);
		INPUT_ARG(rc, 3, 2, INVERT);
		UNSIGNED_OP(rc);
		break;

	case GL_ADD_SIGNED:
		INPUT_ARG(rc, 0, 0, 0);
		INPUT_SRC(rc, 1, ZERO, INVERT);
		INPUT_ARG(rc, 2, 1, 0);
		INPUT_SRC(rc, 3, ZERO, INVERT);
		SIGNED_OP(rc);
		break;

	default:
		assert(0);
	}
}

void
nv04_emit_tex_env(GLcontext *ctx, int emit)
{
	const int i = emit - NOUVEAU_STATE_TEX_ENV0;
	struct nouveau_channel *chan = context_chan(ctx);
	struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
	struct combiner_state rc_a = {}, rc_c = {};

	if (!nv04_mtex_engine(fahrenheit)) {
		context_dirty(ctx, BLEND);
		return;
	}

	/* Compute the new combiner state. */
	if (ctx->Texture.Unit[i]._ReallyEnabled) {
		INIT_COMBINER(A, &rc_a, i);
		setup_combiner(&rc_a);

		INIT_COMBINER(RGB, &rc_c, i);
		setup_combiner(&rc_c);

	} else {
		if (i == 0) {
			INPUT_SRC(&rc_a, 0, PRIMARY_COLOR, 0);
			INPUT_SRC(&rc_c, 0, PRIMARY_COLOR, 0);
		} else {
			INPUT_SRC(&rc_a, 0, PREVIOUS, 0);
			INPUT_SRC(&rc_c, 0, PREVIOUS, 0);
		}

		INPUT_SRC(&rc_a, 1, ZERO, INVERT);
		INPUT_SRC(&rc_c, 1, ZERO, INVERT);
		INPUT_SRC(&rc_a, 2, ZERO, 0);
		INPUT_SRC(&rc_c, 2, ZERO, 0);
		INPUT_SRC(&rc_a, 3, ZERO, 0);
		INPUT_SRC(&rc_c, 3, ZERO, 0);

		UNSIGNED_OP(&rc_a);
		UNSIGNED_OP(&rc_c);
	}

	/* Write the register combiner state out to the hardware. */
	BEGIN_RING(chan, fahrenheit,
		   NV04_MULTITEX_TRIANGLE_COMBINE_ALPHA(i), 2);
	OUT_RING(chan, rc_a.hw);
	OUT_RING(chan, rc_c.hw);

	BEGIN_RING(chan, fahrenheit,
		   NV04_MULTITEX_TRIANGLE_COMBINE_FACTOR, 1);
	OUT_RING(chan, pack_rgba_f(MESA_FORMAT_ARGB8888,
				   ctx->Texture.Unit[0].EnvColor));
}