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
;
38 state
->format0
= R300_TX_WIDTH((pt
->width
[0] - 1) & 0x7ff) |
39 R300_TX_HEIGHT((pt
->height
[0] - 1) & 0x7ff);
41 if (!util_is_power_of_two(pt
->width
[0]) ||
42 !util_is_power_of_two(pt
->height
[0])) {
44 /* rectangles love this */
45 state
->format0
|= R300_TX_PITCH_EN
;
47 stride
= r300_texture_get_stride(tex
, 0) / pt
->block
.size
;
48 state
->format2
= (stride
- 1) & 0x1fff;
51 /* power of two textures (3D, mipmaps, and no pitch) */
52 state
->format0
|= R300_TX_DEPTH(util_logbase2(pt
->depth
[0]) & 0xf) |
53 R300_TX_NUM_LEVELS(pt
->last_level
& 0xf);
56 state
->format1
= r300_translate_texformat(pt
->format
);
57 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
58 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
60 if (pt
->target
== PIPE_TEXTURE_3D
) {
61 state
->format1
|= R300_TX_FORMAT_3D
;
64 /* large textures on r500 */
67 if (pt
->width
[0] > 2048) {
68 state
->format2
|= R500_TXWIDTH_BIT11
;
70 if (pt
->height
[0] > 2048) {
71 state
->format2
|= R500_TXHEIGHT_BIT11
;
74 assert(is_r500
|| (pt
->width
[0] <= 2048 && pt
->height
[0] <= 2048));
76 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
77 pt
->width
[0], pt
->height
[0], pt
->last_level
);
80 unsigned r300_texture_get_offset(struct r300_texture
* tex
, unsigned level
,
81 unsigned zslice
, unsigned face
)
83 unsigned offset
= tex
->offset
[level
];
85 switch (tex
->tex
.target
) {
88 return offset
+ zslice
* tex
->layer_size
[level
];
90 case PIPE_TEXTURE_CUBE
:
92 return offset
+ face
* tex
->layer_size
[level
];
95 assert(zslice
== 0 && face
== 0);
101 * Return the stride, in bytes, of the texture images of the given texture
102 * at the given level.
104 unsigned r300_texture_get_stride(struct r300_texture
* tex
, unsigned level
)
106 if (tex
->stride_override
)
107 return tex
->stride_override
;
109 if (level
> tex
->tex
.last_level
) {
110 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__
,
111 level
, tex
->tex
.last_level
);
115 return align(pf_get_stride(&tex
->tex
.block
, tex
->tex
.width
[level
]), 32);
118 static void r300_setup_miptree(struct r300_texture
* tex
)
120 struct pipe_texture
* base
= &tex
->tex
;
121 int stride
, size
, layer_size
;
124 for (i
= 0; i
<= base
->last_level
; i
++) {
126 base
->width
[i
] = minify(base
->width
[i
-1]);
127 base
->height
[i
] = minify(base
->height
[i
-1]);
128 base
->depth
[i
] = minify(base
->depth
[i
-1]);
131 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
132 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->height
[i
]);
134 stride
= r300_texture_get_stride(tex
, i
);
135 layer_size
= stride
* base
->nblocksy
[i
];
137 if (base
->target
== PIPE_TEXTURE_CUBE
)
138 size
= layer_size
* 6;
140 size
= layer_size
* base
->depth
[i
];
142 tex
->offset
[i
] = align(tex
->size
, 32);
143 tex
->size
= tex
->offset
[i
] + size
;
144 tex
->layer_size
[i
] = layer_size
;
146 debug_printf("r300: Texture miptree: Level %d "
147 "(%dx%dx%d px, pitch %d bytes)\n",
148 i
, base
->width
[i
], base
->height
[i
], base
->depth
[i
],
153 /* Create a new texture. */
154 static struct pipe_texture
*
155 r300_texture_create(struct pipe_screen
* screen
,
156 const struct pipe_texture
* template)
158 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
164 tex
->tex
= *template;
165 pipe_reference_init(&tex
->tex
.reference
, 1);
166 tex
->tex
.screen
= screen
;
168 r300_setup_miptree(tex
);
170 r300_setup_texture_state(tex
, r300_screen(screen
)->caps
->is_r500
);
172 tex
->buffer
= screen
->buffer_create(screen
, 1024,
173 PIPE_BUFFER_USAGE_PIXEL
,
181 return (struct pipe_texture
*)tex
;
184 static void r300_texture_destroy(struct pipe_texture
* texture
)
186 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
188 pipe_buffer_reference(&tex
->buffer
, NULL
);
193 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
194 struct pipe_texture
* texture
,
200 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
201 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
204 offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
207 pipe_reference_init(&surface
->reference
, 1);
208 pipe_texture_reference(&surface
->texture
, texture
);
209 surface
->format
= texture
->format
;
210 surface
->width
= texture
->width
[level
];
211 surface
->height
= texture
->height
[level
];
212 surface
->offset
= offset
;
213 surface
->usage
= flags
;
214 surface
->zslice
= zslice
;
215 surface
->texture
= texture
;
216 surface
->face
= face
;
217 surface
->level
= level
;
223 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
225 pipe_texture_reference(&s
->texture
, NULL
);
229 static struct pipe_texture
*
230 r300_texture_blanket(struct pipe_screen
* screen
,
231 const struct pipe_texture
* base
,
232 const unsigned* stride
,
233 struct pipe_buffer
* buffer
)
235 struct r300_texture
* tex
;
237 tex
= CALLOC_STRUCT(r300_texture
);
243 pipe_reference_init(&tex
->tex
.reference
, 1);
244 tex
->tex
.screen
= screen
;
246 tex
->stride_override
= *stride
;
248 r300_setup_texture_state(tex
, r300_screen(screen
)->caps
->is_r500
);
250 pipe_buffer_reference(&tex
->buffer
, buffer
);
252 return (struct pipe_texture
*)tex
;
255 static struct pipe_video_surface
*
256 r300_video_surface_create(struct pipe_screen
*screen
,
257 enum pipe_video_chroma_format chroma_format
,
258 unsigned width
, unsigned height
)
260 struct r300_video_surface
*r300_vsfc
;
261 struct pipe_texture
template;
264 assert(width
&& height
);
266 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
270 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
271 r300_vsfc
->base
.screen
= screen
;
272 r300_vsfc
->base
.chroma_format
= chroma_format
;
273 r300_vsfc
->base
.width
= width
;
274 r300_vsfc
->base
.height
= height
;
276 memset(&template, 0, sizeof(struct pipe_texture
));
277 template.target
= PIPE_TEXTURE_2D
;
278 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
279 template.last_level
= 0;
280 template.width
[0] = util_next_power_of_two(width
);
281 template.height
[0] = util_next_power_of_two(height
);
282 template.depth
[0] = 1;
283 pf_get_block(template.format
, &template.block
);
284 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
285 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
287 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
294 return &r300_vsfc
->base
;
297 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
299 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
300 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
304 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
306 screen
->texture_create
= r300_texture_create
;
307 screen
->texture_destroy
= r300_texture_destroy
;
308 screen
->get_tex_surface
= r300_get_tex_surface
;
309 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
310 screen
->texture_blanket
= r300_texture_blanket
;
312 screen
->video_surface_create
= r300_video_surface_create
;
313 screen
->video_surface_destroy
= r300_video_surface_destroy
;
316 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
317 struct pipe_buffer
** buffer
,
320 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
325 pipe_buffer_reference(buffer
, tex
->buffer
);
328 *stride
= r300_texture_get_stride(tex
, 0);