fda67b15073c5e90c8395eda2b498574ed29c321
[mesa.git] / src / mesa / drivers / dri / nouveau / nv10_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 "nv10_3d.xml.h"
32 #include "nouveau_util.h"
33 #include "nv10_driver.h"
34
35 void
36 nv10_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 *celsius = 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, celsius,
52 NV10_3D_TEX_GEN_COEFF(i, j), 4);
53 OUT_RINGp(chan, k, 4);
54 }
55
56 BEGIN_RING(chan, celsius, NV10_3D_TEX_GEN_MODE(i,j), 1);
57 OUT_RING(chan, nvgl_texgen_mode(coord->Mode));
58
59 } else {
60 BEGIN_RING(chan, celsius, NV10_3D_TEX_GEN_MODE(i,j), 1);
61 OUT_RING(chan, 0);
62 }
63 }
64
65 context_dirty_i(ctx, TEX_MAT, i);
66 }
67
68 void
69 nv10_emit_tex_mat(struct gl_context *ctx, int emit)
70 {
71 const int i = emit - NOUVEAU_STATE_TEX_MAT0;
72 struct nouveau_context *nctx = to_nouveau_context(ctx);
73 struct nouveau_channel *chan = context_chan(ctx);
74 struct nouveau_grobj *celsius = context_eng3d(ctx);
75
76 if (nctx->fallback == HWTNL &&
77 ((ctx->Texture._TexMatEnabled & 1 << i) ||
78 ctx->Texture.Unit[i]._GenFlags)) {
79 BEGIN_RING(chan, celsius, NV10_3D_TEX_MATRIX_ENABLE(i), 1);
80 OUT_RING(chan, 1);
81
82 BEGIN_RING(chan, celsius, NV10_3D_TEX_MATRIX(i, 0), 16);
83 OUT_RINGm(chan, ctx->TextureMatrixStack[i].Top->m);
84
85 } else {
86 BEGIN_RING(chan, celsius, NV10_3D_TEX_MATRIX_ENABLE(i), 1);
87 OUT_RING(chan, 0);
88 }
89 }
90
91 static uint32_t
92 get_tex_format_pot(struct gl_texture_image *ti)
93 {
94 switch (ti->TexFormat) {
95 case MESA_FORMAT_ARGB8888:
96 return NV10_3D_TEX_FORMAT_FORMAT_A8R8G8B8;
97
98 case MESA_FORMAT_XRGB8888:
99 return NV10_3D_TEX_FORMAT_FORMAT_X8R8G8B8;
100
101 case MESA_FORMAT_ARGB1555:
102 return NV10_3D_TEX_FORMAT_FORMAT_A1R5G5B5;
103
104 case MESA_FORMAT_ARGB4444:
105 return NV10_3D_TEX_FORMAT_FORMAT_A4R4G4B4;
106
107 case MESA_FORMAT_RGB565:
108 return NV10_3D_TEX_FORMAT_FORMAT_R5G6B5;
109
110 case MESA_FORMAT_A8:
111 case MESA_FORMAT_I8:
112 return NV10_3D_TEX_FORMAT_FORMAT_I8;
113
114 case MESA_FORMAT_L8:
115 return NV10_3D_TEX_FORMAT_FORMAT_L8;
116
117 case MESA_FORMAT_CI8:
118 return NV10_3D_TEX_FORMAT_FORMAT_INDEX8;
119
120 default:
121 assert(0);
122 }
123 }
124
125 static uint32_t
126 get_tex_format_rect(struct gl_texture_image *ti)
127 {
128 switch (ti->TexFormat) {
129 case MESA_FORMAT_ARGB1555:
130 return NV10_3D_TEX_FORMAT_FORMAT_A1R5G5B5_RECT;
131
132 case MESA_FORMAT_RGB565:
133 return NV10_3D_TEX_FORMAT_FORMAT_R5G6B5_RECT;
134
135 case MESA_FORMAT_ARGB8888:
136 case MESA_FORMAT_XRGB8888:
137 return NV10_3D_TEX_FORMAT_FORMAT_A8R8G8B8_RECT;
138
139 case MESA_FORMAT_A8:
140 case MESA_FORMAT_L8:
141 case MESA_FORMAT_I8:
142 return NV10_3D_TEX_FORMAT_FORMAT_I8_RECT;
143
144 default:
145 assert(0);
146 }
147 }
148
149 void
150 nv10_emit_tex_obj(struct gl_context *ctx, int emit)
151 {
152 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
153 struct nouveau_channel *chan = context_chan(ctx);
154 struct nouveau_grobj *celsius = context_eng3d(ctx);
155 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
156 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
157 struct gl_texture_object *t;
158 struct nouveau_surface *s;
159 struct gl_texture_image *ti;
160 uint32_t tx_format, tx_filter, tx_enable;
161
162 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
163 BEGIN_RING(chan, celsius, NV10_3D_TEX_ENABLE(i), 1);
164 OUT_RING(chan, 0);
165 return;
166 }
167
168 t = ctx->Texture.Unit[i]._Current;
169 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
170 ti = t->Image[0][t->BaseLevel];
171
172 if (!nouveau_texture_validate(ctx, t))
173 return;
174
175 /* Recompute the texturing registers. */
176 tx_format = nvgl_wrap_mode(t->WrapT) << 28
177 | nvgl_wrap_mode(t->WrapS) << 24
178 | ti->HeightLog2 << 20
179 | ti->WidthLog2 << 16
180 | 5 << 4 | 1 << 12;
181
182 tx_filter = nvgl_filter_mode(t->MagFilter) << 28
183 | nvgl_filter_mode(t->MinFilter) << 24;
184
185 tx_enable = NV10_3D_TEX_ENABLE_ENABLE
186 | log2i(t->MaxAnisotropy) << 4;
187
188 if (t->Target == GL_TEXTURE_RECTANGLE) {
189 BEGIN_RING(chan, celsius, NV10_3D_TEX_NPOT_PITCH(i), 1);
190 OUT_RING(chan, s->pitch << 16);
191 BEGIN_RING(chan, celsius, NV10_3D_TEX_NPOT_SIZE(i), 1);
192 OUT_RING(chan, align(s->width, 2) << 16 | s->height);
193
194 tx_format |= get_tex_format_rect(ti);
195 } else {
196 tx_format |= get_tex_format_pot(ti);
197 }
198
199 if (t->MinFilter != GL_NEAREST &&
200 t->MinFilter != GL_LINEAR) {
201 int lod_min = t->MinLod;
202 int lod_max = MIN2(t->MaxLod, t->_MaxLambda);
203 int lod_bias = t->LodBias
204 + ctx->Texture.Unit[i].LodBias;
205
206 lod_max = CLAMP(lod_max, 0, 15);
207 lod_min = CLAMP(lod_min, 0, 15);
208 lod_bias = CLAMP(lod_bias, 0, 15);
209
210 tx_format |= NV10_3D_TEX_FORMAT_MIPMAP;
211 tx_filter |= lod_bias << 8;
212 tx_enable |= lod_min << 26
213 | lod_max << 14;
214 }
215
216 /* Write it to the hardware. */
217 nouveau_bo_mark(bctx, celsius, NV10_3D_TEX_FORMAT(i),
218 s->bo, tx_format, 0,
219 NV10_3D_TEX_FORMAT_DMA0,
220 NV10_3D_TEX_FORMAT_DMA1,
221 bo_flags | NOUVEAU_BO_OR);
222
223 nouveau_bo_markl(bctx, celsius, NV10_3D_TEX_OFFSET(i),
224 s->bo, s->offset, bo_flags);
225
226 BEGIN_RING(chan, celsius, NV10_3D_TEX_FILTER(i), 1);
227 OUT_RING(chan, tx_filter);
228
229 BEGIN_RING(chan, celsius, NV10_3D_TEX_ENABLE(i), 1);
230 OUT_RING(chan, tx_enable);
231 }
232