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
,
30 struct r300_texture_state
* state
= &tex
->state
;
32 state
->format0
= R300_TX_WIDTH((width
- 1) & 0x7ff) |
33 R300_TX_HEIGHT((height
- 1) & 0x7ff) |
34 R300_TX_NUM_LEVELS(levels
) |
38 state
->format1
= r300_translate_texformat(tex
->tex
.format
);
40 state
->format2
= r300_texture_get_stride(tex
, 0);
42 /* Assume (somewhat foolishly) that oversized textures will
43 * not be permitted by the state tracker. */
45 state
->format2
|= R500_TXWIDTH_BIT11
;
48 state
->format2
|= R500_TXHEIGHT_BIT11
;
51 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
52 width
, height
, levels
);
56 * Return the stride, in bytes, of the texture images of the given texture
59 unsigned r300_texture_get_stride(struct r300_texture
* tex
, unsigned level
)
61 if (tex
->stride_override
)
62 return tex
->stride_override
;
64 if (level
> tex
->tex
.last_level
) {
65 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__
, level
, tex
->tex
.last_level
);
69 return align(pf_get_stride(&tex
->tex
.block
, tex
->tex
.width
[level
]), 32);
72 static void r300_setup_miptree(struct r300_texture
* tex
)
74 struct pipe_texture
* base
= &tex
->tex
;
78 for (i
= 0; i
<= base
->last_level
; i
++) {
80 base
->width
[i
] = minify(base
->width
[i
-1]);
81 base
->height
[i
] = minify(base
->height
[i
-1]);
82 base
->depth
[i
] = minify(base
->depth
[i
-1]);
85 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
86 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->height
[i
]);
88 /* Radeons enjoy things in multiples of 64.
91 * POT, uncompressed, unmippmapped textures can be aligned to 32,
93 stride
= r300_texture_get_stride(tex
, i
);
94 size
= stride
* base
->nblocksy
[i
] * base
->depth
[i
];
96 tex
->offset
[i
] = align(tex
->size
, 32);
97 tex
->size
= tex
->offset
[i
] + size
;
99 debug_printf("r300: Texture miptree: Level %d "
100 "(%dx%dx%d px, pitch %d bytes)\n",
101 i
, base
->width
[i
], base
->height
[i
], base
->depth
[i
],
106 /* Create a new texture. */
107 static struct pipe_texture
*
108 r300_texture_create(struct pipe_screen
* screen
,
109 const struct pipe_texture
* template)
111 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
117 tex
->tex
= *template;
118 pipe_reference_init(&tex
->tex
.reference
, 1);
119 tex
->tex
.screen
= screen
;
121 r300_setup_miptree(tex
);
123 r300_setup_texture_state(tex
, template->width
[0], template->height
[0],
124 template->last_level
);
126 tex
->buffer
= screen
->buffer_create(screen
, 1024,
127 PIPE_BUFFER_USAGE_PIXEL
,
135 return (struct pipe_texture
*)tex
;
138 static void r300_texture_destroy(struct pipe_texture
* texture
)
140 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
142 pipe_buffer_reference(&tex
->buffer
, NULL
);
147 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
148 struct pipe_texture
* texture
,
154 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
155 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
158 /* XXX this is certainly dependent on tex target */
159 offset
= tex
->offset
[level
];
162 pipe_reference_init(&surface
->reference
, 1);
163 pipe_texture_reference(&surface
->texture
, texture
);
164 surface
->format
= texture
->format
;
165 surface
->width
= texture
->width
[level
];
166 surface
->height
= texture
->height
[level
];
167 surface
->offset
= offset
;
168 surface
->usage
= flags
;
174 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
176 pipe_texture_reference(&s
->texture
, NULL
);
180 static struct pipe_texture
*
181 r300_texture_blanket(struct pipe_screen
* screen
,
182 const struct pipe_texture
* base
,
183 const unsigned* stride
,
184 struct pipe_buffer
* buffer
)
186 struct r300_texture
* tex
;
188 /* XXX we should start doing mips now... */
189 if (base
->target
!= PIPE_TEXTURE_2D
||
190 base
->last_level
!= 0 ||
191 base
->depth
[0] != 1) {
195 tex
= CALLOC_STRUCT(r300_texture
);
201 pipe_reference_init(&tex
->tex
.reference
, 1);
202 tex
->tex
.screen
= screen
;
204 tex
->stride_override
= *stride
;
207 r300_setup_texture_state(tex
, tex
->tex
.width
[0], tex
->tex
.height
[0], 0);
209 pipe_buffer_reference(&tex
->buffer
, buffer
);
211 return (struct pipe_texture
*)tex
;
214 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
216 screen
->texture_create
= r300_texture_create
;
217 screen
->texture_destroy
= r300_texture_destroy
;
218 screen
->get_tex_surface
= r300_get_tex_surface
;
219 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
220 screen
->texture_blanket
= r300_texture_blanket
;
223 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
224 struct pipe_buffer
** buffer
,
227 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
232 pipe_buffer_reference(buffer
, tex
->buffer
);
235 *stride
= r300_texture_get_stride(tex
, 0);