2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
33 * \author Keith Whitwell <keith@tungstengraphics.com>
35 * \todo Enable R300 texture tiling code?
38 #include "main/glheader.h"
39 #include "main/imports.h"
40 #include "main/context.h"
41 #include "main/macros.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/enums.h"
45 #include "main/simple_list.h"
47 #include "r300_context.h"
48 #include "radeon_mipmap_tree.h"
53 * Note that the _REV formats are the same as the non-REV formats. This is
54 * because the REV and non-REV formats are identical as a byte string, but
55 * differ when accessed as 16-bit or 32-bit words depending on the endianness of
56 * the host. Since the textures are transferred to the R300 as a byte string
57 * (i.e. without any byte-swapping), the R300 sees the REV and non-REV formats
58 * identically. -- paulus
61 int32_t r300TranslateTexFormat(gl_format mesaFormat
)
65 #ifdef MESA_LITTLE_ENDIAN
66 case MESA_FORMAT_RGBA8888
:
67 return R300_EASY_TX_FORMAT(Y
, Z
, W
, X
, W8Z8Y8X8
);
68 case MESA_FORMAT_RGBA8888_REV
:
69 return R300_EASY_TX_FORMAT(Z
, Y
, X
, W
, W8Z8Y8X8
);
70 case MESA_FORMAT_ARGB8888
:
71 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W8Z8Y8X8
);
72 case MESA_FORMAT_ARGB8888_REV
:
73 return R300_EASY_TX_FORMAT(W
, Z
, Y
, X
, W8Z8Y8X8
);
75 case MESA_FORMAT_RGBA8888
:
76 return R300_EASY_TX_FORMAT(Z
, Y
, X
, W
, W8Z8Y8X8
);
77 case MESA_FORMAT_RGBA8888_REV
:
78 return R300_EASY_TX_FORMAT(Y
, Z
, W
, X
, W8Z8Y8X8
);
79 case MESA_FORMAT_ARGB8888
:
80 return R300_EASY_TX_FORMAT(W
, Z
, Y
, X
, W8Z8Y8X8
);
81 case MESA_FORMAT_ARGB8888_REV
:
82 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W8Z8Y8X8
);
84 case MESA_FORMAT_XRGB8888
:
85 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, W8Z8Y8X8
);
86 case MESA_FORMAT_RGB888
:
87 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, W8Z8Y8X8
);
88 case MESA_FORMAT_RGB565
:
89 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, Z5Y6X5
);
90 case MESA_FORMAT_RGB565_REV
:
91 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, Z5Y6X5
);
92 case MESA_FORMAT_ARGB4444
:
93 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W4Z4Y4X4
);
94 case MESA_FORMAT_ARGB4444_REV
:
95 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W4Z4Y4X4
);
96 case MESA_FORMAT_ARGB1555
:
97 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W1Z5Y5X5
);
98 case MESA_FORMAT_ARGB1555_REV
:
99 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W1Z5Y5X5
);
100 case MESA_FORMAT_AL88
:
101 return R300_EASY_TX_FORMAT(X
, X
, X
, Y
, Y8X8
);
102 case MESA_FORMAT_AL88_REV
:
103 return R300_EASY_TX_FORMAT(X
, X
, X
, Y
, Y8X8
);
104 case MESA_FORMAT_RGB332
:
105 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, Z3Y3X2
);
107 return R300_EASY_TX_FORMAT(ZERO
, ZERO
, ZERO
, X
, X8
);
109 return R300_EASY_TX_FORMAT(X
, X
, X
, ONE
, X8
);
111 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X8
);
112 case MESA_FORMAT_CI8
:
113 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X8
);
114 case MESA_FORMAT_YCBCR
:
115 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, G8R8_G8B8
) | R300_TX_FORMAT_YUV_MODE
;
116 case MESA_FORMAT_YCBCR_REV
:
117 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, G8R8_G8B8
) | R300_TX_FORMAT_YUV_MODE
;
118 case MESA_FORMAT_RGB_DXT1
:
119 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, DXT1
);
120 case MESA_FORMAT_RGBA_DXT1
:
121 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, DXT1
);
122 case MESA_FORMAT_RGBA_DXT3
:
123 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, DXT3
);
124 case MESA_FORMAT_RGBA_DXT5
:
125 return R300_EASY_TX_FORMAT(Y
, Z
, W
, X
, DXT5
);
126 case MESA_FORMAT_RGBA_FLOAT32
:
127 return R300_EASY_TX_FORMAT(Z
, Y
, X
, W
, FL_R32G32B32A32
);
128 case MESA_FORMAT_RGBA_FLOAT16
:
129 return R300_EASY_TX_FORMAT(Z
, Y
, X
, W
, FL_R16G16B16A16
);
130 case MESA_FORMAT_ALPHA_FLOAT32
:
131 return R300_EASY_TX_FORMAT(ZERO
, ZERO
, ZERO
, X
, FL_I32
);
132 case MESA_FORMAT_ALPHA_FLOAT16
:
133 return R300_EASY_TX_FORMAT(ZERO
, ZERO
, ZERO
, X
, FL_I16
);
134 case MESA_FORMAT_LUMINANCE_FLOAT32
:
135 return R300_EASY_TX_FORMAT(X
, X
, X
, ONE
, FL_I32
);
136 case MESA_FORMAT_LUMINANCE_FLOAT16
:
137 return R300_EASY_TX_FORMAT(X
, X
, X
, ONE
, FL_I16
);
138 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
:
139 return R300_EASY_TX_FORMAT(X
, X
, X
, Y
, FL_I32A32
);
140 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
:
141 return R300_EASY_TX_FORMAT(X
, X
, X
, Y
, FL_I16A16
);
142 case MESA_FORMAT_INTENSITY_FLOAT32
:
143 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, FL_I32
);
144 case MESA_FORMAT_INTENSITY_FLOAT16
:
145 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, FL_I16
);
146 case MESA_FORMAT_Z16
:
147 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X16
);
148 case MESA_FORMAT_Z24_S8
:
149 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X24_Y8
);
150 case MESA_FORMAT_S8_Z24
:
151 return R300_EASY_TX_FORMAT(Y
, Y
, Y
, Y
, X24_Y8
);
152 case MESA_FORMAT_Z32
:
153 return R300_EASY_TX_FORMAT(X
, X
, X
, X
, X32
);
154 /* EXT_texture_sRGB */
155 case MESA_FORMAT_SARGB8
:
156 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W8Z8Y8X8
) | R300_TX_FORMAT_GAMMA
;
157 case MESA_FORMAT_SLA8
:
158 return R300_EASY_TX_FORMAT(X
, X
, X
, Y
, Y8X8
) | R300_TX_FORMAT_GAMMA
;
159 case MESA_FORMAT_SL8
:
160 return R300_EASY_TX_FORMAT(X
, X
, X
, ONE
, X8
) | R300_TX_FORMAT_GAMMA
;
161 case MESA_FORMAT_SRGB_DXT1
:
162 return R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, DXT1
) | R300_TX_FORMAT_GAMMA
;
163 case MESA_FORMAT_SRGBA_DXT1
:
164 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, DXT1
) | R300_TX_FORMAT_GAMMA
;
165 case MESA_FORMAT_SRGBA_DXT3
:
166 return R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, DXT3
) | R300_TX_FORMAT_GAMMA
;
167 case MESA_FORMAT_SRGBA_DXT5
:
168 return R300_EASY_TX_FORMAT(Y
, Z
, W
, X
, DXT5
) | R300_TX_FORMAT_GAMMA
;
174 void r300SetDepthTexMode(struct gl_texture_object
*tObj
)
176 static const GLuint formats
[3][3] = {
178 R300_EASY_TX_FORMAT(X
, X
, X
, ONE
, X16
),
179 R300_EASY_TX_FORMAT(X
, X
, X
, X
, X16
),
180 R300_EASY_TX_FORMAT(ZERO
, ZERO
, ZERO
, X
, X16
),
183 R300_EASY_TX_FORMAT(Y
, Y
, Y
, ONE
, X24_Y8
),
184 R300_EASY_TX_FORMAT(Y
, Y
, Y
, Y
, X24_Y8
),
185 R300_EASY_TX_FORMAT(ZERO
, ZERO
, ZERO
, Y
, X24_Y8
),
188 R300_EASY_TX_FORMAT(X
, X
, X
, ONE
, X32
),
189 R300_EASY_TX_FORMAT(X
, X
, X
, X
, X32
),
190 R300_EASY_TX_FORMAT(ZERO
, ZERO
, ZERO
, X
, X32
),
193 const GLuint
*format
;
199 t
= radeon_tex_obj(tObj
);
201 switch (tObj
->Image
[0][tObj
->BaseLevel
]->TexFormat
) {
202 case MESA_FORMAT_Z16
:
205 case MESA_FORMAT_S8_Z24
:
208 case MESA_FORMAT_Z32
:
212 /* Error...which should have already been caught by higher
219 switch (tObj
->Sampler
.DepthMode
) {
221 t
->pp_txformat
= format
[0];
224 t
->pp_txformat
= format
[1];
227 t
->pp_txformat
= format
[2];
230 /* Error...which should have already been caught by higher
240 * Compute the cached hardware register values for the given texture object.
242 * \param rmesa Context pointer
243 * \param t the r300 texture object
245 static void setup_hardware_state(r300ContextPtr rmesa
, radeonTexObj
*t
)
247 const struct gl_texture_image
*firstImage
;
248 firstImage
= t
->base
.Image
[0][t
->minLod
];
250 if (!t
->image_override
) {
251 if (firstImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
252 r300SetDepthTexMode(&t
->base
);
254 int32_t txformat
= r300TranslateTexFormat(firstImage
->TexFormat
);
256 _mesa_problem(rmesa
->radeon
.glCtx
, "%s: Invalid format %s",
257 __FUNCTION__
, _mesa_get_format_name(firstImage
->TexFormat
));
260 t
->pp_txformat
= (uint32_t) txformat
;
264 if (t
->image_override
&& t
->bo
)
267 t
->pp_txsize
= (((R300_TX_WIDTHMASK_MASK
& ((firstImage
->Width
- 1) << R300_TX_WIDTHMASK_SHIFT
)))
268 | ((R300_TX_HEIGHTMASK_MASK
& ((firstImage
->Height
- 1) << R300_TX_HEIGHTMASK_SHIFT
)))
269 | ((R300_TX_DEPTHMASK_MASK
& ((firstImage
->DepthLog2
) << R300_TX_DEPTHMASK_SHIFT
)))
270 | ((R300_TX_MAX_MIP_LEVEL_MASK
& ((t
->maxLod
- t
->minLod
) << R300_TX_MAX_MIP_LEVEL_SHIFT
))));
274 if (t
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
275 t
->pp_txformat
|= R300_TX_FORMAT_CUBIC_MAP
;
276 if (t
->base
.Target
== GL_TEXTURE_3D
)
277 t
->pp_txformat
|= R300_TX_FORMAT_3D
;
280 if (t
->base
.Target
== GL_TEXTURE_RECTANGLE_NV
) {
281 unsigned int align
= (64 / _mesa_get_format_bytes(firstImage
->TexFormat
)) - 1;
282 t
->pp_txsize
|= R300_TX_SIZE_TXPITCH_EN
;
283 if (!t
->image_override
)
284 t
->pp_txpitch
= ((firstImage
->Width
+ align
) & ~align
) - 1;
287 if (rmesa
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
) {
288 if (firstImage
->Width
> 2048)
289 t
->pp_txpitch
|= R500_TXWIDTH_BIT11
;
291 t
->pp_txpitch
&= ~R500_TXWIDTH_BIT11
;
292 if (firstImage
->Height
> 2048)
293 t
->pp_txpitch
|= R500_TXHEIGHT_BIT11
;
295 t
->pp_txpitch
&= ~R500_TXHEIGHT_BIT11
;
300 * Ensure the given texture is ready for rendering.
302 * Mostly this means populating the texture object's mipmap tree.
304 static GLboolean
r300_validate_texture(struct gl_context
* ctx
, struct gl_texture_object
*texObj
)
306 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
307 radeonTexObj
*t
= radeon_tex_obj(texObj
);
309 if (!radeon_validate_texture_miptree(ctx
, texObj
))
312 /* Configure the hardware registers (more precisely, the cached version
313 * of the hardware registers). */
314 setup_hardware_state(rmesa
, t
);
316 t
->validated
= GL_TRUE
;
321 * Ensure all enabled and complete textures are uploaded along with any buffers being used.
323 GLboolean
r300ValidateBuffers(struct gl_context
* ctx
)
325 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
326 struct radeon_renderbuffer
*rrb
;
330 radeon_cs_space_reset_bos(rmesa
->radeon
.cmdbuf
.cs
);
332 rrb
= radeon_get_colorbuffer(&rmesa
->radeon
);
334 if (rrb
&& rrb
->bo
) {
335 radeon_cs_space_add_persistent_bo(rmesa
->radeon
.cmdbuf
.cs
,
337 RADEON_GEM_DOMAIN_VRAM
);
341 rrb
= radeon_get_depthbuffer(&rmesa
->radeon
);
342 if (rrb
&& rrb
->bo
) {
343 radeon_cs_space_add_persistent_bo(rmesa
->radeon
.cmdbuf
.cs
,
345 RADEON_GEM_DOMAIN_VRAM
);
348 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; ++i
) {
351 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
)
354 if (!r300_validate_texture(ctx
, ctx
->Texture
.Unit
[i
]._Current
)) {
356 "failed to validate texture for unit %d.\n",
359 t
= radeon_tex_obj(ctx
->Texture
.Unit
[i
]._Current
);
360 if (t
->image_override
&& t
->bo
)
361 radeon_cs_space_add_persistent_bo(rmesa
->radeon
.cmdbuf
.cs
,
363 RADEON_GEM_DOMAIN_GTT
| RADEON_GEM_DOMAIN_VRAM
, 0);
365 radeon_cs_space_add_persistent_bo(rmesa
->radeon
.cmdbuf
.cs
,
367 RADEON_GEM_DOMAIN_GTT
| RADEON_GEM_DOMAIN_VRAM
, 0);
370 ret
= radeon_cs_space_check_with_bo(rmesa
->radeon
.cmdbuf
.cs
, first_elem(&rmesa
->radeon
.dma
.reserved
)->bo
, RADEON_GEM_DOMAIN_GTT
, 0);
376 void r300SetTexOffset(__DRIcontext
* pDRICtx
, GLint texname
,
377 unsigned long long offset
, GLint depth
, GLuint pitch
)
379 r300ContextPtr rmesa
= pDRICtx
->driverPrivate
;
380 struct gl_texture_object
*tObj
=
381 _mesa_lookup_texture(rmesa
->radeon
.glCtx
, texname
);
382 radeonTexObjPtr t
= radeon_tex_obj(tObj
);
388 t
->image_override
= GL_TRUE
;
394 t
->override_offset
= offset
;
395 t
->pp_txpitch
&= (1 << 13) -1;
400 t
->pp_txformat
= R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W8Z8Y8X8
);
405 t
->pp_txformat
= R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, W8Z8Y8X8
);
409 t
->pp_txformat
= R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, Z5Y6X5
);
415 t
->pp_txpitch
|= pitch_val
;
418 void r300SetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
, GLint texture_format
, __DRIdrawable
*dPriv
)
420 struct gl_texture_unit
*texUnit
;
421 struct gl_texture_object
*texObj
;
422 struct gl_texture_image
*texImage
;
423 struct radeon_renderbuffer
*rb
;
424 radeon_texture_image
*rImage
;
425 radeonContextPtr radeon
;
426 r300ContextPtr rmesa
;
427 struct radeon_framebuffer
*rfb
;
432 radeon
= pDRICtx
->driverPrivate
;
433 rmesa
= pDRICtx
->driverPrivate
;
435 rfb
= dPriv
->driverPrivate
;
436 texUnit
= &radeon
->glCtx
->Texture
.Unit
[radeon
->glCtx
->Texture
.CurrentUnit
];
437 texObj
= _mesa_select_tex_object(radeon
->glCtx
, texUnit
, target
);
438 texImage
= _mesa_get_tex_image(radeon
->glCtx
, texObj
, target
, 0);
440 rImage
= get_radeon_texture_image(texImage
);
441 t
= radeon_tex_obj(texObj
);
446 radeon_update_renderbuffers(pDRICtx
, dPriv
, GL_TRUE
);
447 rb
= rfb
->color_rb
[0];
448 if (rb
->bo
== NULL
) {
449 /* Failed to BO for the buffer */
453 _mesa_lock_texture(radeon
->glCtx
, texObj
);
455 radeon_bo_unref(t
->bo
);
459 radeon_bo_unref(rImage
->bo
);
463 radeon_miptree_unreference(&t
->mt
);
464 radeon_miptree_unreference(&rImage
->mt
);
467 radeon_bo_ref(rImage
->bo
);
469 radeon_bo_ref(t
->bo
);
471 t
->image_override
= GL_TRUE
;
472 t
->override_offset
= 0;
473 t
->pp_txpitch
&= (1 << 13) -1;
474 pitch_val
= rb
->pitch
;
477 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
) {
478 texFormat
= MESA_FORMAT_RGB888
;
479 t
->pp_txformat
= R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, W8Z8Y8X8
);
482 texFormat
= MESA_FORMAT_ARGB8888
;
483 t
->pp_txformat
= R300_EASY_TX_FORMAT(X
, Y
, Z
, W
, W8Z8Y8X8
);
489 texFormat
= MESA_FORMAT_RGB888
;
490 t
->pp_txformat
= R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, W8Z8Y8X8
);
494 texFormat
= MESA_FORMAT_RGB565
;
495 t
->pp_txformat
= R300_EASY_TX_FORMAT(X
, Y
, Z
, ONE
, Z5Y6X5
);
500 _mesa_init_teximage_fields(radeon
->glCtx
, target
, texImage
,
501 rb
->base
.Width
, rb
->base
.Height
, 1, 0,
503 texImage
->RowStride
= rb
->pitch
/ rb
->cpp
;
507 t
->pp_txsize
= (((R300_TX_WIDTHMASK_MASK
& ((rb
->base
.Width
- 1) << R300_TX_WIDTHMASK_SHIFT
)))
508 | ((R300_TX_HEIGHTMASK_MASK
& ((rb
->base
.Height
- 1) << R300_TX_HEIGHTMASK_SHIFT
))));
509 t
->pp_txsize
|= R300_TX_SIZE_TXPITCH_EN
;
510 t
->pp_txpitch
|= pitch_val
;
512 if (rmesa
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
) {
513 if (rb
->base
.Width
> 2048)
514 t
->pp_txpitch
|= R500_TXWIDTH_BIT11
;
516 t
->pp_txpitch
&= ~R500_TXWIDTH_BIT11
;
517 if (rb
->base
.Height
> 2048)
518 t
->pp_txpitch
|= R500_TXHEIGHT_BIT11
;
520 t
->pp_txpitch
&= ~R500_TXHEIGHT_BIT11
;
522 t
->validated
= GL_TRUE
;
523 _mesa_unlock_texture(radeon
->glCtx
, texObj
);
527 void r300SetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
, __DRIdrawable
*dPriv
)
529 r300SetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);