Drop GLcontext typedef and use struct gl_context instead
[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 void
59 nv04_emit_tex_obj(struct gl_context *ctx, int emit)
60 {
61 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
62 struct nouveau_channel *chan = context_chan(ctx);
63 struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
64 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
65 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
66 struct nouveau_surface *s;
67 uint32_t format = 0xa0, filter = 0x1010;
68
69 if (i && !nv04_mtex_engine(fahrenheit))
70 return;
71
72 if (ctx->Texture.Unit[i]._ReallyEnabled) {
73 struct gl_texture_object *t = ctx->Texture.Unit[i]._Current;
74 struct gl_texture_image *ti = t->Image[0][t->BaseLevel];
75 int lod_max = 1, lod_bias = 0;
76
77 if (!nouveau_texture_validate(ctx, t))
78 return;
79
80 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
81
82 if (t->MinFilter != GL_NEAREST &&
83 t->MinFilter != GL_LINEAR) {
84 lod_max = CLAMP(MIN2(t->MaxLod, t->_MaxLambda),
85 0, 15) + 1;
86
87 lod_bias = CLAMP(ctx->Texture.Unit[i].LodBias +
88 t->LodBias, -16, 15) * 8;
89 }
90
91 format |= nvgl_wrap_mode(t->WrapT) << 28 |
92 nvgl_wrap_mode(t->WrapS) << 24 |
93 ti->HeightLog2 << 20 |
94 ti->WidthLog2 << 16 |
95 lod_max << 12 |
96 get_tex_format(ti);
97
98 filter |= log2i(t->MaxAnisotropy) << 31 |
99 nvgl_filter_mode(t->MagFilter) << 28 |
100 log2i(t->MaxAnisotropy) << 27 |
101 nvgl_filter_mode(t->MinFilter) << 24 |
102 (lod_bias & 0xff) << 16;
103
104 } else {
105 s = &to_nv04_context(ctx)->dummy_texture;
106
107 format |= NV04_TEXTURED_TRIANGLE_FORMAT_ADDRESSU_REPEAT |
108 NV04_TEXTURED_TRIANGLE_FORMAT_ADDRESSV_REPEAT |
109 1 << 12 |
110 NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A8R8G8B8;
111
112 filter |= NV04_TEXTURED_TRIANGLE_FILTER_MINIFY_NEAREST |
113 NV04_TEXTURED_TRIANGLE_FILTER_MAGNIFY_NEAREST;
114 }
115
116 if (nv04_mtex_engine(fahrenheit)) {
117 nouveau_bo_markl(bctx, fahrenheit,
118 NV04_MULTITEX_TRIANGLE_OFFSET(i),
119 s->bo, s->offset, bo_flags);
120
121 nouveau_bo_mark(bctx, fahrenheit,
122 NV04_MULTITEX_TRIANGLE_FORMAT(i),
123 s->bo, format, 0,
124 NV04_MULTITEX_TRIANGLE_FORMAT_DMA_A,
125 NV04_MULTITEX_TRIANGLE_FORMAT_DMA_B,
126 bo_flags | NOUVEAU_BO_OR);
127
128 BEGIN_RING(chan, fahrenheit, NV04_MULTITEX_TRIANGLE_FILTER(i), 1);
129 OUT_RING(chan, filter);
130
131 } else {
132 nouveau_bo_markl(bctx, fahrenheit,
133 NV04_TEXTURED_TRIANGLE_OFFSET,
134 s->bo, s->offset, bo_flags);
135
136 nouveau_bo_mark(bctx, fahrenheit,
137 NV04_TEXTURED_TRIANGLE_FORMAT,
138 s->bo, format, 0,
139 NV04_TEXTURED_TRIANGLE_FORMAT_DMA_A,
140 NV04_TEXTURED_TRIANGLE_FORMAT_DMA_B,
141 bo_flags | NOUVEAU_BO_OR);
142
143 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_COLORKEY, 1);
144 OUT_RING(chan, 0);
145
146 BEGIN_RING(chan, fahrenheit, NV04_TEXTURED_TRIANGLE_FILTER, 1);
147 OUT_RING(chan, filter);
148 }
149 }