r300_texture.c revision 4a49301e
1/* 2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com> 3 * 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: 10 * 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 13 * Software. 14 * 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. */ 22 23#include "pipe/p_screen.h" 24 25#include "util/u_math.h" 26#include "util/u_memory.h" 27 28#include "r300_context.h" 29#include "r300_texture.h" 30#include "r300_screen.h" 31 32static void r300_setup_texture_state(struct r300_texture* tex, boolean is_r500) 33{ 34 struct r300_texture_state* state = &tex->state; 35 struct pipe_texture *pt = &tex->tex; 36 37 state->format0 = R300_TX_WIDTH((pt->width[0] - 1) & 0x7ff) | 38 R300_TX_HEIGHT((pt->height[0] - 1) & 0x7ff); 39 40 if (tex->is_npot) { 41 /* rectangles love this */ 42 state->format0 |= R300_TX_PITCH_EN; 43 state->format2 = (tex->pitch[0] - 1) & 0x1fff; 44 } else { 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); 48 } 49 50 state->format1 = r300_translate_texformat(pt->format); 51 if (pt->target == PIPE_TEXTURE_CUBE) { 52 state->format1 |= R300_TX_FORMAT_CUBIC_MAP; 53 } 54 if (pt->target == PIPE_TEXTURE_3D) { 55 state->format1 |= R300_TX_FORMAT_3D; 56 } 57 58 /* large textures on r500 */ 59 if (is_r500) 60 { 61 if (pt->width[0] > 2048) { 62 state->format2 |= R500_TXWIDTH_BIT11; 63 } 64 if (pt->height[0] > 2048) { 65 state->format2 |= R500_TXHEIGHT_BIT11; 66 } 67 } 68 assert(is_r500 || (pt->width[0] <= 2048 && pt->height[0] <= 2048)); 69 70 debug_printf("r300: Set texture state (%dx%d, %d levels)\n", 71 pt->width[0], pt->height[0], pt->last_level); 72} 73 74unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level, 75 unsigned zslice, unsigned face) 76{ 77 unsigned offset = tex->offset[level]; 78 79 switch (tex->tex.target) { 80 case PIPE_TEXTURE_3D: 81 assert(face == 0); 82 return offset + zslice * tex->layer_size[level]; 83 84 case PIPE_TEXTURE_CUBE: 85 assert(zslice == 0); 86 return offset + face * tex->layer_size[level]; 87 88 default: 89 assert(zslice == 0 && face == 0); 90 return offset; 91 } 92} 93 94/** 95 * Return the stride, in bytes, of the texture images of the given texture 96 * at the given level. 97 */ 98unsigned r300_texture_get_stride(struct r300_texture* tex, unsigned level) 99{ 100 if (tex->stride_override) 101 return tex->stride_override; 102 103 if (level > tex->tex.last_level) { 104 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__, 105 level, tex->tex.last_level); 106 return 0; 107 } 108 109 return align(pf_get_stride(&tex->tex.block, tex->tex.width[level]), 32); 110} 111 112static void r300_setup_miptree(struct r300_texture* tex) 113{ 114 struct pipe_texture* base = &tex->tex; 115 int stride, size, layer_size; 116 int i; 117 118 for (i = 0; i <= base->last_level; i++) { 119 if (i > 0) { 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]); 123 } 124 125 base->nblocksx[i] = pf_get_nblocksx(&base->block, base->width[i]); 126 base->nblocksy[i] = pf_get_nblocksy(&base->block, base->height[i]); 127 128 stride = r300_texture_get_stride(tex, i); 129 layer_size = stride * base->nblocksy[i]; 130 131 if (base->target == PIPE_TEXTURE_CUBE) 132 size = layer_size * 6; 133 else 134 size = layer_size * base->depth[i]; 135 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; 140 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], 144 stride); 145 } 146} 147 148static void r300_setup_flags(struct r300_texture* tex) 149{ 150 tex->is_npot = !util_is_power_of_two(tex->tex.width[0]) || 151 !util_is_power_of_two(tex->tex.height[0]); 152} 153 154/* Create a new texture. */ 155static struct pipe_texture* 156 r300_texture_create(struct pipe_screen* screen, 157 const struct pipe_texture* template) 158{ 159 struct r300_texture* tex = CALLOC_STRUCT(r300_texture); 160 161 if (!tex) { 162 return NULL; 163 } 164 165 tex->tex = *template; 166 pipe_reference_init(&tex->tex.reference, 1); 167 tex->tex.screen = screen; 168 169 r300_setup_flags(tex); 170 r300_setup_miptree(tex); 171 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500); 172 173 tex->buffer = screen->buffer_create(screen, 1024, 174 PIPE_BUFFER_USAGE_PIXEL, 175 tex->size); 176 177 if (!tex->buffer) { 178 FREE(tex); 179 return NULL; 180 } 181 182 return (struct pipe_texture*)tex; 183} 184 185static void r300_texture_destroy(struct pipe_texture* texture) 186{ 187 struct r300_texture* tex = (struct r300_texture*)texture; 188 189 pipe_buffer_reference(&tex->buffer, NULL); 190 191 FREE(tex); 192} 193 194static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen, 195 struct pipe_texture* texture, 196 unsigned face, 197 unsigned level, 198 unsigned zslice, 199 unsigned flags) 200{ 201 struct r300_texture* tex = (struct r300_texture*)texture; 202 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface); 203 unsigned offset; 204 205 offset = r300_texture_get_offset(tex, level, zslice, face); 206 207 if (surface) { 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; 219 } 220 221 return surface; 222} 223 224static void r300_tex_surface_destroy(struct pipe_surface* s) 225{ 226 pipe_texture_reference(&s->texture, NULL); 227 FREE(s); 228} 229 230static 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) 235{ 236 struct r300_texture* tex; 237 238 /* Support only 2D textures without mipmaps */ 239 if (base->target != PIPE_TEXTURE_2D || 240 base->depth[0] != 1 || 241 base->last_level != 0) { 242 return NULL; 243 } 244 245 tex = CALLOC_STRUCT(r300_texture); 246 if (!tex) { 247 return NULL; 248 } 249 250 tex->tex = *base; 251 pipe_reference_init(&tex->tex.reference, 1); 252 tex->tex.screen = screen; 253 254 tex->stride_override = *stride; 255 tex->pitch[0] = *stride / base->block.size; 256 257 r300_setup_flags(tex); 258 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500); 259 260 pipe_buffer_reference(&tex->buffer, buffer); 261 262 return (struct pipe_texture*)tex; 263} 264 265static struct pipe_video_surface * 266r300_video_surface_create(struct pipe_screen *screen, 267 enum pipe_video_chroma_format chroma_format, 268 unsigned width, unsigned height) 269{ 270 struct r300_video_surface *r300_vsfc; 271 struct pipe_texture template; 272 273 assert(screen); 274 assert(width && height); 275 276 r300_vsfc = CALLOC_STRUCT(r300_video_surface); 277 if (!r300_vsfc) 278 return NULL; 279 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; 285 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; 296 297 r300_vsfc->tex = screen->texture_create(screen, &template); 298 if (!r300_vsfc->tex) 299 { 300 FREE(r300_vsfc); 301 return NULL; 302 } 303 304 return &r300_vsfc->base; 305} 306 307static void r300_video_surface_destroy(struct pipe_video_surface *vsfc) 308{ 309 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc); 310 pipe_texture_reference(&r300_vsfc->tex, NULL); 311 FREE(r300_vsfc); 312} 313 314void r300_init_screen_texture_functions(struct pipe_screen* screen) 315{ 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; 321 322 screen->video_surface_create = r300_video_surface_create; 323 screen->video_surface_destroy= r300_video_surface_destroy; 324} 325 326boolean r300_get_texture_buffer(struct pipe_texture* texture, 327 struct pipe_buffer** buffer, 328 unsigned* stride) 329{ 330 struct r300_texture* tex = (struct r300_texture*)texture; 331 if (!tex) { 332 return FALSE; 333 } 334 335 pipe_buffer_reference(buffer, tex->buffer); 336 337 if (stride) { 338 *stride = r300_texture_get_stride(tex, 0); 339 } 340 341 return TRUE; 342} 343