mesa: move software texel fetch code into swrast
[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 "nv20_3d.xml.h"
32 #include "nouveau_util.h"
33 #include "nv20_driver.h"
34
35 void
36 nv20_emit_tex_gen(struct gl_context *ctx, int emit)
37 {
38 const int i = emit - NOUVEAU_STATE_TEX_GEN0;
39 struct nouveau_context *nctx = to_nouveau_context(ctx);
40 struct nouveau_channel *chan = context_chan(ctx);
41 struct nouveau_grobj *kelvin = context_eng3d(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_RING(chan, kelvin,
52 NV20_3D_TEX_GEN_COEFF(i, j), 4);
53 OUT_RINGp(chan, k, 4);
54 }
55
56 BEGIN_RING(chan, kelvin, NV20_3D_TEX_GEN_MODE(i, j), 1);
57 OUT_RING(chan, nvgl_texgen_mode(coord->Mode));
58
59 } else {
60 BEGIN_RING(chan, kelvin, NV20_3D_TEX_GEN_MODE(i, j), 1);
61 OUT_RING(chan, 0);
62 }
63 }
64 }
65
66 void
67 nv20_emit_tex_mat(struct gl_context *ctx, int emit)
68 {
69 const int i = emit - NOUVEAU_STATE_TEX_MAT0;
70 struct nouveau_context *nctx = to_nouveau_context(ctx);
71 struct nouveau_channel *chan = context_chan(ctx);
72 struct nouveau_grobj *kelvin = context_eng3d(ctx);
73
74 if (nctx->fallback == HWTNL &&
75 (ctx->Texture._TexMatEnabled & 1 << i)) {
76 BEGIN_RING(chan, kelvin, NV20_3D_TEX_MATRIX_ENABLE(i), 1);
77 OUT_RING(chan, 1);
78
79 BEGIN_RING(chan, kelvin, NV20_3D_TEX_MATRIX(i,0), 16);
80 OUT_RINGm(chan, ctx->TextureMatrixStack[i].Top->m);
81
82 } else {
83 BEGIN_RING(chan, kelvin, NV20_3D_TEX_MATRIX_ENABLE(i), 1);
84 OUT_RING(chan, 0);
85 }
86 }
87
88 static uint32_t
89 get_tex_format_pot(struct gl_texture_image *ti)
90 {
91 switch (ti->TexFormat) {
92 case MESA_FORMAT_ARGB8888:
93 return NV20_3D_TEX_FORMAT_FORMAT_A8R8G8B8;
94
95 case MESA_FORMAT_ARGB1555:
96 return NV20_3D_TEX_FORMAT_FORMAT_A1R5G5B5;
97
98 case MESA_FORMAT_ARGB4444:
99 return NV20_3D_TEX_FORMAT_FORMAT_A4R4G4B4;
100
101 case MESA_FORMAT_XRGB8888:
102 return NV20_3D_TEX_FORMAT_FORMAT_X8R8G8B8;
103
104 case MESA_FORMAT_RGB565:
105 return NV20_3D_TEX_FORMAT_FORMAT_R5G6B5;
106
107 case MESA_FORMAT_A8:
108 case MESA_FORMAT_I8:
109 return NV20_3D_TEX_FORMAT_FORMAT_I8;
110
111 case MESA_FORMAT_L8:
112 return NV20_3D_TEX_FORMAT_FORMAT_L8;
113
114 default:
115 assert(0);
116 }
117 }
118
119 static uint32_t
120 get_tex_format_rect(struct gl_texture_image *ti)
121 {
122 switch (ti->TexFormat) {
123 case MESA_FORMAT_ARGB8888:
124 return NV20_3D_TEX_FORMAT_FORMAT_A8R8G8B8_RECT;
125
126 case MESA_FORMAT_ARGB1555:
127 return NV20_3D_TEX_FORMAT_FORMAT_A1R5G5B5_RECT;
128
129 case MESA_FORMAT_ARGB4444:
130 return NV20_3D_TEX_FORMAT_FORMAT_A4R4G4B4_RECT;
131
132 case MESA_FORMAT_XRGB8888:
133 return NV20_3D_TEX_FORMAT_FORMAT_R8G8B8_RECT;
134
135 case MESA_FORMAT_RGB565:
136 return NV20_3D_TEX_FORMAT_FORMAT_R5G6B5_RECT;
137
138 case MESA_FORMAT_L8:
139 return NV20_3D_TEX_FORMAT_FORMAT_L8_RECT;
140
141 case MESA_FORMAT_A8:
142 case MESA_FORMAT_I8:
143 return NV20_3D_TEX_FORMAT_FORMAT_I8_RECT;
144
145 default:
146 assert(0);
147 }
148 }
149
150 void
151 nv20_emit_tex_obj(struct gl_context *ctx, int emit)
152 {
153 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
154 struct nouveau_channel *chan = context_chan(ctx);
155 struct nouveau_grobj *kelvin = context_eng3d(ctx);
156 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
157 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
158 struct gl_texture_object *t;
159 struct nouveau_surface *s;
160 struct gl_texture_image *ti;
161 uint32_t tx_format, tx_filter, tx_wrap, tx_enable;
162
163 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
164 BEGIN_RING(chan, kelvin, NV20_3D_TEX_ENABLE(i), 1);
165 OUT_RING(chan, 0);
166
167 context_dirty(ctx, TEX_SHADER);
168 return;
169 }
170
171 t = ctx->Texture.Unit[i]._Current;
172 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
173 ti = t->Image[0][t->BaseLevel];
174
175 if (!nouveau_texture_validate(ctx, t))
176 return;
177
178 /* Recompute the texturing registers. */
179 tx_format = ti->DepthLog2 << 28
180 | ti->HeightLog2 << 24
181 | ti->WidthLog2 << 20
182 | NV20_3D_TEX_FORMAT_DIMS_2D
183 | NV20_3D_TEX_FORMAT_NO_BORDER
184 | 1 << 16;
185
186 tx_wrap = nvgl_wrap_mode(t->Sampler.WrapR) << 16
187 | nvgl_wrap_mode(t->Sampler.WrapT) << 8
188 | nvgl_wrap_mode(t->Sampler.WrapS) << 0;
189
190 tx_filter = nvgl_filter_mode(t->Sampler.MagFilter) << 24
191 | nvgl_filter_mode(t->Sampler.MinFilter) << 16
192 | 2 << 12;
193
194 tx_enable = NV20_3D_TEX_ENABLE_ENABLE
195 | log2i(t->Sampler.MaxAnisotropy) << 4;
196
197 if (t->Target == GL_TEXTURE_RECTANGLE) {
198 BEGIN_RING(chan, kelvin, NV20_3D_TEX_NPOT_PITCH(i), 1);
199 OUT_RING(chan, s->pitch << 16);
200 BEGIN_RING(chan, kelvin, NV20_3D_TEX_NPOT_SIZE(i), 1);
201 OUT_RING(chan, s->width << 16 | s->height);
202
203 tx_format |= get_tex_format_rect(ti);
204 } else {
205 tx_format |= get_tex_format_pot(ti);
206 }
207
208 if (t->Sampler.MinFilter != GL_NEAREST &&
209 t->Sampler.MinFilter != GL_LINEAR) {
210 int lod_min = t->Sampler.MinLod;
211 int lod_max = MIN2(t->Sampler.MaxLod, t->_MaxLambda);
212 int lod_bias = t->Sampler.LodBias
213 + ctx->Texture.Unit[i].LodBias;
214
215 lod_max = CLAMP(lod_max, 0, 15);
216 lod_min = CLAMP(lod_min, 0, 15);
217 lod_bias = CLAMP(lod_bias, 0, 15);
218
219 tx_format |= NV20_3D_TEX_FORMAT_MIPMAP;
220 tx_filter |= lod_bias << 8;
221 tx_enable |= lod_min << 26
222 | lod_max << 14;
223 }
224
225 /* Write it to the hardware. */
226 nouveau_bo_mark(bctx, kelvin, NV20_3D_TEX_FORMAT(i),
227 s->bo, tx_format, 0,
228 NV20_3D_TEX_FORMAT_DMA0,
229 NV20_3D_TEX_FORMAT_DMA1,
230 bo_flags | NOUVEAU_BO_OR);
231
232 nouveau_bo_markl(bctx, kelvin, NV20_3D_TEX_OFFSET(i),
233 s->bo, s->offset, bo_flags);
234
235 BEGIN_RING(chan, kelvin, NV20_3D_TEX_WRAP(i), 1);
236 OUT_RING(chan, tx_wrap);
237
238 BEGIN_RING(chan, kelvin, NV20_3D_TEX_FILTER(i), 1);
239 OUT_RING(chan, tx_filter);
240
241 BEGIN_RING(chan, kelvin, NV20_3D_TEX_ENABLE(i), 1);
242 OUT_RING(chan, tx_enable);
243
244 context_dirty(ctx, TEX_SHADER);
245 }
246
247 void
248 nv20_emit_tex_shader(struct gl_context *ctx, int emit)
249 {
250 struct nouveau_channel *chan = context_chan(ctx);
251 struct nouveau_grobj *kelvin = context_eng3d(ctx);
252 uint32_t tx_shader_op = 0;
253 int i;
254
255 for (i = 0; i < NV20_TEXTURE_UNITS; i++) {
256 if (!ctx->Texture.Unit[i]._ReallyEnabled)
257 continue;
258
259 tx_shader_op |= NV20_3D_TEX_SHADER_OP_TX0_TEXTURE_2D << 5 * i;
260 }
261
262 BEGIN_RING(chan, kelvin, NV20_3D_TEX_SHADER_OP, 1);
263 OUT_RING(chan, tx_shader_op);
264 }