2 * Copyright (C) 2009 Francisco Jerez.
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:
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.
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.
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"
36 get_tex_format_pot(struct gl_texture_image
*ti
)
38 switch (ti
->TexFormat
) {
39 case MESA_FORMAT_ARGB8888
:
40 return NV20TCL_TX_FORMAT_FORMAT_A8R8G8B8
;
42 case MESA_FORMAT_ARGB1555
:
43 return NV20TCL_TX_FORMAT_FORMAT_A1R5G5B5
;
45 case MESA_FORMAT_ARGB4444
:
46 return NV20TCL_TX_FORMAT_FORMAT_A4R4G4B4
;
48 case MESA_FORMAT_RGB565
:
49 return NV20TCL_TX_FORMAT_FORMAT_R5G6B5
;
53 return NV20TCL_TX_FORMAT_FORMAT_A8
;
56 return NV20TCL_TX_FORMAT_FORMAT_L8
;
59 return NV20TCL_TX_FORMAT_FORMAT_INDEX8
;
67 get_tex_format_rect(struct gl_texture_image
*ti
)
69 switch (ti
->TexFormat
) {
70 case MESA_FORMAT_ARGB1555
:
71 return NV20TCL_TX_FORMAT_FORMAT_A1R5G5B5_RECT
;
73 case MESA_FORMAT_RGB565
:
74 return NV20TCL_TX_FORMAT_FORMAT_R5G6B5_RECT
;
76 case MESA_FORMAT_ARGB8888
:
77 return NV20TCL_TX_FORMAT_FORMAT_A8R8G8B8_RECT
;
80 return NV20TCL_TX_FORMAT_FORMAT_L8_RECT
;
84 return NV20TCL_TX_FORMAT_FORMAT_A8_RECT
;
86 case MESA_FORMAT_ARGB4444
:
87 return NV20TCL_TX_FORMAT_FORMAT_A4R4G4B4_RECT
;
95 nv20_emit_tex_obj(GLcontext
*ctx
, int emit
)
97 const int i
= emit
- NOUVEAU_STATE_TEX_OBJ0
;
98 struct nouveau_channel
*chan
= context_chan(ctx
);
99 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
100 struct nouveau_bo_context
*bctx
= context_bctx_i(ctx
, TEXTURE
, i
);
101 const int bo_flags
= NOUVEAU_BO_RD
| NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
;
102 struct gl_texture_object
*t
;
103 struct nouveau_surface
*s
;
104 struct gl_texture_image
*ti
;
105 uint32_t tx_format
, tx_filter
, tx_wrap
, tx_enable
;
107 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
108 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_ENABLE(i
), 1);
111 context_dirty(ctx
, TEX_SHADER
);
115 t
= ctx
->Texture
.Unit
[i
]._Current
;
116 s
= &to_nouveau_texture(t
)->surfaces
[t
->BaseLevel
];
117 ti
= t
->Image
[0][t
->BaseLevel
];
119 if (!nouveau_texture_validate(ctx
, t
))
122 /* Recompute the texturing registers. */
123 tx_format
= ti
->DepthLog2
<< 28
124 | ti
->HeightLog2
<< 24
125 | ti
->WidthLog2
<< 20
126 | NV20TCL_TX_FORMAT_DIMS_2D
127 | NV20TCL_TX_FORMAT_NO_BORDER
130 tx_wrap
= nvgl_wrap_mode(t
->WrapR
) << 16
131 | nvgl_wrap_mode(t
->WrapT
) << 8
132 | nvgl_wrap_mode(t
->WrapS
) << 0;
134 tx_filter
= nvgl_filter_mode(t
->MagFilter
) << 24
135 | nvgl_filter_mode(t
->MinFilter
) << 16;
137 tx_enable
= NV20TCL_TX_ENABLE_ENABLE
138 | log2i(t
->MaxAnisotropy
) << 4;
140 if (t
->Target
== GL_TEXTURE_RECTANGLE
) {
141 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_NPOT_PITCH(i
), 1);
142 OUT_RING(chan
, s
->pitch
<< 16);
143 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_NPOT_SIZE(i
), 1);
144 OUT_RING(chan
, s
->width
<< 16 | s
->height
);
146 tx_format
|= get_tex_format_rect(ti
);
148 tx_format
|= get_tex_format_pot(ti
);
151 if (t
->MinFilter
!= GL_NEAREST
&&
152 t
->MinFilter
!= GL_LINEAR
) {
153 int lod_min
= t
->MinLod
;
154 int lod_max
= MIN2(t
->MaxLod
, t
->_MaxLambda
);
155 int lod_bias
= t
->LodBias
156 + ctx
->Texture
.Unit
[i
].LodBias
;
158 lod_max
= CLAMP(lod_max
, 0, 15);
159 lod_min
= CLAMP(lod_min
, 0, 15);
160 lod_bias
= CLAMP(lod_bias
, 0, 15);
162 tx_format
|= NV20TCL_TX_FORMAT_MIPMAP
;
163 tx_filter
|= lod_bias
<< 8;
164 tx_enable
|= lod_min
<< 26
168 /* Write it to the hardware. */
169 nouveau_bo_mark(bctx
, kelvin
, NV20TCL_TX_FORMAT(i
),
171 NV20TCL_TX_FORMAT_DMA0
,
172 NV20TCL_TX_FORMAT_DMA1
,
173 bo_flags
| NOUVEAU_BO_OR
);
175 nouveau_bo_markl(bctx
, kelvin
, NV20TCL_TX_OFFSET(i
),
178 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_WRAP(i
), 1);
179 OUT_RING(chan
, tx_wrap
);
181 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_FILTER(i
), 1);
182 OUT_RING(chan
, tx_filter
);
184 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_ENABLE(i
), 1);
185 OUT_RING(chan
, tx_enable
);
187 context_dirty(ctx
, TEX_SHADER
);
191 nv20_emit_tex_shader(GLcontext
*ctx
, int emit
)
193 struct nouveau_channel
*chan
= context_chan(ctx
);
194 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
195 uint32_t tx_shader_op
= 0;
198 for (i
= 0; i
< NV20_TEXTURE_UNITS
; i
++) {
199 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
)
202 tx_shader_op
|= NV20TCL_TX_SHADER_OP_TX0_TEXTURE_2D
<< 5 * i
;
205 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_SHADER_OP
, 1);
206 OUT_RING(chan
, tx_shader_op
);