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
->width
[0] - 1) & 0x7ff) |
38 R300_TX_HEIGHT((pt
->height
[0] - 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
->depth
[0]) & 0xf) |
47 R300_TX_NUM_LEVELS(pt
->last_level
& 0xf);
50 state
->format1
= r300_translate_texformat(pt
->format
);
51 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
52 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
54 if (pt
->target
== PIPE_TEXTURE_3D
) {
55 state
->format1
|= R300_TX_FORMAT_3D
;
58 /* large textures on r500 */
61 if (pt
->width
[0] > 2048) {
62 state
->format2
|= R500_TXWIDTH_BIT11
;
64 if (pt
->height
[0] > 2048) {
65 state
->format2
|= R500_TXHEIGHT_BIT11
;
68 assert(is_r500
|| (pt
->width
[0] <= 2048 && pt
->height
[0] <= 2048));
70 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
71 pt
->width
[0], pt
->height
[0], pt
->last_level
);
74 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
75 unsigned zslice
, unsigned face
)
77 unsigned offset
= tex
->offset
[level
];
79 switch (tex
->tex
.target
) {
82 return offset
+ zslice
* tex
->layer_size
[level
];
84 case PIPE_TEXTURE_CUBE
:
86 return offset
+ face
* tex
->layer_size
[level
];
89 assert(zslice
== 0 && face
== 0);
95 * Return the stride, in bytes, of the texture images of the given texture
98 unsigned r300_texture_get_stride(struct r300_texture
* tex
, unsigned level
)
100 if (tex
->stride_override
)
101 return tex
->stride_override
;
103 if (level
> tex
->tex
.last_level
) {
104 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__
,
105 level
, tex
->tex
.last_level
);
109 return align(pf_get_stride(&tex
->tex
.block
, tex
->tex
.width
[level
]), 32);
112 static void r300_setup_miptree(struct r300_texture
* tex
)
114 struct pipe_texture
* base
= &tex
->tex
;
115 int stride
, size
, layer_size
;
118 for (i
= 0; i
<= base
->last_level
; i
++) {
120 base
->width
[i
] = minify(base
->width
[i
-1]);
121 base
->height
[i
] = minify(base
->height
[i
-1]);
122 base
->depth
[i
] = minify(base
->depth
[i
-1]);
125 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
126 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->height
[i
]);
128 stride
= r300_texture_get_stride(tex
, i
);
129 layer_size
= stride
* base
->nblocksy
[i
];
131 if (base
->target
== PIPE_TEXTURE_CUBE
)
132 size
= layer_size
* 6;
134 size
= layer_size
* base
->depth
[i
];
136 tex
->offset
[i
] = align(tex
->size
, 32);
137 tex
->size
= tex
->offset
[i
] + size
;
138 tex
->layer_size
[i
] = layer_size
;
139 tex
->pitch
[i
] = stride
/ base
->block
.size
;
141 debug_printf("r300: Texture miptree: Level %d "
142 "(%dx%dx%d px, pitch %d bytes)\n",
143 i
, base
->width
[i
], base
->height
[i
], base
->depth
[i
],
148 static void r300_setup_flags(struct r300_texture
* tex
)
150 tex
->is_npot
= !util_is_power_of_two(tex
->tex
.width
[0]) ||
151 !util_is_power_of_two(tex
->tex
.height
[0]);
154 /* Create a new texture. */
155 static struct pipe_texture
*
156 r300_texture_create(struct pipe_screen
* screen
,
157 const struct pipe_texture
* template)
159 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
165 tex
->tex
= *template;
166 pipe_reference_init(&tex
->tex
.reference
, 1);
167 tex
->tex
.screen
= screen
;
169 r300_setup_flags(tex
);
170 r300_setup_miptree(tex
);
171 r300_setup_texture_state(tex
, r300_screen(screen
)->caps
->is_r500
);
173 tex
->buffer
= screen
->buffer_create(screen
, 1024,
174 PIPE_BUFFER_USAGE_PIXEL
,
182 return (struct pipe_texture
*)tex
;
185 static void r300_texture_destroy(struct pipe_texture
* texture
)
187 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
189 pipe_buffer_reference(&tex
->buffer
, NULL
);
194 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
195 struct pipe_texture
* texture
,
201 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
202 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
205 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
208 pipe_reference_init(&surface
->reference
, 1);
209 pipe_texture_reference(&surface
->texture
, texture
);
210 surface
->format
= texture
->format
;
211 surface
->width
= texture
->width
[level
];
212 surface
->height
= texture
->height
[level
];
213 surface
->offset
= offset
;
214 surface
->usage
= flags
;
215 surface
->zslice
= zslice
;
216 surface
->texture
= texture
;
217 surface
->face
= face
;
218 surface
->level
= level
;
224 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
226 pipe_texture_reference(&s
->texture
, NULL
);
230 static struct pipe_texture
*
231 r300_texture_blanket(struct pipe_screen
* screen
,
232 const struct pipe_texture
* base
,
233 const unsigned* stride
,
234 struct pipe_buffer
* buffer
)
236 struct r300_texture
* tex
;
238 /* Support only 2D textures without mipmaps */
239 if (base
->target
!= PIPE_TEXTURE_2D
||
240 base
->depth
[0] != 1 ||
241 base
->last_level
!= 0) {
245 tex
= CALLOC_STRUCT(r300_texture
);
251 pipe_reference_init(&tex
->tex
.reference
, 1);
252 tex
->tex
.screen
= screen
;
254 tex
->stride_override
= *stride
;
255 tex
->pitch
[0] = *stride
/ base
->block
.size
;
257 r300_setup_flags(tex
);
258 r300_setup_texture_state(tex
, r300_screen(screen
)->caps
->is_r500
);
260 pipe_buffer_reference(&tex
->buffer
, buffer
);
262 return (struct pipe_texture
*)tex
;
265 static struct pipe_video_surface
*
266 r300_video_surface_create(struct pipe_screen
*screen
,
267 enum pipe_video_chroma_format chroma_format
,
268 unsigned width
, unsigned height
)
270 struct r300_video_surface
*r300_vsfc
;
271 struct pipe_texture
template;
274 assert(width
&& height
);
276 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
280 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
281 r300_vsfc
->base
.screen
= screen
;
282 r300_vsfc
->base
.chroma_format
= chroma_format
;
283 r300_vsfc
->base
.width
= width
;
284 r300_vsfc
->base
.height
= height
;
286 memset(&template, 0, sizeof(struct pipe_texture
));
287 template.target
= PIPE_TEXTURE_2D
;
288 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
289 template.last_level
= 0;
290 template.width
[0] = util_next_power_of_two(width
);
291 template.height
[0] = util_next_power_of_two(height
);
292 template.depth
[0] = 1;
293 pf_get_block(template.format
, &template.block
);
294 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
295 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
297 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
304 return &r300_vsfc
->base
;
307 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
309 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
310 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
314 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
316 screen
->texture_create
= r300_texture_create
;
317 screen
->texture_destroy
= r300_texture_destroy
;
318 screen
->get_tex_surface
= r300_get_tex_surface
;
319 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
320 screen
->texture_blanket
= r300_texture_blanket
;
322 screen
->video_surface_create
= r300_video_surface_create
;
323 screen
->video_surface_destroy
= r300_video_surface_destroy
;
326 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
327 struct pipe_buffer
** buffer
,
330 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
335 pipe_buffer_reference(buffer
, tex
->buffer
);
338 *stride
= r300_texture_get_stride(tex
, 0);