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