7a5914d9b7fe5eda207ccc42222dbfa2c86337f8
[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 static uint32_t
36 get_tex_format_pot(struct gl_texture_image *ti)
37 {
38 switch (ti->TexFormat) {
39 case MESA_FORMAT_ARGB8888:
40 return NV20TCL_TX_FORMAT_FORMAT_A8R8G8B8;
41
42 case MESA_FORMAT_ARGB1555:
43 return NV20TCL_TX_FORMAT_FORMAT_A1R5G5B5;
44
45 case MESA_FORMAT_ARGB4444:
46 return NV20TCL_TX_FORMAT_FORMAT_A4R4G4B4;
47
48 case MESA_FORMAT_RGB565:
49 return NV20TCL_TX_FORMAT_FORMAT_R5G6B5;
50
51 case MESA_FORMAT_A8:
52 case MESA_FORMAT_I8:
53 return NV20TCL_TX_FORMAT_FORMAT_A8;
54
55 case MESA_FORMAT_L8:
56 return NV20TCL_TX_FORMAT_FORMAT_L8;
57
58 case MESA_FORMAT_CI8:
59 return NV20TCL_TX_FORMAT_FORMAT_INDEX8;
60
61 default:
62 assert(0);
63 }
64 }
65
66 static uint32_t
67 get_tex_format_rect(struct gl_texture_image *ti)
68 {
69 switch (ti->TexFormat) {
70 case MESA_FORMAT_ARGB1555:
71 return NV20TCL_TX_FORMAT_FORMAT_A1R5G5B5_RECT;
72
73 case MESA_FORMAT_RGB565:
74 return NV20TCL_TX_FORMAT_FORMAT_R5G6B5_RECT;
75
76 case MESA_FORMAT_ARGB8888:
77 return NV20TCL_TX_FORMAT_FORMAT_A8R8G8B8_RECT;
78
79 case MESA_FORMAT_L8:
80 return NV20TCL_TX_FORMAT_FORMAT_L8_RECT;
81
82 case MESA_FORMAT_A8:
83 case MESA_FORMAT_I8:
84 return NV20TCL_TX_FORMAT_FORMAT_A8_RECT;
85
86 case MESA_FORMAT_ARGB4444:
87 return NV20TCL_TX_FORMAT_FORMAT_A4R4G4B4_RECT;
88
89 default:
90 assert(0);
91 }
92 }
93
94 void
95 nv20_emit_tex_obj(GLcontext *ctx, int emit)
96 {
97 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
98 struct nouveau_channel *chan = context_chan(ctx);
99 struct nouveau_grobj *kelvin = context_eng3d(ctx);
100 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
101 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
102 struct gl_texture_object *t;
103 struct nouveau_surface *s;
104 struct gl_texture_image *ti;
105 uint32_t tx_format, tx_filter, tx_wrap, tx_enable;
106
107 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
108 BEGIN_RING(chan, kelvin, NV20TCL_TX_ENABLE(i), 1);
109 OUT_RING(chan, 0);
110
111 context_dirty(ctx, TEX_SHADER);
112 return;
113 }
114
115 t = ctx->Texture.Unit[i]._Current;
116 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
117 ti = t->Image[0][t->BaseLevel];
118
119 if (!nouveau_texture_validate(ctx, t))
120 return;
121
122 /* Recompute the texturing registers. */
123 tx_format = ti->DepthLog2 << 28
124 | ti->HeightLog2 << 24
125 | ti->WidthLog2 << 20
126 | NV20TCL_TX_FORMAT_DIMS_2D
127 | NV20TCL_TX_FORMAT_NO_BORDER
128 | 1 << 16;
129
130 tx_wrap = nvgl_wrap_mode(t->WrapR) << 16
131 | nvgl_wrap_mode(t->WrapT) << 8
132 | nvgl_wrap_mode(t->WrapS) << 0;
133
134 tx_filter = nvgl_filter_mode(t->MagFilter) << 24
135 | nvgl_filter_mode(t->MinFilter) << 16;
136
137 tx_enable = NV20TCL_TX_ENABLE_ENABLE
138 | log2i(t->MaxAnisotropy) << 4;
139
140 if (t->Target == GL_TEXTURE_RECTANGLE) {
141 BEGIN_RING(chan, kelvin, NV20TCL_TX_NPOT_PITCH(i), 1);
142 OUT_RING(chan, s->pitch << 16);
143 BEGIN_RING(chan, kelvin, NV20TCL_TX_NPOT_SIZE(i), 1);
144 OUT_RING(chan, s->width << 16 | s->height);
145
146 tx_format |= get_tex_format_rect(ti);
147 } else {
148 tx_format |= get_tex_format_pot(ti);
149 }
150
151 if (t->MinFilter != GL_NEAREST &&
152 t->MinFilter != GL_LINEAR) {
153 int lod_min = t->MinLod;
154 int lod_max = MIN2(t->MaxLod, t->_MaxLambda);
155 int lod_bias = t->LodBias
156 + ctx->Texture.Unit[i].LodBias;
157
158 lod_max = CLAMP(lod_max, 0, 15);
159 lod_min = CLAMP(lod_min, 0, 15);
160 lod_bias = CLAMP(lod_bias, 0, 15);
161
162 tx_format |= NV20TCL_TX_FORMAT_MIPMAP;
163 tx_filter |= lod_bias << 8;
164 tx_enable |= lod_min << 26
165 | lod_max << 14;
166 }
167
168 /* Write it to the hardware. */
169 nouveau_bo_mark(bctx, kelvin, NV20TCL_TX_FORMAT(i),
170 s->bo, tx_format, 0,
171 NV20TCL_TX_FORMAT_DMA0,
172 NV20TCL_TX_FORMAT_DMA1,
173 bo_flags | NOUVEAU_BO_OR);
174
175 nouveau_bo_markl(bctx, kelvin, NV20TCL_TX_OFFSET(i),
176 s->bo, 0, bo_flags);
177
178 BEGIN_RING(chan, kelvin, NV20TCL_TX_WRAP(i), 1);
179 OUT_RING(chan, tx_wrap);
180
181 BEGIN_RING(chan, kelvin, NV20TCL_TX_FILTER(i), 1);
182 OUT_RING(chan, tx_filter);
183
184 BEGIN_RING(chan, kelvin, NV20TCL_TX_ENABLE(i), 1);
185 OUT_RING(chan, tx_enable);
186
187 context_dirty(ctx, TEX_SHADER);
188 }
189
190 void
191 nv20_emit_tex_shader(GLcontext *ctx, int emit)
192 {
193 struct nouveau_channel *chan = context_chan(ctx);
194 struct nouveau_grobj *kelvin = context_eng3d(ctx);
195 uint32_t tx_shader_op = 0;
196 int i;
197
198 for (i = 0; i < NV20_TEXTURE_UNITS; i++) {
199 if (!ctx->Texture.Unit[i]._ReallyEnabled)
200 continue;
201
202 tx_shader_op |= NV20TCL_TX_SHADER_OP_TX0_TEXTURE_2D << 5 * i;
203 }
204
205 BEGIN_RING(chan, kelvin, NV20TCL_TX_SHADER_OP, 1);
206 OUT_RING(chan, tx_shader_op);
207 }