02a5ca797ae0cce44f6c485871fe37269af7e88f
[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 void
36 nv10_emit_tex_gen(GLcontext *ctx, int emit)
37 {
38 }
39
40 static uint32_t
41 get_tex_format_pot(struct gl_texture_image *ti)
42 {
43 switch (ti->TexFormat) {
44 case MESA_FORMAT_ARGB8888:
45 return NV10TCL_TX_FORMAT_FORMAT_A8R8G8B8;
46
47 case MESA_FORMAT_XRGB8888:
48 return NV10TCL_TX_FORMAT_FORMAT_X8R8G8B8;
49
50 case MESA_FORMAT_ARGB1555:
51 return NV10TCL_TX_FORMAT_FORMAT_A1R5G5B5;
52
53 case MESA_FORMAT_ARGB4444:
54 return NV10TCL_TX_FORMAT_FORMAT_A4R4G4B4;
55
56 case MESA_FORMAT_RGB565:
57 return NV10TCL_TX_FORMAT_FORMAT_R5G6B5;
58
59 case MESA_FORMAT_A8:
60 case MESA_FORMAT_I8:
61 return NV10TCL_TX_FORMAT_FORMAT_A8;
62
63 case MESA_FORMAT_L8:
64 return NV10TCL_TX_FORMAT_FORMAT_L8;
65
66 case MESA_FORMAT_CI8:
67 return NV10TCL_TX_FORMAT_FORMAT_INDEX8;
68
69 default:
70 assert(0);
71 }
72 }
73
74 static uint32_t
75 get_tex_format_rect(struct gl_texture_image *ti)
76 {
77 switch (ti->TexFormat) {
78 case MESA_FORMAT_ARGB1555:
79 return NV10TCL_TX_FORMAT_FORMAT_A1R5G5B5_RECT;
80
81 case MESA_FORMAT_RGB565:
82 return NV10TCL_TX_FORMAT_FORMAT_R5G6B5_RECT;
83
84 case MESA_FORMAT_ARGB8888:
85 case MESA_FORMAT_XRGB8888:
86 return NV10TCL_TX_FORMAT_FORMAT_A8R8G8B8_RECT;
87
88 case MESA_FORMAT_A8:
89 case MESA_FORMAT_L8:
90 case MESA_FORMAT_I8:
91 return NV10TCL_TX_FORMAT_FORMAT_A8_RECT;
92
93 default:
94 assert(0);
95 }
96 }
97
98 void
99 nv10_emit_tex_obj(GLcontext *ctx, int emit)
100 {
101 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
102 struct nouveau_channel *chan = context_chan(ctx);
103 struct nouveau_grobj *celsius = context_eng3d(ctx);
104 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
105 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
106 struct gl_texture_object *t;
107 struct nouveau_surface *s;
108 struct gl_texture_image *ti;
109 uint32_t tx_format, tx_filter, tx_enable;
110
111 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
112 BEGIN_RING(chan, celsius, NV10TCL_TX_ENABLE(i), 1);
113 OUT_RING(chan, 0);
114 return;
115 }
116
117 t = ctx->Texture.Unit[i]._Current;
118 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
119 ti = t->Image[0][t->BaseLevel];
120
121 if (!nouveau_texture_validate(ctx, t))
122 return;
123
124 /* Recompute the texturing registers. */
125 tx_format = nvgl_wrap_mode(t->WrapT) << 28
126 | nvgl_wrap_mode(t->WrapS) << 24
127 | ti->HeightLog2 << 20
128 | ti->WidthLog2 << 16
129 | 5 << 4 | 1 << 12;
130
131 tx_filter = nvgl_filter_mode(t->MagFilter) << 28
132 | nvgl_filter_mode(t->MinFilter) << 24;
133
134 tx_enable = NV10TCL_TX_ENABLE_ENABLE
135 | log2i(t->MaxAnisotropy) << 4;
136
137 if (t->Target == GL_TEXTURE_RECTANGLE) {
138 BEGIN_RING(chan, celsius, NV10TCL_TX_NPOT_PITCH(i), 1);
139 OUT_RING(chan, s->pitch << 16);
140 BEGIN_RING(chan, celsius, NV10TCL_TX_NPOT_SIZE(i), 1);
141 OUT_RING(chan, align(s->width, 2) << 16 | s->height);
142
143 tx_format |= get_tex_format_rect(ti);
144 } else {
145 tx_format |= get_tex_format_pot(ti);
146 }
147
148 if (t->MinFilter != GL_NEAREST &&
149 t->MinFilter != GL_LINEAR) {
150 int lod_min = t->MinLod;
151 int lod_max = MIN2(t->MaxLod, t->_MaxLambda);
152 int lod_bias = t->LodBias
153 + ctx->Texture.Unit[i].LodBias;
154
155 lod_max = CLAMP(lod_max, 0, 15);
156 lod_min = CLAMP(lod_min, 0, 15);
157 lod_bias = CLAMP(lod_bias, 0, 15);
158
159 tx_format |= NV10TCL_TX_FORMAT_MIPMAP;
160 tx_filter |= lod_bias << 8;
161 tx_enable |= lod_min << 26
162 | lod_max << 14;
163 }
164
165 /* Write it to the hardware. */
166 nouveau_bo_mark(bctx, celsius, NV10TCL_TX_FORMAT(i),
167 s->bo, tx_format, 0,
168 NV10TCL_TX_FORMAT_DMA0,
169 NV10TCL_TX_FORMAT_DMA1,
170 bo_flags | NOUVEAU_BO_OR);
171
172 nouveau_bo_markl(bctx, celsius, NV10TCL_TX_OFFSET(i),
173 s->bo, 0, bo_flags);
174
175 BEGIN_RING(chan, celsius, NV10TCL_TX_FILTER(i), 1);
176 OUT_RING(chan, tx_filter);
177
178 BEGIN_RING(chan, celsius, NV10TCL_TX_ENABLE(i), 1);
179 OUT_RING(chan, tx_enable);
180 }
181