Merge commit 'origin/st-shader-varients'
[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
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 }
48
49 state->format1 = r300_translate_texformat(pt->format);
50 if (pt->target == PIPE_TEXTURE_CUBE) {
51 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
52 }
53 if (pt->target == PIPE_TEXTURE_3D) {
54 state->format1 |= R300_TX_FORMAT_3D;
55 }
56
57 /* large textures on r500 */
58 if (is_r500)
59 {
60 if (pt->width[0] > 2048) {
61 state->format2 |= R500_TXWIDTH_BIT11;
62 }
63 if (pt->height[0] > 2048) {
64 state->format2 |= R500_TXHEIGHT_BIT11;
65 }
66 }
67 assert(is_r500 || (pt->width[0] <= 2048 && pt->height[0] <= 2048));
68
69 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
70 pt->width[0], pt->height[0], pt->last_level);
71 }
72
73 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
74 unsigned zslice, unsigned face)
75 {
76 unsigned offset = tex->offset[level];
77
78 switch (tex->tex.target) {
79 case PIPE_TEXTURE_3D:
80 assert(face == 0);
81 return offset + zslice * tex->layer_size[level];
82
83 case PIPE_TEXTURE_CUBE:
84 assert(zslice == 0);
85 return offset + face * tex->layer_size[level];
86
87 default:
88 assert(zslice == 0 && face == 0);
89 return offset;
90 }
91 }
92
93 /**
94 * Return the stride, in bytes, of the texture images of the given texture
95 * at the given level.
96 */
97 unsigned r300_texture_get_stride(struct r300_texture* tex, unsigned level)
98 {
99 if (tex->stride_override)
100 return tex->stride_override;
101
102 if (level > tex->tex.last_level) {
103 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__,
104 level, tex->tex.last_level);
105 return 0;
106 }
107
108 return align(pf_get_stride(&tex->tex.block, tex->tex.width[level]), 32);
109 }
110
111 static void r300_setup_miptree(struct r300_texture* tex)
112 {
113 struct pipe_texture* base = &tex->tex;
114 int stride, size, layer_size;
115 int i;
116
117 for (i = 0; i <= base->last_level; i++) {
118 if (i > 0) {
119 base->width[i] = minify(base->width[i-1]);
120 base->height[i] = minify(base->height[i-1]);
121 base->depth[i] = minify(base->depth[i-1]);
122 }
123
124 base->nblocksx[i] = pf_get_nblocksx(&base->block, base->width[i]);
125 base->nblocksy[i] = pf_get_nblocksy(&base->block, base->height[i]);
126
127 stride = r300_texture_get_stride(tex, i);
128 layer_size = stride * base->nblocksy[i];
129
130 if (base->target == PIPE_TEXTURE_CUBE)
131 size = layer_size * 6;
132 else
133 size = layer_size * base->depth[i];
134
135 tex->offset[i] = align(tex->size, 32);
136 tex->size = tex->offset[i] + size;
137 tex->layer_size[i] = layer_size;
138 tex->pitch[i] = stride / base->block.size;
139
140 debug_printf("r300: Texture miptree: Level %d "
141 "(%dx%dx%d px, pitch %d bytes)\n",
142 i, base->width[i], base->height[i], base->depth[i],
143 stride);
144 }
145 }
146
147 static void r300_setup_flags(struct r300_texture* tex)
148 {
149 tex->is_npot = !util_is_power_of_two(tex->tex.width[0]) ||
150 !util_is_power_of_two(tex->tex.height[0]);
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_flags(tex);
169 r300_setup_miptree(tex);
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 /* Support only 2D textures without mipmaps */
238 if (base->target != PIPE_TEXTURE_2D ||
239 base->depth[0] != 1 ||
240 base->last_level != 0) {
241 return NULL;
242 }
243
244 tex = CALLOC_STRUCT(r300_texture);
245 if (!tex) {
246 return NULL;
247 }
248
249 tex->tex = *base;
250 pipe_reference_init(&tex->tex.reference, 1);
251 tex->tex.screen = screen;
252
253 tex->stride_override = *stride;
254 tex->pitch[0] = *stride / base->block.size;
255
256 r300_setup_flags(tex);
257 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500);
258
259 pipe_buffer_reference(&tex->buffer, buffer);
260
261 return (struct pipe_texture*)tex;
262 }
263
264 static struct pipe_video_surface *
265 r300_video_surface_create(struct pipe_screen *screen,
266 enum pipe_video_chroma_format chroma_format,
267 unsigned width, unsigned height)
268 {
269 struct r300_video_surface *r300_vsfc;
270 struct pipe_texture template;
271
272 assert(screen);
273 assert(width && height);
274
275 r300_vsfc = CALLOC_STRUCT(r300_video_surface);
276 if (!r300_vsfc)
277 return NULL;
278
279 pipe_reference_init(&r300_vsfc->base.reference, 1);
280 r300_vsfc->base.screen = screen;
281 r300_vsfc->base.chroma_format = chroma_format;
282 r300_vsfc->base.width = width;
283 r300_vsfc->base.height = height;
284
285 memset(&template, 0, sizeof(struct pipe_texture));
286 template.target = PIPE_TEXTURE_2D;
287 template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
288 template.last_level = 0;
289 template.width[0] = util_next_power_of_two(width);
290 template.height[0] = util_next_power_of_two(height);
291 template.depth[0] = 1;
292 pf_get_block(template.format, &template.block);
293 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER |
294 PIPE_TEXTURE_USAGE_RENDER_TARGET;
295
296 r300_vsfc->tex = screen->texture_create(screen, &template);
297 if (!r300_vsfc->tex)
298 {
299 FREE(r300_vsfc);
300 return NULL;
301 }
302
303 return &r300_vsfc->base;
304 }
305
306 static void r300_video_surface_destroy(struct pipe_video_surface *vsfc)
307 {
308 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc);
309 pipe_texture_reference(&r300_vsfc->tex, NULL);
310 FREE(r300_vsfc);
311 }
312
313 void r300_init_screen_texture_functions(struct pipe_screen* screen)
314 {
315 screen->texture_create = r300_texture_create;
316 screen->texture_destroy = r300_texture_destroy;
317 screen->get_tex_surface = r300_get_tex_surface;
318 screen->tex_surface_destroy = r300_tex_surface_destroy;
319 screen->texture_blanket = r300_texture_blanket;
320
321 screen->video_surface_create = r300_video_surface_create;
322 screen->video_surface_destroy= r300_video_surface_destroy;
323 }
324
325 boolean r300_get_texture_buffer(struct pipe_texture* texture,
326 struct pipe_buffer** buffer,
327 unsigned* stride)
328 {
329 struct r300_texture* tex = (struct r300_texture*)texture;
330 if (!tex) {
331 return FALSE;
332 }
333
334 pipe_buffer_reference(buffer, tex->buffer);
335
336 if (stride) {
337 *stride = r300_texture_get_stride(tex, 0);
338 }
339
340 return TRUE;
341 }