2bf760d3b08bf0fab5acd50823896e7e3fa7b7b8
[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(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 return NV20TCL_TX_FORMAT_FORMAT_A8;
53
54 case MESA_FORMAT_L8:
55 return NV20TCL_TX_FORMAT_FORMAT_L8;
56
57 case MESA_FORMAT_CI8:
58 return NV20TCL_TX_FORMAT_FORMAT_INDEX8;
59
60 default:
61 assert(0);
62 }
63 }
64
65 void
66 nv20_emit_tex_obj(GLcontext *ctx, int emit)
67 {
68 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
69 struct nouveau_channel *chan = context_chan(ctx);
70 struct nouveau_grobj *kelvin = context_eng3d(ctx);
71 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
72 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
73 struct gl_texture_object *t;
74 struct nouveau_surface *s;
75 struct gl_texture_image *ti;
76 uint32_t tx_format, tx_filter, tx_wrap, tx_enable;
77
78 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
79 BEGIN_RING(chan, kelvin, NV20TCL_TX_ENABLE(i), 1);
80 OUT_RING(chan, 0);
81
82 context_dirty(ctx, TEX_SHADER);
83 return;
84 }
85
86 t = ctx->Texture.Unit[i]._Current;
87 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
88 ti = t->Image[0][t->BaseLevel];
89
90 if (!nouveau_texture_validate(ctx, t))
91 return;
92
93 /* Recompute the texturing registers. */
94 tx_format = ti->DepthLog2 << 28
95 | ti->HeightLog2 << 24
96 | ti->WidthLog2 << 20
97 | get_tex_format(ti)
98 | NV20TCL_TX_FORMAT_DIMS_2D
99 | NV20TCL_TX_FORMAT_NO_BORDER
100 | 1 << 16;
101
102 tx_wrap = nvgl_wrap_mode(t->WrapR) << 16
103 | nvgl_wrap_mode(t->WrapT) << 8
104 | nvgl_wrap_mode(t->WrapS) << 0;
105
106 tx_filter = nvgl_filter_mode(t->MagFilter) << 24
107 | nvgl_filter_mode(t->MinFilter) << 16;
108
109 tx_enable = NV20TCL_TX_ENABLE_ENABLE
110 | log2i(t->MaxAnisotropy) << 4;
111
112 if (t->MinFilter != GL_NEAREST &&
113 t->MinFilter != GL_LINEAR) {
114 int lod_min = t->MinLod;
115 int lod_max = MIN2(t->MaxLod, t->_MaxLambda);
116 int lod_bias = t->LodBias
117 + ctx->Texture.Unit[i].LodBias;
118
119 lod_max = CLAMP(lod_max, 0, 15);
120 lod_min = CLAMP(lod_min, 0, 15);
121 lod_bias = CLAMP(lod_bias, 0, 15);
122
123 tx_format |= NV20TCL_TX_FORMAT_MIPMAP;
124 tx_filter |= lod_bias << 8;
125 tx_enable |= lod_min << 26
126 | lod_max << 14;
127 }
128
129 /* Write it to the hardware. */
130 nouveau_bo_mark(bctx, kelvin, NV20TCL_TX_FORMAT(i),
131 s->bo, tx_format, 0,
132 NV20TCL_TX_FORMAT_DMA0,
133 NV20TCL_TX_FORMAT_DMA1,
134 bo_flags | NOUVEAU_BO_OR);
135
136 nouveau_bo_markl(bctx, kelvin, NV20TCL_TX_OFFSET(i),
137 s->bo, 0, bo_flags);
138
139 BEGIN_RING(chan, kelvin, NV20TCL_TX_WRAP(i), 1);
140 OUT_RING(chan, tx_wrap);
141
142 BEGIN_RING(chan, kelvin, NV20TCL_TX_FILTER(i), 1);
143 OUT_RING(chan, tx_filter);
144
145 BEGIN_RING(chan, kelvin, NV20TCL_TX_ENABLE(i), 1);
146 OUT_RING(chan, tx_enable);
147
148 context_dirty(ctx, TEX_SHADER);
149 }
150
151 void
152 nv20_emit_tex_shader(GLcontext *ctx, int emit)
153 {
154 struct nouveau_channel *chan = context_chan(ctx);
155 struct nouveau_grobj *kelvin = context_eng3d(ctx);
156 uint32_t tx_shader_op = 0;
157 int i;
158
159 for (i = 0; i < NV20_TEXTURE_UNITS; i++) {
160 if (!ctx->Texture.Unit[i]._ReallyEnabled)
161 continue;
162
163 tx_shader_op |= NV20TCL_TX_SHADER_OP_TX0_TEXTURE_2D << 5 * i;
164 }
165
166 BEGIN_RING(chan, kelvin, NV20TCL_TX_SHADER_OP, 1);
167 OUT_RING(chan, tx_shader_op);
168 }