Merge branch 'mesa_7_6_branch'
[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 "r300_texture.h"
24
25 static void r300_setup_texture_state(struct r300_texture* tex)
26 {
27 struct r300_texture_state* state = &tex->state;
28 struct pipe_texture *pt = &tex->tex;
29
30 state->format0 = R300_TX_WIDTH((pt->width[0] - 1) & 0x7ff) |
31 R300_TX_HEIGHT((pt->height[0] - 1) & 0x7ff) |
32 R300_TX_DEPTH(util_logbase2(pt->depth[0]) & 0xf) |
33 R300_TX_NUM_LEVELS(pt->last_level) |
34 R300_TX_PITCH_EN;
35
36 /* XXX */
37 state->format1 = r300_translate_texformat(pt->format);
38 if (pt->target == PIPE_TEXTURE_CUBE) {
39 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
40 }
41 if (pt->target == PIPE_TEXTURE_3D) {
42 state->format1 |= R300_TX_FORMAT_3D;
43 }
44
45 state->format2 = (r300_texture_get_stride(tex, 0) / pt->block.size) - 1;
46
47 /* Assume (somewhat foolishly) that oversized textures will
48 * not be permitted by the state tracker. */
49 if (pt->width[0] > 2048) {
50 state->format2 |= R500_TXWIDTH_BIT11;
51 }
52 if (pt->height[0] > 2048) {
53 state->format2 |= R500_TXHEIGHT_BIT11;
54 }
55
56 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
57 pt->width[0], pt->height[0], pt->last_level);
58 }
59
60 /**
61 * Return the stride, in bytes, of the texture images of the given texture
62 * at the given level.
63 */
64 unsigned r300_texture_get_stride(struct r300_texture* tex, unsigned level)
65 {
66 if (tex->stride_override)
67 return tex->stride_override;
68
69 if (level > tex->tex.last_level) {
70 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__, level, tex->tex.last_level);
71 return 0;
72 }
73
74 return align(pf_get_stride(&tex->tex.block, tex->tex.width[level]), 32);
75 }
76
77 static void r300_setup_miptree(struct r300_texture* tex)
78 {
79 struct pipe_texture* base = &tex->tex;
80 int stride, size;
81 int i;
82
83 for (i = 0; i <= base->last_level; i++) {
84 if (i > 0) {
85 base->width[i] = minify(base->width[i-1]);
86 base->height[i] = minify(base->height[i-1]);
87 base->depth[i] = minify(base->depth[i-1]);
88 }
89
90 base->nblocksx[i] = pf_get_nblocksx(&base->block, base->width[i]);
91 base->nblocksy[i] = pf_get_nblocksy(&base->block, base->height[i]);
92
93 /* Radeons enjoy things in multiples of 64.
94 *
95 * XXX
96 * POT, uncompressed, unmippmapped textures can be aligned to 32,
97 * instead of 64. */
98 stride = r300_texture_get_stride(tex, i);
99 size = stride * base->nblocksy[i] * base->depth[i];
100
101 tex->offset[i] = align(tex->size, 32);
102 tex->size = tex->offset[i] + size;
103
104 debug_printf("r300: Texture miptree: Level %d "
105 "(%dx%dx%d px, pitch %d bytes)\n",
106 i, base->width[i], base->height[i], base->depth[i],
107 stride);
108 }
109 }
110
111 /* Create a new texture. */
112 static struct pipe_texture*
113 r300_texture_create(struct pipe_screen* screen,
114 const struct pipe_texture* template)
115 {
116 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
117
118 if (!tex) {
119 return NULL;
120 }
121
122 tex->tex = *template;
123 pipe_reference_init(&tex->tex.reference, 1);
124 tex->tex.screen = screen;
125
126 r300_setup_miptree(tex);
127
128 r300_setup_texture_state(tex);
129
130 tex->buffer = screen->buffer_create(screen, 1024,
131 PIPE_BUFFER_USAGE_PIXEL,
132 tex->size);
133
134 if (!tex->buffer) {
135 FREE(tex);
136 return NULL;
137 }
138
139 return (struct pipe_texture*)tex;
140 }
141
142 static void r300_texture_destroy(struct pipe_texture* texture)
143 {
144 struct r300_texture* tex = (struct r300_texture*)texture;
145
146 pipe_buffer_reference(&tex->buffer, NULL);
147
148 FREE(tex);
149 }
150
151 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
152 struct pipe_texture* texture,
153 unsigned face,
154 unsigned level,
155 unsigned zslice,
156 unsigned flags)
157 {
158 struct r300_texture* tex = (struct r300_texture*)texture;
159 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
160 unsigned offset;
161
162 /* XXX this is certainly dependent on tex target */
163 offset = tex->offset[level];
164
165 if (surface) {
166 pipe_reference_init(&surface->reference, 1);
167 pipe_texture_reference(&surface->texture, texture);
168 surface->format = texture->format;
169 surface->width = texture->width[level];
170 surface->height = texture->height[level];
171 surface->offset = offset;
172 surface->usage = flags;
173 }
174
175 return surface;
176 }
177
178 static void r300_tex_surface_destroy(struct pipe_surface* s)
179 {
180 pipe_texture_reference(&s->texture, NULL);
181 FREE(s);
182 }
183
184 static struct pipe_texture*
185 r300_texture_blanket(struct pipe_screen* screen,
186 const struct pipe_texture* base,
187 const unsigned* stride,
188 struct pipe_buffer* buffer)
189 {
190 struct r300_texture* tex;
191
192 /* XXX we should start doing mips now... */
193 if (base->target != PIPE_TEXTURE_2D ||
194 base->last_level != 0 ||
195 base->depth[0] != 1) {
196 return NULL;
197 }
198
199 tex = CALLOC_STRUCT(r300_texture);
200 if (!tex) {
201 return NULL;
202 }
203
204 tex->tex = *base;
205 pipe_reference_init(&tex->tex.reference, 1);
206 tex->tex.screen = screen;
207
208 tex->stride_override = *stride;
209
210 /* XXX */
211 r300_setup_texture_state(tex);
212
213 pipe_buffer_reference(&tex->buffer, buffer);
214
215 return (struct pipe_texture*)tex;
216 }
217
218 void r300_init_screen_texture_functions(struct pipe_screen* screen)
219 {
220 screen->texture_create = r300_texture_create;
221 screen->texture_destroy = r300_texture_destroy;
222 screen->get_tex_surface = r300_get_tex_surface;
223 screen->tex_surface_destroy = r300_tex_surface_destroy;
224 screen->texture_blanket = r300_texture_blanket;
225 }
226
227 boolean r300_get_texture_buffer(struct pipe_texture* texture,
228 struct pipe_buffer** buffer,
229 unsigned* stride)
230 {
231 struct r300_texture* tex = (struct r300_texture*)texture;
232 if (!tex) {
233 return FALSE;
234 }
235
236 pipe_buffer_reference(buffer, tex->buffer);
237
238 if (stride) {
239 *stride = r300_texture_get_stride(tex, 0);
240 }
241
242 return TRUE;
243 }