Merge branch 'gallium-vertexelementcso'
[mesa.git] / src / mesa / drivers / dri / nouveau / nv04_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_texture.h"
30 #include "nouveau_util.h"
31 #include "nouveau_gldefs.h"
32 #include "nouveau_class.h"
33 #include "nv04_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_A8:
40 case MESA_FORMAT_L8:
41 case MESA_FORMAT_I8:
42 return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_Y8;
43 case MESA_FORMAT_ARGB1555:
44 return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A1R5G5B5;
45 case MESA_FORMAT_ARGB4444:
46 return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A4R4G4B4;
47 case MESA_FORMAT_RGB565:
48 return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_R5G6B5;
49 case MESA_FORMAT_ARGB8888:
50 return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A8R8G8B8;
51 case MESA_FORMAT_XRGB8888:
52 return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_X8R8G8B8;
53 default:
54 assert(0);
55 }
56 }
57
58 static inline unsigned
59 get_wrap_mode(unsigned wrap)
60 {
61 switch (wrap) {
62 case GL_REPEAT:
63 return 0x1;
64 case GL_MIRRORED_REPEAT:
65 return 0x2;
66 case GL_CLAMP:
67 case GL_CLAMP_TO_EDGE:
68 return 0x3;
69 case GL_CLAMP_TO_BORDER:
70 return 0x4;
71 default:
72 assert(0);
73 }
74 }
75
76 void
77 nv04_emit_tex_obj(GLcontext *ctx, int emit)
78 {
79 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
80 struct nouveau_channel *chan = context_chan(ctx);
81 struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
82 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
83 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
84 struct nouveau_surface *s;
85 uint32_t format = 0xa0, filter = 0x1010;
86
87 if (i && !nv04_mtex_engine(fahrenheit))
88 return;
89
90 if (ctx->Texture.Unit[i]._ReallyEnabled) {
91 struct gl_texture_object *t = ctx->Texture.Unit[i]._Current;
92 struct gl_texture_image *ti = t->Image[0][t->BaseLevel];
93 int lod_max = 1, lod_bias = 0;
94
95 if (!nouveau_texture_validate(ctx, t))
96 return;
97
98 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
99
100 if (t->MinFilter != GL_NEAREST &&
101 t->MinFilter != GL_LINEAR) {
102 lod_max = CLAMP(MIN2(t->MaxLod, t->_MaxLambda),
103 0, 15) + 1;
104
105 lod_bias = CLAMP(ctx->Texture.Unit[i].LodBias +
106 t->LodBias, 0, 15);
107 }
108
109 format |= get_wrap_mode(t->WrapT) << 28 |
110 get_wrap_mode(t->WrapS) << 24 |
111 ti->HeightLog2 << 20 |
112 ti->WidthLog2 << 16 |
113 lod_max << 12 |
114 get_tex_format(ti);
115
116 filter |= log2i(t->MaxAnisotropy) << 31 |
117 nvgl_filter_mode(t->MagFilter) << 28 |
118 log2i(t->MaxAnisotropy) << 27 |
119 nvgl_filter_mode(t->MinFilter) << 24 |
120 lod_bias << 16;
121
122 } else {
123 s = &to_nv04_context(ctx)->dummy_texture;
124
125 format |= NV04_TEXTURED_TRIANGLE_FORMAT_ADDRESSU_REPEAT |
126 NV04_TEXTURED_TRIANGLE_FORMAT_ADDRESSV_REPEAT |
127 1 << 12 |
128 NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A8R8G8B8;
129
130 filter |= NV04_TEXTURED_TRIANGLE_FILTER_MINIFY_NEAREST |
131 NV04_TEXTURED_TRIANGLE_FILTER_MAGNIFY_NEAREST;
132 }
133
134 if (nv04_mtex_engine(fahrenheit)) {
135 nouveau_bo_markl(bctx, fahrenheit,
136 NV04_MULTITEX_TRIANGLE_OFFSET(i),
137 s->bo, 0, bo_flags);
138
139 nouveau_bo_mark(bctx, fahrenheit,
140 NV04_MULTITEX_TRIANGLE_FORMAT(i),
141 s->bo, format, 0,
142 NV04_MULTITEX_TRIANGLE_FORMAT_DMA_A,
143 NV04_MULTITEX_TRIANGLE_FORMAT_DMA_B,
144 bo_flags | NOUVEAU_BO_OR);
145
146 BEGIN_RING(chan, fahrenheit, NV04_MULTITEX_TRIANGLE_FILTER(i), 1);
147 OUT_RING(chan, filter);
148
149 } else {
150 nouveau_bo_markl(bctx, fahrenheit,
151 NV04_TEXTURED_TRIANGLE_OFFSET,
152 s->bo, 0, bo_flags);
153
154 nouveau_bo_mark(bctx, fahrenheit,
155 NV04_TEXTURED_TRIANGLE_FORMAT,
156 s->bo, format, 0,
157 NV04_TEXTURED_TRIANGLE_FORMAT_DMA_A,
158 NV04_TEXTURED_TRIANGLE_FORMAT_DMA_B,
159 bo_flags | NOUVEAU_BO_OR);
160
161 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_COLORKEY, 1);
162 OUT_RING(chan, 0);
163
164 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_FILTER, 1);
165 OUT_RING(chan, filter);
166 }
167 }