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