1 /*
2  * Copyright (C) 2009 Francisco Jerez.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26 
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_gldefs.h"
30 #include "nouveau_texture.h"
31 #include "nv20_3d.xml.h"
32 #include "nouveau_util.h"
33 #include "nv20_driver.h"
34 #include "main/samplerobj.h"
35 
36 void
nv20_emit_tex_gen(struct gl_context * ctx,int emit)37 nv20_emit_tex_gen(struct gl_context *ctx, int emit)
38 {
39 	const int i = emit - NOUVEAU_STATE_TEX_GEN0;
40 	struct nouveau_context *nctx = to_nouveau_context(ctx);
41 	struct nouveau_pushbuf *push = context_push(ctx);
42 	struct gl_texture_unit *unit = &ctx->Texture.Unit[i];
43 	int j;
44 
45 	for (j = 0; j < 4; j++) {
46 		if (nctx->fallback == HWTNL && (unit->TexGenEnabled & 1 << j)) {
47 			struct gl_texgen *coord = get_texgen_coord(unit, j);
48 			float *k = get_texgen_coeff(coord);
49 
50 			if (k) {
51 				BEGIN_NV04(push, NV20_3D(TEX_GEN_COEFF(i, j)), 4);
52 				PUSH_DATAp(push, k, 4);
53 			}
54 
55 			BEGIN_NV04(push, NV20_3D(TEX_GEN_MODE(i, j)), 1);
56 			PUSH_DATA (push, nvgl_texgen_mode(coord->Mode));
57 
58 		} else {
59 			BEGIN_NV04(push, NV20_3D(TEX_GEN_MODE(i, j)), 1);
60 			PUSH_DATA (push, 0);
61 		}
62 	}
63 }
64 
65 void
nv20_emit_tex_mat(struct gl_context * ctx,int emit)66 nv20_emit_tex_mat(struct gl_context *ctx, int emit)
67 {
68 	const int i = emit - NOUVEAU_STATE_TEX_MAT0;
69 	struct nouveau_context *nctx = to_nouveau_context(ctx);
70 	struct nouveau_pushbuf *push = context_push(ctx);
71 
72 	if (nctx->fallback == HWTNL &&
73 	    (ctx->Texture._TexMatEnabled & 1 << i)) {
74 		BEGIN_NV04(push, NV20_3D(TEX_MATRIX_ENABLE(i)), 1);
75 		PUSH_DATA (push, 1);
76 
77 		BEGIN_NV04(push, NV20_3D(TEX_MATRIX(i,0)), 16);
78 		PUSH_DATAm(push, ctx->TextureMatrixStack[i].Top->m);
79 
80 	} else {
81 		BEGIN_NV04(push, NV20_3D(TEX_MATRIX_ENABLE(i)), 1);
82 		PUSH_DATA (push, 0);
83 	}
84 }
85 
86 static uint32_t
get_tex_format_pot(struct gl_texture_image * ti)87 get_tex_format_pot(struct gl_texture_image *ti)
88 {
89 	switch (ti->TexFormat) {
90 	case MESA_FORMAT_B8G8R8A8_UNORM:
91 		return NV20_3D_TEX_FORMAT_FORMAT_A8R8G8B8;
92 
93 	case MESA_FORMAT_B5G5R5A1_UNORM:
94 		return NV20_3D_TEX_FORMAT_FORMAT_A1R5G5B5;
95 
96 	case MESA_FORMAT_B4G4R4A4_UNORM:
97 		return NV20_3D_TEX_FORMAT_FORMAT_A4R4G4B4;
98 
99 	case MESA_FORMAT_B8G8R8X8_UNORM:
100 		return NV20_3D_TEX_FORMAT_FORMAT_X8R8G8B8;
101 
102 	case MESA_FORMAT_B5G6R5_UNORM:
103 		return NV20_3D_TEX_FORMAT_FORMAT_R5G6B5;
104 
105 	case MESA_FORMAT_A_UNORM8:
106 	case MESA_FORMAT_I_UNORM8:
107 		return NV20_3D_TEX_FORMAT_FORMAT_I8;
108 
109 	case MESA_FORMAT_L_UNORM8:
110 		return NV20_3D_TEX_FORMAT_FORMAT_L8;
111 
112 	case MESA_FORMAT_RGB_DXT1:
113 	case MESA_FORMAT_RGBA_DXT1:
114 		return NV20_3D_TEX_FORMAT_FORMAT_DXT1;
115 
116 	case MESA_FORMAT_RGBA_DXT3:
117 		return NV20_3D_TEX_FORMAT_FORMAT_DXT3;
118 
119 	case MESA_FORMAT_RGBA_DXT5:
120 		return NV20_3D_TEX_FORMAT_FORMAT_DXT5;
121 
122 	default:
123 		assert(0);
124 	}
125 }
126 
127 static uint32_t
get_tex_format_rect(struct gl_texture_image * ti)128 get_tex_format_rect(struct gl_texture_image *ti)
129 {
130 	switch (ti->TexFormat) {
131 	case MESA_FORMAT_B8G8R8A8_UNORM:
132 		return NV20_3D_TEX_FORMAT_FORMAT_A8R8G8B8_RECT;
133 
134 	case MESA_FORMAT_B5G5R5A1_UNORM:
135 		return NV20_3D_TEX_FORMAT_FORMAT_A1R5G5B5_RECT;
136 
137 	case MESA_FORMAT_B4G4R4A4_UNORM:
138 		return NV20_3D_TEX_FORMAT_FORMAT_A4R4G4B4_RECT;
139 
140 	case MESA_FORMAT_B8G8R8X8_UNORM:
141 		return NV20_3D_TEX_FORMAT_FORMAT_R8G8B8_RECT;
142 
143 	case MESA_FORMAT_B5G6R5_UNORM:
144 		return NV20_3D_TEX_FORMAT_FORMAT_R5G6B5_RECT;
145 
146 	case MESA_FORMAT_L_UNORM8:
147 		return NV20_3D_TEX_FORMAT_FORMAT_L8_RECT;
148 
149 	case MESA_FORMAT_A_UNORM8:
150 	case MESA_FORMAT_I_UNORM8:
151 		return NV20_3D_TEX_FORMAT_FORMAT_I8_RECT;
152 
153 	default:
154 		assert(0);
155 	}
156 }
157 
158 void
nv20_emit_tex_obj(struct gl_context * ctx,int emit)159 nv20_emit_tex_obj(struct gl_context *ctx, int emit)
160 {
161 	const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
162 	struct nouveau_pushbuf *push = context_push(ctx);
163 	const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
164 	struct gl_texture_object *t;
165 	struct nouveau_surface *s;
166 	struct gl_texture_image *ti;
167 	const struct gl_sampler_object *sa;
168 	uint8_t r, g, b, a;
169 	uint32_t tx_format, tx_filter, tx_wrap, tx_bcolor, tx_enable;
170 
171 	PUSH_RESET(push, BUFCTX_TEX(i));
172 
173 	if (!ctx->Texture.Unit[i]._Current) {
174 		BEGIN_NV04(push, NV20_3D(TEX_ENABLE(i)), 1);
175 		PUSH_DATA (push, 0);
176 
177 		context_dirty(ctx, TEX_SHADER);
178 		return;
179 	}
180 
181 	t = ctx->Texture.Unit[i]._Current;
182 	s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
183 	ti = t->Image[0][t->BaseLevel];
184 	sa = _mesa_get_samplerobj(ctx, i);
185 
186 	if (!nouveau_texture_validate(ctx, t))
187 		return;
188 
189 	/* Recompute the texturing registers. */
190 	tx_format = ti->DepthLog2 << 28
191 		| ti->HeightLog2 << 24
192 		| ti->WidthLog2 << 20
193 		| NV20_3D_TEX_FORMAT_DIMS_2D
194 		| NV20_3D_TEX_FORMAT_NO_BORDER
195 		| 1 << 16;
196 
197 	switch (t->Target) {
198 	case GL_TEXTURE_1D:
199 		tx_wrap = NV20_3D_TEX_WRAP_R_CLAMP_TO_EDGE
200 			| NV20_3D_TEX_WRAP_T_CLAMP_TO_EDGE
201 			| nvgl_wrap_mode_nv20(sa->WrapS) << 0;
202 		break;
203 
204 	default:
205 		tx_wrap = nvgl_wrap_mode_nv20(sa->WrapR) << 16
206 			| nvgl_wrap_mode_nv20(sa->WrapT) << 8
207 			| nvgl_wrap_mode_nv20(sa->WrapS) << 0;
208 		break;
209 	}
210 
211 	tx_filter = nvgl_filter_mode(sa->MagFilter) << 24
212 		| nvgl_filter_mode(sa->MinFilter) << 16
213 		| 2 << 12;
214 
215 	r = FLOAT_TO_UBYTE(sa->BorderColor.f[0]);
216 	g = FLOAT_TO_UBYTE(sa->BorderColor.f[1]);
217 	b = FLOAT_TO_UBYTE(sa->BorderColor.f[2]);
218 	a = FLOAT_TO_UBYTE(sa->BorderColor.f[3]);
219 	switch (ti->_BaseFormat) {
220 	case GL_LUMINANCE:
221 		a = 0xff;
222 		/* fallthrough */
223 	case GL_LUMINANCE_ALPHA:
224 		g = b = r;
225 		break;
226 	case GL_RGB:
227 		a = 0xff;
228 		break;
229 	case GL_INTENSITY:
230 		g = b = a = r;
231 		break;
232 	case GL_ALPHA:
233 		r = g = b = 0;
234 		break;
235 	}
236 	tx_bcolor = b << 0 | g << 8 | r << 16 | a << 24;
237 
238 	tx_enable = NV20_3D_TEX_ENABLE_ENABLE
239 		| log2i(sa->MaxAnisotropy) << 4;
240 
241 	if (t->Target == GL_TEXTURE_RECTANGLE) {
242 		BEGIN_NV04(push, NV20_3D(TEX_NPOT_PITCH(i)), 1);
243 		PUSH_DATA (push, s->pitch << 16);
244 		BEGIN_NV04(push, NV20_3D(TEX_NPOT_SIZE(i)), 1);
245 		PUSH_DATA (push, s->width << 16 | s->height);
246 
247 		tx_format |= get_tex_format_rect(ti);
248 	} else {
249 		tx_format |= get_tex_format_pot(ti);
250 	}
251 
252 	if (sa->MinFilter != GL_NEAREST &&
253 	    sa->MinFilter != GL_LINEAR) {
254 		int lod_min = sa->MinLod;
255 		int lod_max = MIN2(sa->MaxLod, t->_MaxLambda);
256 		int lod_bias = sa->LodBias
257 			+ ctx->Texture.Unit[i].LodBias;
258 
259 		lod_max = CLAMP(lod_max, 0, 15);
260 		lod_min = CLAMP(lod_min, 0, 15);
261 		lod_bias = CLAMP(lod_bias, 0, 15);
262 
263 		tx_format |= NV20_3D_TEX_FORMAT_MIPMAP;
264 		tx_filter |= lod_bias << 8;
265 		tx_enable |= lod_min << 26
266 			| lod_max << 14;
267 	}
268 
269 	/* Write it to the hardware. */
270 	BEGIN_NV04(push, NV20_3D(TEX_FORMAT(i)), 1);
271 	PUSH_MTHD (push, NV20_3D(TEX_FORMAT(i)), BUFCTX_TEX(i),
272 			 s->bo, tx_format, bo_flags | NOUVEAU_BO_OR,
273 			 NV20_3D_TEX_FORMAT_DMA0,
274 			 NV20_3D_TEX_FORMAT_DMA1);
275 
276 	BEGIN_NV04(push, NV20_3D(TEX_OFFSET(i)), 1);
277 	PUSH_MTHDl(push, NV20_3D(TEX_OFFSET(i)), BUFCTX_TEX(i),
278 			 s->bo, s->offset, bo_flags);
279 
280 	BEGIN_NV04(push, NV20_3D(TEX_WRAP(i)), 1);
281 	PUSH_DATA (push, tx_wrap);
282 
283 	BEGIN_NV04(push, NV20_3D(TEX_FILTER(i)), 1);
284 	PUSH_DATA (push, tx_filter);
285 
286 	BEGIN_NV04(push, NV20_3D(TEX_BORDER_COLOR(i)), 1);
287 	PUSH_DATA (push, tx_bcolor);
288 
289 	BEGIN_NV04(push, NV20_3D(TEX_ENABLE(i)), 1);
290 	PUSH_DATA (push, tx_enable);
291 
292 	context_dirty(ctx, TEX_SHADER);
293 }
294 
295 void
nv20_emit_tex_shader(struct gl_context * ctx,int emit)296 nv20_emit_tex_shader(struct gl_context *ctx, int emit)
297 {
298 	struct nouveau_pushbuf *push = context_push(ctx);
299 	uint32_t tx_shader_op = 0;
300 	int i;
301 
302 	for (i = 0; i < NV20_TEXTURE_UNITS; i++) {
303 		if (!ctx->Texture.Unit[i]._Current)
304 			continue;
305 
306 		tx_shader_op |= NV20_3D_TEX_SHADER_OP_TX0_TEXTURE_2D << 5 * i;
307 	}
308 
309 	BEGIN_NV04(push, NV20_3D(TEX_SHADER_OP), 1);
310 	PUSH_DATA (push, tx_shader_op);
311 }
312