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