r300g: Keep texture formats organized.
[mesa.git] / src / gallium / drivers / r300 / r300_texture.c
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
32 static 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 unsigned stride;
37
38 state->format0 = R300_TX_WIDTH((pt->width[0] - 1) & 0x7ff) |
39 R300_TX_HEIGHT((pt->height[0] - 1) & 0x7ff);
40
41 if (!util_is_power_of_two(pt->width[0]) ||
42 !util_is_power_of_two(pt->height[0])) {
43
44 /* rectangles love this */
45 state->format0 |= R300_TX_PITCH_EN;
46
47 stride = r300_texture_get_stride(tex, 0) / pt->block.size;
48 state->format2 = (stride - 1) & 0x1fff;
49 }
50 else {
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);
54 }
55
56 state->format1 = r300_translate_texformat(pt->format);
57 if (pt->target == PIPE_TEXTURE_CUBE) {
58 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
59 }
60 if (pt->target == PIPE_TEXTURE_3D) {
61 state->format1 |= R300_TX_FORMAT_3D;
62 }
63
64 /* large textures on r500 */
65 if (is_r500)
66 {
67 if (pt->width[0] > 2048) {
68 state->format2 |= R500_TXWIDTH_BIT11;
69 }
70 if (pt->height[0] > 2048) {
71 state->format2 |= R500_TXHEIGHT_BIT11;
72 }
73 }
74 assert(is_r500 || (pt->width[0] <= 2048 && pt->height[0] <= 2048));
75
76 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
77 pt->width[0], pt->height[0], pt->last_level);
78 }
79
80 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
81 unsigned zslice, unsigned face)
82 {
83 unsigned offset = tex->offset[level];
84
85 switch (tex->tex.target) {
86 case PIPE_TEXTURE_3D:
87 assert(face == 0);
88 return offset + zslice * tex->layer_size[level];
89
90 case PIPE_TEXTURE_CUBE:
91 assert(zslice == 0);
92 return offset + face * tex->layer_size[level];
93
94 default:
95 assert(zslice == 0 && face == 0);
96 return offset;
97 }
98 }
99
100 /**
101 * Return the stride, in bytes, of the texture images of the given texture
102 * at the given level.
103 */
104 unsigned r300_texture_get_stride(struct r300_texture* tex, unsigned level)
105 {
106 if (tex->stride_override)
107 return tex->stride_override;
108
109 if (level > tex->tex.last_level) {
110 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__,
111 level, tex->tex.last_level);
112 return 0;
113 }
114
115 return align(pf_get_stride(&tex->tex.block, tex->tex.width[level]), 32);
116 }
117
118 static void r300_setup_miptree(struct r300_texture* tex)
119 {
120 struct pipe_texture* base = &tex->tex;
121 int stride, size, layer_size;
122 int i;
123
124 for (i = 0; i <= base->last_level; i++) {
125 if (i > 0) {
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]);
129 }
130
131 base->nblocksx[i] = pf_get_nblocksx(&base->block, base->width[i]);
132 base->nblocksy[i] = pf_get_nblocksy(&base->block, base->height[i]);
133
134 stride = r300_texture_get_stride(tex, i);
135 layer_size = stride * base->nblocksy[i];
136
137 if (base->target == PIPE_TEXTURE_CUBE)
138 size = layer_size * 6;
139 else
140 size = layer_size * base->depth[i];
141
142 tex->offset[i] = align(tex->size, 32);
143 tex->size = tex->offset[i] + size;
144 tex->layer_size[i] = layer_size;
145
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],
149 stride);
150 }
151 }
152
153 /* Create a new texture. */
154 static struct pipe_texture*
155 r300_texture_create(struct pipe_screen* screen,
156 const struct pipe_texture* template)
157 {
158 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
159
160 if (!tex) {
161 return NULL;
162 }
163
164 tex->tex = *template;
165 pipe_reference_init(&tex->tex.reference, 1);
166 tex->tex.screen = screen;
167
168 r300_setup_miptree(tex);
169
170 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500);
171
172 tex->buffer = screen->buffer_create(screen, 1024,
173 PIPE_BUFFER_USAGE_PIXEL,
174 tex->size);
175
176 if (!tex->buffer) {
177 FREE(tex);
178 return NULL;
179 }
180
181 return (struct pipe_texture*)tex;
182 }
183
184 static void r300_texture_destroy(struct pipe_texture* texture)
185 {
186 struct r300_texture* tex = (struct r300_texture*)texture;
187
188 pipe_buffer_reference(&tex->buffer, NULL);
189
190 FREE(tex);
191 }
192
193 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
194 struct pipe_texture* texture,
195 unsigned face,
196 unsigned level,
197 unsigned zslice,
198 unsigned flags)
199 {
200 struct r300_texture* tex = (struct r300_texture*)texture;
201 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
202 unsigned offset;
203
204 offset = r300_texture_get_offset(tex, level, zslice, face);
205
206 if (surface) {
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;
218 }
219
220 return surface;
221 }
222
223 static void r300_tex_surface_destroy(struct pipe_surface* s)
224 {
225 pipe_texture_reference(&s->texture, NULL);
226 FREE(s);
227 }
228
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)
234 {
235 struct r300_texture* tex;
236
237 tex = CALLOC_STRUCT(r300_texture);
238 if (!tex) {
239 return NULL;
240 }
241
242 tex->tex = *base;
243 pipe_reference_init(&tex->tex.reference, 1);
244 tex->tex.screen = screen;
245
246 tex->stride_override = *stride;
247
248 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500);
249
250 pipe_buffer_reference(&tex->buffer, buffer);
251
252 return (struct pipe_texture*)tex;
253 }
254
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)
259 {
260 struct r300_video_surface *r300_vsfc;
261 struct pipe_texture template;
262
263 assert(screen);
264 assert(width && height);
265
266 r300_vsfc = CALLOC_STRUCT(r300_video_surface);
267 if (!r300_vsfc)
268 return NULL;
269
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;
275
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;
286
287 r300_vsfc->tex = screen->texture_create(screen, &template);
288 if (!r300_vsfc->tex)
289 {
290 FREE(r300_vsfc);
291 return NULL;
292 }
293
294 return &r300_vsfc->base;
295 }
296
297 static void r300_video_surface_destroy(struct pipe_video_surface *vsfc)
298 {
299 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc);
300 pipe_texture_reference(&r300_vsfc->tex, NULL);
301 FREE(r300_vsfc);
302 }
303
304 void r300_init_screen_texture_functions(struct pipe_screen* screen)
305 {
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;
311
312 screen->video_surface_create = r300_video_surface_create;
313 screen->video_surface_destroy= r300_video_surface_destroy;
314 }
315
316 boolean r300_get_texture_buffer(struct pipe_texture* texture,
317 struct pipe_buffer** buffer,
318 unsigned* stride)
319 {
320 struct r300_texture* tex = (struct r300_texture*)texture;
321 if (!tex) {
322 return FALSE;
323 }
324
325 pipe_buffer_reference(buffer, tex->buffer);
326
327 if (stride) {
328 *stride = r300_texture_get_stride(tex, 0);
329 }
330
331 return TRUE;
332 }