r300g: Fix two trivial texture size issues.
[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 debug_printf("r300: Set texture state (%dx%d, pitch %d, %d levels)\n",
60 width, height, pitch, levels);
61 }
62
63 static void r300_setup_miptree(struct r300_texture* tex)
64 {
65 struct pipe_texture* base = &tex->tex;
66 int stride, size, offset;
67 int i;
68
69 for (i = 0; i <= base->last_level; i++) {
70 if (i > 0) {
71 base->width[i] = minify(base->width[i-1]);
72 base->height[i] = minify(base->height[i-1]);
73 base->depth[i] = minify(base->depth[i-1]);
74 }
75
76 base->nblocksx[i] = pf_get_nblocksx(&base->block, base->width[i]);
77 base->nblocksy[i] = pf_get_nblocksy(&base->block, base->height[i]);
78
79 /* Radeons enjoy things in multiples of 64.
80 *
81 * XXX
82 * POT, uncompressed, unmippmapped textures can be aligned to 32,
83 * instead of 64. */
84 stride = align(
85 (base->nblocksx[i] * base->block.size) / base->block.width,
86 32);
87 size = stride * base->nblocksy[i] * base->depth[i];
88
89 tex->offset[i] = align(tex->size, 32);
90 tex->size += tex->offset[i] + size;
91
92 debug_printf("r300: Texture miptree: Level %d "
93 "(%dx%dx%d px, pitch %d bytes)\n",
94 i, base->width[i], base->height[i], base->depth[i],
95 stride);
96 /* Save stride of first level to the texture. */
97 if (i == 0) {
98 tex->stride = stride;
99 }
100 }
101 }
102
103 /* Create a new texture. */
104 static struct pipe_texture*
105 r300_texture_create(struct pipe_screen* screen,
106 const struct pipe_texture* template)
107 {
108 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
109
110 if (!tex) {
111 return NULL;
112 }
113
114 tex->tex = *template;
115 pipe_reference_init(&tex->tex.reference, 1);
116 tex->tex.screen = screen;
117
118 r300_setup_miptree(tex);
119
120 r300_setup_texture_state(tex, template->width[0], template->height[0],
121 template->width[0], template->last_level);
122
123 tex->buffer = screen->buffer_create(screen, 1024,
124 PIPE_BUFFER_USAGE_PIXEL,
125 tex->size);
126
127 if (!tex->buffer) {
128 FREE(tex);
129 return NULL;
130 }
131
132 return (struct pipe_texture*)tex;
133 }
134
135 static void r300_texture_destroy(struct pipe_texture* texture)
136 {
137 struct r300_texture* tex = (struct r300_texture*)texture;
138
139 pipe_buffer_reference(&tex->buffer, NULL);
140
141 FREE(tex);
142 }
143
144 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
145 struct pipe_texture* texture,
146 unsigned face,
147 unsigned level,
148 unsigned zslice,
149 unsigned flags)
150 {
151 struct r300_texture* tex = (struct r300_texture*)texture;
152 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
153 unsigned offset;
154
155 /* XXX this is certainly dependent on tex target */
156 offset = tex->offset[level];
157
158 if (surface) {
159 pipe_reference_init(&surface->reference, 1);
160 pipe_texture_reference(&surface->texture, texture);
161 surface->format = texture->format;
162 surface->width = texture->width[level];
163 surface->height = texture->height[level];
164 surface->offset = offset;
165 surface->usage = flags;
166 }
167
168 return surface;
169 }
170
171 static void r300_tex_surface_destroy(struct pipe_surface* s)
172 {
173 pipe_texture_reference(&s->texture, NULL);
174 FREE(s);
175 }
176
177 static struct pipe_texture*
178 r300_texture_blanket(struct pipe_screen* screen,
179 const struct pipe_texture* base,
180 const unsigned* stride,
181 struct pipe_buffer* buffer)
182 {
183 struct r300_texture* tex;
184
185 /* XXX we should start doing mips now... */
186 if (base->target != PIPE_TEXTURE_2D ||
187 base->last_level != 0 ||
188 base->depth[0] != 1) {
189 return NULL;
190 }
191
192 tex = CALLOC_STRUCT(r300_texture);
193 if (!tex) {
194 return NULL;
195 }
196
197 tex->tex = *base;
198 pipe_reference_init(&tex->tex.reference, 1);
199 tex->tex.screen = screen;
200
201 tex->stride = *stride;
202
203 /* XXX */
204 r300_setup_texture_state(tex, tex->tex.width[0], tex->tex.height[0],
205 tex->stride, 0);
206
207 pipe_buffer_reference(&tex->buffer, buffer);
208
209 return (struct pipe_texture*)tex;
210 }
211
212 void r300_init_screen_texture_functions(struct pipe_screen* screen)
213 {
214 screen->texture_create = r300_texture_create;
215 screen->texture_destroy = r300_texture_destroy;
216 screen->get_tex_surface = r300_get_tex_surface;
217 screen->tex_surface_destroy = r300_tex_surface_destroy;
218 screen->texture_blanket = r300_texture_blanket;
219 }
220
221 boolean r300_get_texture_buffer(struct pipe_texture* texture,
222 struct pipe_buffer** buffer,
223 unsigned* stride)
224 {
225 struct r300_texture* tex = (struct r300_texture*)texture;
226 if (!tex) {
227 return FALSE;
228 }
229
230 pipe_buffer_reference(buffer, tex->buffer);
231
232 if (stride) {
233 *stride = tex->stride;
234 }
235
236 return TRUE;
237 }