dri/nouveau: Try to validate textures earlier.
[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(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_ARGB1555:
48 return NV10TCL_TX_FORMAT_FORMAT_A1R5G5B5;
49
50 case MESA_FORMAT_ARGB4444:
51 return NV10TCL_TX_FORMAT_FORMAT_A4R4G4B4;
52
53 case MESA_FORMAT_RGB565:
54 return NV10TCL_TX_FORMAT_FORMAT_R5G6B5;
55
56 case MESA_FORMAT_A8:
57 return NV10TCL_TX_FORMAT_FORMAT_A8;
58
59 case MESA_FORMAT_L8:
60 return NV10TCL_TX_FORMAT_FORMAT_L8;
61
62 case MESA_FORMAT_CI8:
63 return NV10TCL_TX_FORMAT_FORMAT_INDEX8;
64
65 default:
66 assert(0);
67 }
68 }
69
70 void
71 nv10_emit_tex_obj(GLcontext *ctx, int emit)
72 {
73 const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
74 struct nouveau_channel *chan = context_chan(ctx);
75 struct nouveau_grobj *celsius = context_eng3d(ctx);
76 struct nouveau_bo_context *bctx = context_bctx_i(ctx, TEXTURE, i);
77 const int bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM;
78 struct gl_texture_object *t;
79 struct nouveau_surface *s;
80 struct gl_texture_image *ti;
81 uint32_t tx_format, tx_filter, tx_enable;
82
83 if (!ctx->Texture.Unit[i]._ReallyEnabled) {
84 BEGIN_RING(chan, celsius, NV10TCL_TX_ENABLE(i), 1);
85 OUT_RING(chan, 0);
86 return;
87 }
88
89 t = ctx->Texture.Unit[i]._Current;
90 s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
91 ti = t->Image[0][t->BaseLevel];
92
93 if (!nouveau_texture_validate(ctx, t))
94 return;
95
96 /* Recompute the texturing registers. */
97 tx_format = nvgl_wrap_mode(t->WrapT) << 28
98 | nvgl_wrap_mode(t->WrapS) << 24
99 | ti->HeightLog2 << 20
100 | ti->WidthLog2 << 16
101 | get_tex_format(ti)
102 | 5 << 4 | 1 << 12;
103
104 tx_filter = nvgl_filter_mode(t->MagFilter) << 28
105 | nvgl_filter_mode(t->MinFilter) << 24;
106
107 tx_enable = NV10TCL_TX_ENABLE_ENABLE
108 | log2i(t->MaxAnisotropy) << 4;
109
110 if (t->MinFilter != GL_NEAREST &&
111 t->MinFilter != GL_LINEAR) {
112 int lod_min = t->MinLod;
113 int lod_max = MIN2(t->MaxLod, t->_MaxLambda);
114 int lod_bias = t->LodBias
115 + ctx->Texture.Unit[i].LodBias;
116
117 lod_max = CLAMP(lod_max, 0, 15);
118 lod_min = CLAMP(lod_min, 0, 15);
119 lod_bias = CLAMP(lod_bias, 0, 15);
120
121 tx_format |= NV10TCL_TX_FORMAT_MIPMAP;
122 tx_filter |= lod_bias << 8;
123 tx_enable |= lod_min << 26
124 | lod_max << 14;
125 }
126
127 /* Write it to the hardware. */
128 nouveau_bo_mark(bctx, celsius, NV10TCL_TX_FORMAT(i),
129 s->bo, tx_format, 0,
130 NV10TCL_TX_FORMAT_DMA0,
131 NV10TCL_TX_FORMAT_DMA1,
132 bo_flags | NOUVEAU_BO_OR);
133
134 nouveau_bo_markl(bctx, celsius, NV10TCL_TX_OFFSET(i),
135 s->bo, 0, bo_flags);
136
137 BEGIN_RING(chan, celsius, NV10TCL_TX_FILTER(i), 1);
138 OUT_RING(chan, tx_filter);
139
140 BEGIN_RING(chan, celsius, NV10TCL_TX_ENABLE(i), 1);
141 OUT_RING(chan, tx_enable);
142 }
143