Merge branch '7.8'
[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 "nouveau_class.h"
32 #include "nouveau_util.h"
33 #include "nv10_driver.h"
34
35 #define TX_GEN_MODE(i, j) (NV10TCL_TX_GEN_MODE_S(i) + 4 * (j))
36 #define TX_GEN_COEFF(i, j) (NV10TCL_TX_GEN_COEFF_S_A(i) + 16 * (j))
37 #define TX_MATRIX(i) (NV10TCL_TX0_MATRIX(0) + 64 * (i))
38
39 void
40 nv10_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 *celsius = 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, celsius,
56 TX_GEN_COEFF(i, j), 4);
57 OUT_RINGp(chan, k, 4);
58 }
59
60 BEGIN_RING(chan, celsius, TX_GEN_MODE(i, j), 1);
61 OUT_RING(chan, nvgl_texgen_mode(coord->Mode));
62
63 } else {
64 BEGIN_RING(chan, celsius, TX_GEN_MODE(i, j), 1);
65 OUT_RING(chan, 0);
66 }
67 }
68
69 context_dirty_i(ctx, TEX_MAT, i);
70 }
71
72 void
73 nv10_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 *celsius = context_eng3d(ctx);
79
80 if (nctx->fallback == HWTNL &&
81 ((ctx->Texture._TexMatEnabled & 1 << i) ||
82 ctx->Texture.Unit[i]._GenFlags)) {
83 BEGIN_RING(chan, celsius, NV10TCL_TX_MATRIX_ENABLE(i), 1);
84 OUT_RING(chan, 1);
85
86 BEGIN_RING(chan, celsius, TX_MATRIX(i), 16);
87 OUT_RINGm(chan, ctx->TextureMatrixStack[i].Top->m);
88
89 } else {
90 BEGIN_RING(chan, celsius, NV10TCL_TX_MATRIX_ENABLE(i), 1);
91 OUT_RING(chan, 0);
92 }
93 }
94
95 static uint32_t
96 get_tex_format_pot(struct gl_texture_image *ti)
97 {
98 switch (ti->TexFormat) {
99 case MESA_FORMAT_ARGB8888:
100 return NV10TCL_TX_FORMAT_FORMAT_A8R8G8B8;
101
102 case MESA_FORMAT_XRGB8888:
103 return NV10TCL_TX_FORMAT_FORMAT_X8R8G8B8;
104
105 case MESA_FORMAT_ARGB1555:
106 return NV10TCL_TX_FORMAT_FORMAT_A1R5G5B5;
107
108 case MESA_FORMAT_ARGB4444:
109 return NV10TCL_TX_FORMAT_FORMAT_A4R4G4B4;
110
111 case MESA_FORMAT_RGB565:
112 return NV10TCL_TX_FORMAT_FORMAT_R5G6B5;
113
114 case MESA_FORMAT_A8:
115 case MESA_FORMAT_I8:
116 return NV10TCL_TX_FORMAT_FORMAT_A8;
117
118 case MESA_FORMAT_L8:
119 return NV10TCL_TX_FORMAT_FORMAT_L8;
120
121 case MESA_FORMAT_CI8:
122 return NV10TCL_TX_FORMAT_FORMAT_INDEX8;
123
124 default:
125 assert(0);
126 }
127 }
128
129 static uint32_t
130 get_tex_format_rect(struct gl_texture_image *ti)
131 {
132 switch (ti->TexFormat) {
133 case MESA_FORMAT_ARGB1555:
134 return NV10TCL_TX_FORMAT_FORMAT_A1R5G5B5_RECT;
135
136 case MESA_FORMAT_RGB565:
137 return NV10TCL_TX_FORMAT_FORMAT_R5G6B5_RECT;
138
139 case MESA_FORMAT_ARGB8888:
140 case MESA_FORMAT_XRGB8888:
141 return NV10TCL_TX_FORMAT_FORMAT_A8R8G8B8_RECT;
142
143 case MESA_FORMAT_A8:
144 case MESA_FORMAT_L8:
145 case MESA_FORMAT_I8:
146 return NV10TCL_TX_FORMAT_FORMAT_A8_RECT;
147
148 default:
149 assert(0);
150 }
151 }
152
153 void
154 nv10_emit_tex_obj(GLcontext *ctx, int emit)
155 {
156 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
157 struct nouveau_channel *chan = context_chan(ctx);
158 struct nouveau_grobj *celsius = context_eng3d(ctx);
159 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
160 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
161 struct gl_texture_object *t;
162 struct nouveau_surface *s;
163 struct gl_texture_image *ti;
164 uint32_t tx_format, tx_filter, tx_enable;
165
166 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
167 BEGIN_RING(chan, celsius, NV10TCL_TX_ENABLE(i), 1);
168 OUT_RING(chan, 0);
169 return;
170 }
171
172 t = ctx->Texture.Unit[i]._Current;
173 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
174 ti = t->Image[0][t->BaseLevel];
175
176 if (!nouveau_texture_validate(ctx, t))
177 return;
178
179 /* Recompute the texturing registers. */
180 tx_format = nvgl_wrap_mode(t->WrapT) << 28
181 | nvgl_wrap_mode(t->WrapS) << 24
182 | ti->HeightLog2 << 20
183 | ti->WidthLog2 << 16
184 | 5 << 4 | 1 << 12;
185
186 tx_filter = nvgl_filter_mode(t->MagFilter) << 28
187 | nvgl_filter_mode(t->MinFilter) << 24;
188
189 tx_enable = NV10TCL_TX_ENABLE_ENABLE
190 | log2i(t->MaxAnisotropy) << 4;
191
192 if (t->Target == GL_TEXTURE_RECTANGLE) {
193 BEGIN_RING(chan, celsius, NV10TCL_TX_NPOT_PITCH(i), 1);
194 OUT_RING(chan, s->pitch << 16);
195 BEGIN_RING(chan, celsius, NV10TCL_TX_NPOT_SIZE(i), 1);
196 OUT_RING(chan, align(s->width, 2) << 16 | s->height);
197
198 tx_format |= get_tex_format_rect(ti);
199 } else {
200 tx_format |= get_tex_format_pot(ti);
201 }
202
203 if (t->MinFilter != GL_NEAREST &&
204 t->MinFilter != GL_LINEAR) {
205 int lod_min = t->MinLod;
206 int lod_max = MIN2(t->MaxLod, t->_MaxLambda);
207 int lod_bias = t->LodBias
208 + ctx->Texture.Unit[i].LodBias;
209
210 lod_max = CLAMP(lod_max, 0, 15);
211 lod_min = CLAMP(lod_min, 0, 15);
212 lod_bias = CLAMP(lod_bias, 0, 15);
213
214 tx_format |= NV10TCL_TX_FORMAT_MIPMAP;
215 tx_filter |= lod_bias << 8;
216 tx_enable |= lod_min << 26
217 | lod_max << 14;
218 }
219
220 /* Write it to the hardware. */
221 nouveau_bo_mark(bctx, celsius, NV10TCL_TX_FORMAT(i),
222 s->bo, tx_format, 0,
223 NV10TCL_TX_FORMAT_DMA0,
224 NV10TCL_TX_FORMAT_DMA1,
225 bo_flags | NOUVEAU_BO_OR);
226
227 nouveau_bo_markl(bctx, celsius, NV10TCL_TX_OFFSET(i),
228 s->bo, 0, bo_flags);
229
230 BEGIN_RING(chan, celsius, NV10TCL_TX_FILTER(i), 1);
231 OUT_RING(chan, tx_filter);
232
233 BEGIN_RING(chan, celsius, NV10TCL_TX_ENABLE(i), 1);
234 OUT_RING(chan, tx_enable);
235 }
236