2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "r300_texture.h"
25 static void r300_setup_texture_state(struct r300_texture
* tex
)
27 struct r300_texture_state
* state
= &tex
->state
;
28 struct pipe_texture
*pt
= &tex
->tex
;
30 state
->format0
= R300_TX_WIDTH((pt
->width
[0] - 1) & 0x7ff) |
31 R300_TX_HEIGHT((pt
->height
[0] - 1) & 0x7ff) |
32 R300_TX_DEPTH(util_logbase2(pt
->depth
[0]) & 0xf) |
33 R300_TX_NUM_LEVELS(pt
->last_level
) |
37 state
->format1
= r300_translate_texformat(pt
->format
);
38 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
39 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
41 if (pt
->target
== PIPE_TEXTURE_3D
) {
42 state
->format1
|= R300_TX_FORMAT_3D
;
45 state
->format2
= (r300_texture_get_stride(tex
, 0) / pt
->block
.size
) - 1;
47 /* Assume (somewhat foolishly) that oversized textures will
48 * not be permitted by the state tracker. */
49 if (pt
->width
[0] > 2048) {
50 state
->format2
|= R500_TXWIDTH_BIT11
;
52 if (pt
->height
[0] > 2048) {
53 state
->format2
|= R500_TXHEIGHT_BIT11
;
56 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
57 pt
->width
[0], pt
->height
[0], pt
->last_level
);
61 * Return the stride, in bytes, of the texture images of the given texture
64 unsigned r300_texture_get_stride(struct r300_texture
* tex
, unsigned level
)
66 if (tex
->stride_override
)
67 return tex
->stride_override
;
69 if (level
> tex
->tex
.last_level
) {
70 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__
, level
, tex
->tex
.last_level
);
74 return align(pf_get_stride(&tex
->tex
.block
, tex
->tex
.width
[level
]), 32);
77 static void r300_setup_miptree(struct r300_texture
* tex
)
79 struct pipe_texture
* base
= &tex
->tex
;
83 for (i
= 0; i
<= base
->last_level
; i
++) {
85 base
->width
[i
] = minify(base
->width
[i
-1]);
86 base
->height
[i
] = minify(base
->height
[i
-1]);
87 base
->depth
[i
] = minify(base
->depth
[i
-1]);
90 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
91 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->height
[i
]);
93 /* Radeons enjoy things in multiples of 64.
96 * POT, uncompressed, unmippmapped textures can be aligned to 32,
98 stride
= r300_texture_get_stride(tex
, i
);
99 size
= stride
* base
->nblocksy
[i
] * base
->depth
[i
];
101 tex
->offset
[i
] = align(tex
->size
, 32);
102 tex
->size
= tex
->offset
[i
] + size
;
104 debug_printf("r300: Texture miptree: Level %d "
105 "(%dx%dx%d px, pitch %d bytes)\n",
106 i
, base
->width
[i
], base
->height
[i
], base
->depth
[i
],
111 /* Create a new texture. */
112 static struct pipe_texture
*
113 r300_texture_create(struct pipe_screen
* screen
,
114 const struct pipe_texture
* template)
116 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
122 tex
->tex
= *template;
123 pipe_reference_init(&tex
->tex
.reference
, 1);
124 tex
->tex
.screen
= screen
;
126 r300_setup_miptree(tex
);
128 r300_setup_texture_state(tex
);
130 tex
->buffer
= screen
->buffer_create(screen
, 1024,
131 PIPE_BUFFER_USAGE_PIXEL
,
139 return (struct pipe_texture
*)tex
;
142 static void r300_texture_destroy(struct pipe_texture
* texture
)
144 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
146 pipe_buffer_reference(&tex
->buffer
, NULL
);
151 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
152 struct pipe_texture
* texture
,
158 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
159 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
162 /* XXX this is certainly dependent on tex target */
163 offset
= tex
->offset
[level
];
166 pipe_reference_init(&surface
->reference
, 1);
167 pipe_texture_reference(&surface
->texture
, texture
);
168 surface
->format
= texture
->format
;
169 surface
->width
= texture
->width
[level
];
170 surface
->height
= texture
->height
[level
];
171 surface
->offset
= offset
;
172 surface
->usage
= flags
;
178 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
180 pipe_texture_reference(&s
->texture
, NULL
);
184 static struct pipe_texture
*
185 r300_texture_blanket(struct pipe_screen
* screen
,
186 const struct pipe_texture
* base
,
187 const unsigned* stride
,
188 struct pipe_buffer
* buffer
)
190 struct r300_texture
* tex
;
192 /* XXX we should start doing mips now... */
193 if (base
->target
!= PIPE_TEXTURE_2D
||
194 base
->last_level
!= 0 ||
195 base
->depth
[0] != 1) {
199 tex
= CALLOC_STRUCT(r300_texture
);
205 pipe_reference_init(&tex
->tex
.reference
, 1);
206 tex
->tex
.screen
= screen
;
208 tex
->stride_override
= *stride
;
211 r300_setup_texture_state(tex
);
213 pipe_buffer_reference(&tex
->buffer
, buffer
);
215 return (struct pipe_texture
*)tex
;
218 static struct pipe_video_surface
*
219 r300_video_surface_create(struct pipe_screen
*screen
,
220 enum pipe_video_chroma_format chroma_format
,
221 unsigned width
, unsigned height
)
223 struct r300_video_surface
*r300_vsfc
;
224 struct pipe_texture
template;
227 assert(width
&& height
);
229 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
233 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
234 r300_vsfc
->base
.screen
= screen
;
235 r300_vsfc
->base
.chroma_format
= chroma_format
;
236 r300_vsfc
->base
.width
= width
;
237 r300_vsfc
->base
.height
= height
;
239 memset(&template, 0, sizeof(struct pipe_texture
));
240 template.target
= PIPE_TEXTURE_2D
;
241 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
242 template.last_level
= 0;
243 template.width
[0] = util_next_power_of_two(width
);
244 template.height
[0] = util_next_power_of_two(height
);
245 template.depth
[0] = 1;
246 pf_get_block(template.format
, &template.block
);
247 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
248 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
250 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
257 return &r300_vsfc
->base
;
260 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
262 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
263 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
267 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
269 screen
->texture_create
= r300_texture_create
;
270 screen
->texture_destroy
= r300_texture_destroy
;
271 screen
->get_tex_surface
= r300_get_tex_surface
;
272 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
273 screen
->texture_blanket
= r300_texture_blanket
;
275 screen
->video_surface_create
= r300_video_surface_create
;
276 screen
->video_surface_destroy
= r300_video_surface_destroy
;
279 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
280 struct pipe_buffer
** buffer
,
283 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
288 pipe_buffer_reference(buffer
, tex
->buffer
);
291 *stride
= r300_texture_get_stride(tex
, 0);