2 * Copyright (C) 2009 Francisco Jerez.
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:
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.
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.
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"
36 nv10_emit_tex_gen(struct gl_context *ctx, int emit)
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];
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);
51 BEGIN_RING(chan, celsius,
52 NV10_3D_TEX_GEN_COEFF(i, j), 4);
53 OUT_RINGp(chan, k, 4);
56 BEGIN_RING(chan, celsius, NV10_3D_TEX_GEN_MODE(i,j), 1);
57 OUT_RING(chan, nvgl_texgen_mode(coord->Mode));
60 BEGIN_RING(chan, celsius, NV10_3D_TEX_GEN_MODE(i,j), 1);
65 context_dirty_i(ctx, TEX_MAT, i);
69 nv10_emit_tex_mat(struct gl_context *ctx, int emit)
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);
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);
82 BEGIN_RING(chan, celsius, NV10_3D_TEX_MATRIX(i, 0), 16);
83 OUT_RINGm(chan, ctx->TextureMatrixStack[i].Top->m);
86 BEGIN_RING(chan, celsius, NV10_3D_TEX_MATRIX_ENABLE(i), 1);
92 get_tex_format_pot(struct gl_texture_image *ti)
94 switch (ti->TexFormat) {
95 case MESA_FORMAT_ARGB8888:
96 return NV10_3D_TEX_FORMAT_FORMAT_A8R8G8B8;
98 case MESA_FORMAT_XRGB8888:
99 return NV10_3D_TEX_FORMAT_FORMAT_X8R8G8B8;
101 case MESA_FORMAT_ARGB1555:
102 return NV10_3D_TEX_FORMAT_FORMAT_A1R5G5B5;
104 case MESA_FORMAT_ARGB4444:
105 return NV10_3D_TEX_FORMAT_FORMAT_A4R4G4B4;
107 case MESA_FORMAT_RGB565:
108 return NV10_3D_TEX_FORMAT_FORMAT_R5G6B5;
112 return NV10_3D_TEX_FORMAT_FORMAT_I8;
115 return NV10_3D_TEX_FORMAT_FORMAT_L8;
117 case MESA_FORMAT_CI8:
118 return NV10_3D_TEX_FORMAT_FORMAT_INDEX8;
126 get_tex_format_rect(struct gl_texture_image *ti)
128 switch (ti->TexFormat) {
129 case MESA_FORMAT_ARGB1555:
130 return NV10_3D_TEX_FORMAT_FORMAT_A1R5G5B5_RECT;
132 case MESA_FORMAT_RGB565:
133 return NV10_3D_TEX_FORMAT_FORMAT_R5G6B5_RECT;
135 case MESA_FORMAT_ARGB8888:
136 case MESA_FORMAT_XRGB8888:
137 return NV10_3D_TEX_FORMAT_FORMAT_A8R8G8B8_RECT;
142 return NV10_3D_TEX_FORMAT_FORMAT_I8_RECT;
150 nv10_emit_tex_obj(struct gl_context *ctx, int emit)
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;
162 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
163 BEGIN_RING(chan, celsius, NV10_3D_TEX_ENABLE(i), 1);
168 t = ctx->Texture.Unit[i]._Current;
169 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
170 ti = t->Image[0][t->BaseLevel];
172 if (!nouveau_texture_validate(ctx, t))
175 /* Recompute the texturing registers. */
176 tx_format = nvgl_wrap_mode(t->Sampler.WrapT) << 28
177 | nvgl_wrap_mode(t->Sampler.WrapS) << 24
178 | ti->HeightLog2 << 20
179 | ti->WidthLog2 << 16
182 tx_filter = nvgl_filter_mode(t->Sampler.MagFilter) << 28
183 | nvgl_filter_mode(t->Sampler.MinFilter) << 24;
185 tx_enable = NV10_3D_TEX_ENABLE_ENABLE
186 | log2i(t->Sampler.MaxAnisotropy) << 4;
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);
194 tx_format |= get_tex_format_rect(ti);
196 tx_format |= get_tex_format_pot(ti);
199 if (t->Sampler.MinFilter != GL_NEAREST &&
200 t->Sampler.MinFilter != GL_LINEAR) {
201 int lod_min = t->Sampler.MinLod;
202 int lod_max = MIN2(t->Sampler.MaxLod, t->_MaxLambda);
203 int lod_bias = t->Sampler.LodBias
204 + ctx->Texture.Unit[i].LodBias;
206 lod_max = CLAMP(lod_max, 0, 15);
207 lod_min = CLAMP(lod_min, 0, 15);
208 lod_bias = CLAMP(lod_bias, 0, 15);
210 tx_format |= NV10_3D_TEX_FORMAT_MIPMAP;
211 tx_filter |= lod_bias << 8;
212 tx_enable |= lod_min << 26
216 /* Write it to the hardware. */
217 nouveau_bo_mark(bctx, celsius, NV10_3D_TEX_FORMAT(i),
219 NV10_3D_TEX_FORMAT_DMA0,
220 NV10_3D_TEX_FORMAT_DMA1,
221 bo_flags | NOUVEAU_BO_OR);
223 nouveau_bo_markl(bctx, celsius, NV10_3D_TEX_OFFSET(i),
224 s->bo, s->offset, bo_flags);
226 BEGIN_RING(chan, celsius, NV10_3D_TEX_FILTER(i), 1);
227 OUT_RING(chan, tx_filter);
229 BEGIN_RING(chan, celsius, NV10_3D_TEX_ENABLE(i), 1);
230 OUT_RING(chan, tx_enable);