bb8a79c2c92a9aafbf95038f4157f6159163475a
[mesa.git] / src / mesa / drivers / dri / nouveau / nv20_state_tex.c
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 "nouveau_class.h"
32 #include "nouveau_util.h"
33 #include "nv20_driver.h"
34
35 #define TX_GEN_MODE(i, j) (NV20TCL_TX_GEN_MODE_S(i) + 4 * (j))
36 #define TX_GEN_COEFF(i, j) (NV20TCL_TX_GEN_COEFF_S_A(i) + 16 * (j))
37 #define TX_MATRIX(i) (NV20TCL_TX0_MATRIX(0) + 64 * (i))
38
39 void
40 nv20_emit_tex_gen(GLcontext *ctx, int emit)
41 {
42 const int i = emit - NOUVEAU_STATE_TEX_GEN0;
43 struct nouveau_context *nctx = to_nouveau_context(ctx);
44 struct nouveau_channel *chan = context_chan(ctx);
45 struct nouveau_grobj *kelvin = context_eng3d(ctx);
46 struct gl_texture_unit *unit = &ctx->Texture.Unit[i];
47 int j;
48
49 for (j = 0; j < 4; j++) {
50 if (nctx->fallback == HWTNL && (unit->TexGenEnabled & 1 << j)) {
51 struct gl_texgen *coord = get_texgen_coord(unit, j);
52 float *k = get_texgen_coeff(coord);
53
54 if (k) {
55 BEGIN_RING(chan, kelvin, TX_GEN_COEFF(i, j), 4);
56 OUT_RINGf(chan, k[0]);
57 OUT_RINGf(chan, k[1]);
58 OUT_RINGf(chan, k[2]);
59 OUT_RINGf(chan, k[3]);
60 }
61
62 BEGIN_RING(chan, kelvin, TX_GEN_MODE(i, j), 1);
63 OUT_RING(chan, nvgl_texgen_mode(coord->Mode));
64
65 } else {
66 BEGIN_RING(chan, kelvin, TX_GEN_MODE(i, j), 1);
67 OUT_RING(chan, 0);
68 }
69 }
70 }
71
72 void
73 nv20_emit_tex_mat(GLcontext *ctx, int emit)
74 {
75 const int i = emit - NOUVEAU_STATE_TEX_MAT0;
76 struct nouveau_context *nctx = to_nouveau_context(ctx);
77 struct nouveau_channel *chan = context_chan(ctx);
78 struct nouveau_grobj *kelvin = context_eng3d(ctx);
79
80 if (nctx->fallback == HWTNL &&
81 (ctx->Texture._TexMatEnabled & 1 << i)) {
82 BEGIN_RING(chan, kelvin, NV20TCL_TX_MATRIX_ENABLE(i), 1);
83 OUT_RING(chan, 1);
84
85 BEGIN_RING(chan, kelvin, TX_MATRIX(i), 16);
86 OUT_RINGm(chan, ctx->TextureMatrixStack[i].Top->m);
87
88 } else {
89 BEGIN_RING(chan, kelvin, NV20TCL_TX_MATRIX_ENABLE(i), 1);
90 OUT_RING(chan, 0);
91 }
92 }
93
94 static uint32_t
95 get_tex_format_pot(struct gl_texture_image *ti)
96 {
97 switch (ti->TexFormat) {
98 case MESA_FORMAT_ARGB8888:
99 return NV20TCL_TX_FORMAT_FORMAT_A8R8G8B8;
100
101 case MESA_FORMAT_ARGB1555:
102 return NV20TCL_TX_FORMAT_FORMAT_A1R5G5B5;
103
104 case MESA_FORMAT_ARGB4444:
105 return NV20TCL_TX_FORMAT_FORMAT_A4R4G4B4;
106
107 case MESA_FORMAT_XRGB8888:
108 return NV20TCL_TX_FORMAT_FORMAT_X8R8G8B8;
109
110 case MESA_FORMAT_RGB565:
111 return NV20TCL_TX_FORMAT_FORMAT_R5G6B5;
112
113 case MESA_FORMAT_A8:
114 case MESA_FORMAT_I8:
115 return NV20TCL_TX_FORMAT_FORMAT_A8;
116
117 case MESA_FORMAT_L8:
118 return NV20TCL_TX_FORMAT_FORMAT_L8;
119
120 case MESA_FORMAT_CI8:
121 return NV20TCL_TX_FORMAT_FORMAT_INDEX8;
122
123 default:
124 assert(0);
125 }
126 }
127
128 static uint32_t
129 get_tex_format_rect(struct gl_texture_image *ti)
130 {
131 switch (ti->TexFormat) {
132 case MESA_FORMAT_ARGB8888:
133 return NV20TCL_TX_FORMAT_FORMAT_A8R8G8B8_RECT;
134
135 case MESA_FORMAT_ARGB1555:
136 return NV20TCL_TX_FORMAT_FORMAT_A1R5G5B5_RECT;
137
138 case MESA_FORMAT_ARGB4444:
139 return NV20TCL_TX_FORMAT_FORMAT_A4R4G4B4_RECT;
140
141 case MESA_FORMAT_XRGB8888:
142 return NV20TCL_TX_FORMAT_FORMAT_R8G8B8_RECT;
143
144 case MESA_FORMAT_RGB565:
145 return NV20TCL_TX_FORMAT_FORMAT_R5G6B5_RECT;
146
147 case MESA_FORMAT_L8:
148 return NV20TCL_TX_FORMAT_FORMAT_L8_RECT;
149
150 case MESA_FORMAT_A8:
151 case MESA_FORMAT_I8:
152 return NV20TCL_TX_FORMAT_FORMAT_A8_RECT;
153
154 default:
155 assert(0);
156 }
157 }
158
159 void
160 nv20_emit_tex_obj(GLcontext *ctx, int emit)
161 {
162 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
163 struct nouveau_channel *chan = context_chan(ctx);
164 struct nouveau_grobj *kelvin = context_eng3d(ctx);
165 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
166 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
167 struct gl_texture_object *t;
168 struct nouveau_surface *s;
169 struct gl_texture_image *ti;
170 uint32_t tx_format, tx_filter, tx_wrap, tx_enable;
171
172 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
173 BEGIN_RING(chan, kelvin, NV20TCL_TX_ENABLE(i), 1);
174 OUT_RING(chan, 0);
175
176 context_dirty(ctx, TEX_SHADER);
177 return;
178 }
179
180 t = ctx->Texture.Unit[i]._Current;
181 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
182 ti = t->Image[0][t->BaseLevel];
183
184 if (!nouveau_texture_validate(ctx, t))
185 return;
186
187 /* Recompute the texturing registers. */
188 tx_format = ti->DepthLog2 << 28
189 | ti->HeightLog2 << 24
190 | ti->WidthLog2 << 20
191 | NV20TCL_TX_FORMAT_DIMS_2D
192 | NV20TCL_TX_FORMAT_NO_BORDER
193 | 1 << 16;
194
195 tx_wrap = nvgl_wrap_mode(t->WrapR) << 16
196 | nvgl_wrap_mode(t->WrapT) << 8
197 | nvgl_wrap_mode(t->WrapS) << 0;
198
199 tx_filter = nvgl_filter_mode(t->MagFilter) << 24
200 | nvgl_filter_mode(t->MinFilter) << 16;
201
202 tx_enable = NV20TCL_TX_ENABLE_ENABLE
203 | log2i(t->MaxAnisotropy) << 4;
204
205 if (t->Target == GL_TEXTURE_RECTANGLE) {
206 BEGIN_RING(chan, kelvin, NV20TCL_TX_NPOT_PITCH(i), 1);
207 OUT_RING(chan, s->pitch << 16);
208 BEGIN_RING(chan, kelvin, NV20TCL_TX_NPOT_SIZE(i), 1);
209 OUT_RING(chan, s->width << 16 | s->height);
210
211 tx_format |= get_tex_format_rect(ti);
212 } else {
213 tx_format |= get_tex_format_pot(ti);
214 }
215
216 if (t->MinFilter != GL_NEAREST &&
217 t->MinFilter != GL_LINEAR) {
218 int lod_min = t->MinLod;
219 int lod_max = MIN2(t->MaxLod, t->_MaxLambda);
220 int lod_bias = t->LodBias
221 + ctx->Texture.Unit[i].LodBias;
222
223 lod_max = CLAMP(lod_max, 0, 15);
224 lod_min = CLAMP(lod_min, 0, 15);
225 lod_bias = CLAMP(lod_bias, 0, 15);
226
227 tx_format |= NV20TCL_TX_FORMAT_MIPMAP;
228 tx_filter |= lod_bias << 8;
229 tx_enable |= lod_min << 26
230 | lod_max << 14;
231 }
232
233 /* Write it to the hardware. */
234 nouveau_bo_mark(bctx, kelvin, NV20TCL_TX_FORMAT(i),
235 s->bo, tx_format, 0,
236 NV20TCL_TX_FORMAT_DMA0,
237 NV20TCL_TX_FORMAT_DMA1,
238 bo_flags | NOUVEAU_BO_OR);
239
240 nouveau_bo_markl(bctx, kelvin, NV20TCL_TX_OFFSET(i),
241 s->bo, 0, bo_flags);
242
243 BEGIN_RING(chan, kelvin, NV20TCL_TX_WRAP(i), 1);
244 OUT_RING(chan, tx_wrap);
245
246 BEGIN_RING(chan, kelvin, NV20TCL_TX_FILTER(i), 1);
247 OUT_RING(chan, tx_filter);
248
249 BEGIN_RING(chan, kelvin, NV20TCL_TX_ENABLE(i), 1);
250 OUT_RING(chan, tx_enable);
251
252 context_dirty(ctx, TEX_SHADER);
253 }
254
255 void
256 nv20_emit_tex_shader(GLcontext *ctx, int emit)
257 {
258 struct nouveau_channel *chan = context_chan(ctx);
259 struct nouveau_grobj *kelvin = context_eng3d(ctx);
260 uint32_t tx_shader_op = 0;
261 int i;
262
263 for (i = 0; i < NV20_TEXTURE_UNITS; i++) {
264 if (!ctx->Texture.Unit[i]._ReallyEnabled)
265 continue;
266
267 tx_shader_op |= NV20TCL_TX_SHADER_OP_TX0_TEXTURE_2D << 5 * i;
268 }
269
270 BEGIN_RING(chan, kelvin, NV20TCL_TX_SHADER_OP, 1);
271 OUT_RING(chan, tx_shader_op);
272 }