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_XRGB8888
:
49 return NV20TCL_TX_FORMAT_FORMAT_X8R8G8B8
;
51 case MESA_FORMAT_RGB565
:
52 return NV20TCL_TX_FORMAT_FORMAT_R5G6B5
;
56 return NV20TCL_TX_FORMAT_FORMAT_A8
;
59 return NV20TCL_TX_FORMAT_FORMAT_L8
;
62 return NV20TCL_TX_FORMAT_FORMAT_INDEX8
;
70 get_tex_format_rect(struct gl_texture_image
*ti
)
72 switch (ti
->TexFormat
) {
73 case MESA_FORMAT_ARGB8888
:
74 return NV20TCL_TX_FORMAT_FORMAT_A8R8G8B8_RECT
;
76 case MESA_FORMAT_ARGB1555
:
77 return NV20TCL_TX_FORMAT_FORMAT_A1R5G5B5_RECT
;
79 case MESA_FORMAT_ARGB4444
:
80 return NV20TCL_TX_FORMAT_FORMAT_A4R4G4B4_RECT
;
82 case MESA_FORMAT_XRGB8888
:
83 return NV20TCL_TX_FORMAT_FORMAT_R8G8B8_RECT
;
85 case MESA_FORMAT_RGB565
:
86 return NV20TCL_TX_FORMAT_FORMAT_R5G6B5_RECT
;
89 return NV20TCL_TX_FORMAT_FORMAT_L8_RECT
;
93 return NV20TCL_TX_FORMAT_FORMAT_A8_RECT
;
101 nv20_emit_tex_obj(GLcontext
*ctx
, int emit
)
103 const int i
= emit
- NOUVEAU_STATE_TEX_OBJ0
;
104 struct nouveau_channel
*chan
= context_chan(ctx
);
105 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
106 struct nouveau_bo_context
*bctx
= context_bctx_i(ctx
, TEXTURE
, i
);
107 const int bo_flags
= NOUVEAU_BO_RD
| NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
;
108 struct gl_texture_object
*t
;
109 struct nouveau_surface
*s
;
110 struct gl_texture_image
*ti
;
111 uint32_t tx_format
, tx_filter
, tx_wrap
, tx_enable
;
113 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
114 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_ENABLE(i
), 1);
117 context_dirty(ctx
, TEX_SHADER
);
121 t
= ctx
->Texture
.Unit
[i
]._Current
;
122 s
= &to_nouveau_texture(t
)->surfaces
[t
->BaseLevel
];
123 ti
= t
->Image
[0][t
->BaseLevel
];
125 if (!nouveau_texture_validate(ctx
, t
))
128 /* Recompute the texturing registers. */
129 tx_format
= ti
->DepthLog2
<< 28
130 | ti
->HeightLog2
<< 24
131 | ti
->WidthLog2
<< 20
132 | NV20TCL_TX_FORMAT_DIMS_2D
133 | NV20TCL_TX_FORMAT_NO_BORDER
136 tx_wrap
= nvgl_wrap_mode(t
->WrapR
) << 16
137 | nvgl_wrap_mode(t
->WrapT
) << 8
138 | nvgl_wrap_mode(t
->WrapS
) << 0;
140 tx_filter
= nvgl_filter_mode(t
->MagFilter
) << 24
141 | nvgl_filter_mode(t
->MinFilter
) << 16;
143 tx_enable
= NV20TCL_TX_ENABLE_ENABLE
144 | log2i(t
->MaxAnisotropy
) << 4;
146 if (t
->Target
== GL_TEXTURE_RECTANGLE
) {
147 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_NPOT_PITCH(i
), 1);
148 OUT_RING(chan
, s
->pitch
<< 16);
149 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_NPOT_SIZE(i
), 1);
150 OUT_RING(chan
, s
->width
<< 16 | s
->height
);
152 tx_format
|= get_tex_format_rect(ti
);
154 tx_format
|= get_tex_format_pot(ti
);
157 if (t
->MinFilter
!= GL_NEAREST
&&
158 t
->MinFilter
!= GL_LINEAR
) {
159 int lod_min
= t
->MinLod
;
160 int lod_max
= MIN2(t
->MaxLod
, t
->_MaxLambda
);
161 int lod_bias
= t
->LodBias
162 + ctx
->Texture
.Unit
[i
].LodBias
;
164 lod_max
= CLAMP(lod_max
, 0, 15);
165 lod_min
= CLAMP(lod_min
, 0, 15);
166 lod_bias
= CLAMP(lod_bias
, 0, 15);
168 tx_format
|= NV20TCL_TX_FORMAT_MIPMAP
;
169 tx_filter
|= lod_bias
<< 8;
170 tx_enable
|= lod_min
<< 26
174 /* Write it to the hardware. */
175 nouveau_bo_mark(bctx
, kelvin
, NV20TCL_TX_FORMAT(i
),
177 NV20TCL_TX_FORMAT_DMA0
,
178 NV20TCL_TX_FORMAT_DMA1
,
179 bo_flags
| NOUVEAU_BO_OR
);
181 nouveau_bo_markl(bctx
, kelvin
, NV20TCL_TX_OFFSET(i
),
184 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_WRAP(i
), 1);
185 OUT_RING(chan
, tx_wrap
);
187 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_FILTER(i
), 1);
188 OUT_RING(chan
, tx_filter
);
190 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_ENABLE(i
), 1);
191 OUT_RING(chan
, tx_enable
);
193 context_dirty(ctx
, TEX_SHADER
);
197 nv20_emit_tex_shader(GLcontext
*ctx
, int emit
)
199 struct nouveau_channel
*chan
= context_chan(ctx
);
200 struct nouveau_grobj
*kelvin
= context_eng3d(ctx
);
201 uint32_t tx_shader_op
= 0;
204 for (i
= 0; i
< NV20_TEXTURE_UNITS
; i
++) {
205 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
)
208 tx_shader_op
|= NV20TCL_TX_SHADER_OP_TX0_TEXTURE_2D
<< 5 * i
;
211 BEGIN_RING(chan
, kelvin
, NV20TCL_TX_SHADER_OP
, 1);
212 OUT_RING(chan
, tx_shader_op
);