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 "pipe/p_screen.h"
25 #include "util/u_math.h"
26 #include "util/u_memory.h"
28 #include "r300_context.h"
29 #include "r300_texture.h"
30 #include "r300_screen.h"
32 static void r300_setup_texture_state(struct r300_texture
* tex
, boolean is_r500
)
34 struct r300_texture_state
* state
= &tex
->state
;
35 struct pipe_texture
*pt
= &tex
->tex
;
37 state
->format0
= R300_TX_WIDTH((pt
->width0
- 1) & 0x7ff) |
38 R300_TX_HEIGHT((pt
->height0
- 1) & 0x7ff);
41 /* rectangles love this */
42 state
->format0
|= R300_TX_PITCH_EN
;
43 state
->format2
= (tex
->pitch
[0] - 1) & 0x1fff;
45 /* power of two textures (3D, mipmaps, and no pitch) */
46 state
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth0
) & 0xf);
49 state
->format1
= r300_translate_texformat(pt
->format
);
50 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
51 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
53 if (pt
->target
== PIPE_TEXTURE_3D
) {
54 state
->format1
|= R300_TX_FORMAT_3D
;
57 /* large textures on r500 */
60 if (pt
->width0
> 2048) {
61 state
->format2
|= R500_TXWIDTH_BIT11
;
63 if (pt
->height0
> 2048) {
64 state
->format2
|= R500_TXHEIGHT_BIT11
;
67 assert(is_r500
|| (pt
->width0
<= 2048 && pt
->height0
<= 2048));
69 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
70 pt
->width0
, pt
->height0
, pt
->last_level
);
73 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
74 unsigned zslice
, unsigned face
)
76 unsigned offset
= tex
->offset
[level
];
78 switch (tex
->tex
.target
) {
81 return offset
+ zslice
* tex
->layer_size
[level
];
83 case PIPE_TEXTURE_CUBE
:
85 return offset
+ face
* tex
->layer_size
[level
];
88 assert(zslice
== 0 && face
== 0);
94 * Return the stride, in bytes, of the texture images of the given texture
97 unsigned r300_texture_get_stride(struct r300_texture
* tex
, unsigned level
)
99 if (tex
->stride_override
)
100 return tex
->stride_override
;
102 if (level
> tex
->tex
.last_level
) {
103 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__
,
104 level
, tex
->tex
.last_level
);
108 return align(pf_get_stride(&tex
->tex
.block
, u_minify(tex
->tex
.width0
, level
)), 32);
111 static void r300_setup_miptree(struct r300_texture
* tex
)
113 struct pipe_texture
* base
= &tex
->tex
;
114 int stride
, size
, layer_size
;
117 for (i
= 0; i
<= base
->last_level
; i
++) {
118 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, u_minify(base
->width0
, i
));
119 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, u_minify(base
->height0
, i
));
121 stride
= r300_texture_get_stride(tex
, i
);
122 layer_size
= stride
* base
->nblocksy
[i
];
124 if (base
->target
== PIPE_TEXTURE_CUBE
)
125 size
= layer_size
* 6;
127 size
= layer_size
* u_minify(base
->depth0
, i
);
129 tex
->offset
[i
] = align(tex
->size
, 32);
130 tex
->size
= tex
->offset
[i
] + size
;
131 tex
->layer_size
[i
] = layer_size
;
132 tex
->pitch
[i
] = stride
/ base
->block
.size
;
134 debug_printf("r300: Texture miptree: Level %d "
135 "(%dx%dx%d px, pitch %d bytes)\n",
136 i
, u_minify(base
->width0
, i
), u_minify(base
->height0
, i
),
137 u_minify(base
->depth0
, i
), stride
);
141 static void r300_setup_flags(struct r300_texture
* tex
)
143 tex
->is_npot
= !util_is_power_of_two(tex
->tex
.width0
) ||
144 !util_is_power_of_two(tex
->tex
.height0
);
147 /* Create a new texture. */
148 static struct pipe_texture
*
149 r300_texture_create(struct pipe_screen
* screen
,
150 const struct pipe_texture
* template)
152 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
158 tex
->tex
= *template;
159 pipe_reference_init(&tex
->tex
.reference
, 1);
160 tex
->tex
.screen
= screen
;
162 r300_setup_flags(tex
);
163 r300_setup_miptree(tex
);
164 r300_setup_texture_state(tex
, r300_screen(screen
)->caps
->is_r500
);
166 tex
->buffer
= screen
->buffer_create(screen
, 1024,
167 PIPE_BUFFER_USAGE_PIXEL
,
175 return (struct pipe_texture
*)tex
;
178 static void r300_texture_destroy(struct pipe_texture
* texture
)
180 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
182 pipe_buffer_reference(&tex
->buffer
, NULL
);
187 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
188 struct pipe_texture
* texture
,
194 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
195 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
198 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
201 pipe_reference_init(&surface
->reference
, 1);
202 pipe_texture_reference(&surface
->texture
, texture
);
203 surface
->format
= texture
->format
;
204 surface
->width
= u_minify(texture
->width0
, level
);
205 surface
->height
= u_minify(texture
->height0
, level
);
206 surface
->offset
= offset
;
207 surface
->usage
= flags
;
208 surface
->zslice
= zslice
;
209 surface
->texture
= texture
;
210 surface
->face
= face
;
211 surface
->level
= level
;
217 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
219 pipe_texture_reference(&s
->texture
, NULL
);
223 static struct pipe_texture
*
224 r300_texture_blanket(struct pipe_screen
* screen
,
225 const struct pipe_texture
* base
,
226 const unsigned* stride
,
227 struct pipe_buffer
* buffer
)
229 struct r300_texture
* tex
;
231 /* Support only 2D textures without mipmaps */
232 if (base
->target
!= PIPE_TEXTURE_2D
||
234 base
->last_level
!= 0) {
238 tex
= CALLOC_STRUCT(r300_texture
);
244 pipe_reference_init(&tex
->tex
.reference
, 1);
245 tex
->tex
.screen
= screen
;
247 tex
->stride_override
= *stride
;
248 tex
->pitch
[0] = *stride
/ base
->block
.size
;
250 r300_setup_flags(tex
);
251 r300_setup_texture_state(tex
, r300_screen(screen
)->caps
->is_r500
);
253 pipe_buffer_reference(&tex
->buffer
, buffer
);
255 return (struct pipe_texture
*)tex
;
258 static struct pipe_video_surface
*
259 r300_video_surface_create(struct pipe_screen
*screen
,
260 enum pipe_video_chroma_format chroma_format
,
261 unsigned width
, unsigned height
)
263 struct r300_video_surface
*r300_vsfc
;
264 struct pipe_texture
template;
267 assert(width
&& height
);
269 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
273 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
274 r300_vsfc
->base
.screen
= screen
;
275 r300_vsfc
->base
.chroma_format
= chroma_format
;
276 r300_vsfc
->base
.width
= width
;
277 r300_vsfc
->base
.height
= height
;
279 memset(&template, 0, sizeof(struct pipe_texture
));
280 template.target
= PIPE_TEXTURE_2D
;
281 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
282 template.last_level
= 0;
283 template.width0
= util_next_power_of_two(width
);
284 template.height0
= util_next_power_of_two(height
);
286 pf_get_block(template.format
, &template.block
);
287 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
288 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
290 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
297 return &r300_vsfc
->base
;
300 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
302 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
303 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
307 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
309 screen
->texture_create
= r300_texture_create
;
310 screen
->texture_destroy
= r300_texture_destroy
;
311 screen
->get_tex_surface
= r300_get_tex_surface
;
312 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
313 screen
->texture_blanket
= r300_texture_blanket
;
315 screen
->video_surface_create
= r300_video_surface_create
;
316 screen
->video_surface_destroy
= r300_video_surface_destroy
;
319 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
320 struct pipe_buffer
** buffer
,
323 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
328 pipe_buffer_reference(buffer
, tex
->buffer
);
331 *stride
= r300_texture_get_stride(tex
, 0);