d01e91f8ee19da1a97c450c31e9fa9237d3e279c
[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 nouveau_texture_validate(ctx, t);
91
92 /* Recompute the texturing registers. */
93 tx_format = ti->DepthLog2 << 28
94 | ti->HeightLog2 << 24
95 | ti->WidthLog2 << 20
96 | get_tex_format(ti)
97 | NV20TCL_TX_FORMAT_DIMS_2D
98 | NV20TCL_TX_FORMAT_NO_BORDER
99 | 1 << 16;
100
101 tx_wrap = nvgl_wrap_mode(t->WrapR) << 16
102 | nvgl_wrap_mode(t->WrapT) << 8
103 | nvgl_wrap_mode(t->WrapS) << 0;
104
105 tx_filter = nvgl_filter_mode(t->MagFilter) << 24
106 | nvgl_filter_mode(t->MinFilter) << 16;
107
108 tx_enable = NV20TCL_TX_ENABLE_ENABLE
109 | log2i(t->MaxAnisotropy) << 4;
110
111 if (t->MinFilter != GL_NEAREST &&
112 t->MinFilter != GL_LINEAR) {
113 int lod_min = t->MinLod;
114 int lod_max = MIN2(t->MaxLod, t->_MaxLambda);
115 int lod_bias = t->LodBias
116 + ctx->Texture.Unit[i].LodBias;
117
118 lod_max = CLAMP(lod_max, 0, 15);
119 lod_min = CLAMP(lod_min, 0, 15);
120 lod_bias = CLAMP(lod_bias, 0, 15);
121
122 tx_format |= NV20TCL_TX_FORMAT_MIPMAP;
123 tx_filter |= lod_bias << 8;
124 tx_enable |= lod_min << 26
125 | lod_max << 14;
126 }
127
128 /* Write it to the hardware. */
129 nouveau_bo_mark(bctx, kelvin, NV20TCL_TX_FORMAT(i),
130 s->bo, tx_format, 0,
131 NV20TCL_TX_FORMAT_DMA0,
132 NV20TCL_TX_FORMAT_DMA1,
133 bo_flags | NOUVEAU_BO_OR);
134
135 nouveau_bo_markl(bctx, kelvin, NV20TCL_TX_OFFSET(i),
136 s->bo, 0, bo_flags);
137
138 BEGIN_RING(chan, kelvin, NV20TCL_TX_WRAP(i), 1);
139 OUT_RING(chan, tx_wrap);
140
141 BEGIN_RING(chan, kelvin, NV20TCL_TX_FILTER(i), 1);
142 OUT_RING(chan, tx_filter);
143
144 BEGIN_RING(chan, kelvin, NV20TCL_TX_ENABLE(i), 1);
145 OUT_RING(chan, tx_enable);
146
147 context_dirty(ctx, TEX_SHADER);
148 }
149
150 void
151 nv20_emit_tex_shader(GLcontext *ctx, int emit)
152 {
153 struct nouveau_channel *chan = context_chan(ctx);
154 struct nouveau_grobj *kelvin = context_eng3d(ctx);
155 uint32_t tx_shader_op = 0;
156 int i;
157
158 for (i = 0; i < NV20_TEXTURE_UNITS; i++) {
159 if (!ctx->Texture.Unit[i]._ReallyEnabled)
160 continue;
161
162 tx_shader_op |= NV20TCL_TX_SHADER_OP_TX0_TEXTURE_2D << 5 * i;
163 }
164
165 BEGIN_RING(chan, kelvin, NV20TCL_TX_SHADER_OP, 1);
166 OUT_RING(chan, tx_shader_op);
167 }