r300g: Always emit scissors.
[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_format.h"
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28
29 #include "r300_context.h"
30 #include "r300_texture.h"
31 #include "r300_screen.h"
32
33 static void r300_setup_texture_state(struct r300_texture* tex, boolean is_r500)
34 {
35 struct r300_texture_state* state = &tex->state;
36 struct pipe_texture *pt = &tex->tex;
37
38 state->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
39 R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
40
41 if (tex->is_npot) {
42 /* rectangles love this */
43 state->format0 |= R300_TX_PITCH_EN;
44 state->format2 = (tex->pitch[0] - 1) & 0x1fff;
45 } else {
46 /* power of two textures (3D, mipmaps, and no pitch) */
47 state->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
48 }
49
50 state->format1 = r300_translate_texformat(pt->format);
51 if (pt->target == PIPE_TEXTURE_CUBE) {
52 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
53 }
54 if (pt->target == PIPE_TEXTURE_3D) {
55 state->format1 |= R300_TX_FORMAT_3D;
56 }
57
58 /* large textures on r500 */
59 if (is_r500)
60 {
61 if (pt->width0 > 2048) {
62 state->format2 |= R500_TXWIDTH_BIT11;
63 }
64 if (pt->height0 > 2048) {
65 state->format2 |= R500_TXHEIGHT_BIT11;
66 }
67 }
68 assert(is_r500 || (pt->width0 <= 2048 && pt->height0 <= 2048));
69
70 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
71 pt->width0, pt->height0, pt->last_level);
72 }
73
74 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
75 unsigned zslice, unsigned face)
76 {
77 unsigned offset = tex->offset[level];
78
79 switch (tex->tex.target) {
80 case PIPE_TEXTURE_3D:
81 assert(face == 0);
82 return offset + zslice * tex->layer_size[level];
83
84 case PIPE_TEXTURE_CUBE:
85 assert(zslice == 0);
86 return offset + face * tex->layer_size[level];
87
88 default:
89 assert(zslice == 0 && face == 0);
90 return offset;
91 }
92 }
93
94 /**
95 * Return the stride, in bytes, of the texture images of the given texture
96 * at the given level.
97 */
98 unsigned r300_texture_get_stride(struct r300_texture* tex, unsigned level)
99 {
100 if (tex->stride_override)
101 return tex->stride_override;
102
103 if (level > tex->tex.last_level) {
104 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__,
105 level, tex->tex.last_level);
106 return 0;
107 }
108
109 return align(util_format_get_stride(tex->tex.format, u_minify(tex->tex.width0, level)), 32);
110 }
111
112 static void r300_setup_miptree(struct r300_texture* tex)
113 {
114 struct pipe_texture* base = &tex->tex;
115 int stride, size, layer_size;
116 int i;
117
118 debug_printf("r300: Making miptree for texture, format %s\n", pf_name(base->format));
119
120 for (i = 0; i <= base->last_level; i++) {
121 unsigned nblocksy = util_format_get_nblocksy(base->format, u_minify(base->height0, i));
122
123 stride = r300_texture_get_stride(tex, i);
124 layer_size = stride * nblocksy;
125
126 if (base->target == PIPE_TEXTURE_CUBE)
127 size = layer_size * 6;
128 else
129 size = layer_size * u_minify(base->depth0, i);
130
131 tex->offset[i] = align(tex->size, 32);
132 tex->size = tex->offset[i] + size;
133 tex->layer_size[i] = layer_size;
134 tex->pitch[i] = stride / util_format_get_blocksize(base->format);
135
136 debug_printf("r300: Texture miptree: Level %d "
137 "(%dx%dx%d px, pitch %d bytes) %d bytes total\n",
138 i, u_minify(base->width0, i), u_minify(base->height0, i),
139 u_minify(base->depth0, i), stride, tex->size);
140 }
141 }
142
143 static void r300_setup_flags(struct r300_texture* tex)
144 {
145 tex->is_npot = !util_is_power_of_two(tex->tex.width0) ||
146 !util_is_power_of_two(tex->tex.height0);
147 }
148
149 /* Create a new texture. */
150 static struct pipe_texture*
151 r300_texture_create(struct pipe_screen* screen,
152 const struct pipe_texture* template)
153 {
154 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
155
156 if (!tex) {
157 return NULL;
158 }
159
160 tex->tex = *template;
161 pipe_reference_init(&tex->tex.reference, 1);
162 tex->tex.screen = screen;
163
164 r300_setup_flags(tex);
165 r300_setup_miptree(tex);
166 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500);
167
168 tex->buffer = screen->buffer_create(screen, 1024,
169 PIPE_BUFFER_USAGE_PIXEL,
170 tex->size);
171
172 if (!tex->buffer) {
173 FREE(tex);
174 return NULL;
175 }
176
177 return (struct pipe_texture*)tex;
178 }
179
180 static void r300_texture_destroy(struct pipe_texture* texture)
181 {
182 struct r300_texture* tex = (struct r300_texture*)texture;
183
184 pipe_buffer_reference(&tex->buffer, NULL);
185
186 FREE(tex);
187 }
188
189 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
190 struct pipe_texture* texture,
191 unsigned face,
192 unsigned level,
193 unsigned zslice,
194 unsigned flags)
195 {
196 struct r300_texture* tex = (struct r300_texture*)texture;
197 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
198 unsigned offset;
199
200 offset = r300_texture_get_offset(tex, level, zslice, face);
201
202 if (surface) {
203 pipe_reference_init(&surface->reference, 1);
204 pipe_texture_reference(&surface->texture, texture);
205 surface->format = texture->format;
206 surface->width = u_minify(texture->width0, level);
207 surface->height = u_minify(texture->height0, level);
208 surface->offset = offset;
209 surface->usage = flags;
210 surface->zslice = zslice;
211 surface->texture = texture;
212 surface->face = face;
213 surface->level = level;
214 }
215
216 return surface;
217 }
218
219 static void r300_tex_surface_destroy(struct pipe_surface* s)
220 {
221 pipe_texture_reference(&s->texture, NULL);
222 FREE(s);
223 }
224
225 static struct pipe_texture*
226 r300_texture_blanket(struct pipe_screen* screen,
227 const struct pipe_texture* base,
228 const unsigned* stride,
229 struct pipe_buffer* buffer)
230 {
231 struct r300_texture* tex;
232
233 /* Support only 2D textures without mipmaps */
234 if (base->target != PIPE_TEXTURE_2D ||
235 base->depth0 != 1 ||
236 base->last_level != 0) {
237 return NULL;
238 }
239
240 tex = CALLOC_STRUCT(r300_texture);
241 if (!tex) {
242 return NULL;
243 }
244
245 tex->tex = *base;
246 pipe_reference_init(&tex->tex.reference, 1);
247 tex->tex.screen = screen;
248
249 tex->stride_override = *stride;
250 tex->pitch[0] = *stride / util_format_get_blocksize(base->format);
251
252 r300_setup_flags(tex);
253 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500);
254
255 pipe_buffer_reference(&tex->buffer, buffer);
256
257 return (struct pipe_texture*)tex;
258 }
259
260 static struct pipe_video_surface *
261 r300_video_surface_create(struct pipe_screen *screen,
262 enum pipe_video_chroma_format chroma_format,
263 unsigned width, unsigned height)
264 {
265 struct r300_video_surface *r300_vsfc;
266 struct pipe_texture template;
267
268 assert(screen);
269 assert(width && height);
270
271 r300_vsfc = CALLOC_STRUCT(r300_video_surface);
272 if (!r300_vsfc)
273 return NULL;
274
275 pipe_reference_init(&r300_vsfc->base.reference, 1);
276 r300_vsfc->base.screen = screen;
277 r300_vsfc->base.chroma_format = chroma_format;
278 r300_vsfc->base.width = width;
279 r300_vsfc->base.height = height;
280
281 memset(&template, 0, sizeof(struct pipe_texture));
282 template.target = PIPE_TEXTURE_2D;
283 template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
284 template.last_level = 0;
285 template.width0 = util_next_power_of_two(width);
286 template.height0 = util_next_power_of_two(height);
287 template.depth0 = 1;
288 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER |
289 PIPE_TEXTURE_USAGE_RENDER_TARGET;
290
291 r300_vsfc->tex = screen->texture_create(screen, &template);
292 if (!r300_vsfc->tex)
293 {
294 FREE(r300_vsfc);
295 return NULL;
296 }
297
298 return &r300_vsfc->base;
299 }
300
301 static void r300_video_surface_destroy(struct pipe_video_surface *vsfc)
302 {
303 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc);
304 pipe_texture_reference(&r300_vsfc->tex, NULL);
305 FREE(r300_vsfc);
306 }
307
308 void r300_init_screen_texture_functions(struct pipe_screen* screen)
309 {
310 screen->texture_create = r300_texture_create;
311 screen->texture_destroy = r300_texture_destroy;
312 screen->get_tex_surface = r300_get_tex_surface;
313 screen->tex_surface_destroy = r300_tex_surface_destroy;
314 screen->texture_blanket = r300_texture_blanket;
315
316 screen->video_surface_create = r300_video_surface_create;
317 screen->video_surface_destroy= r300_video_surface_destroy;
318 }
319
320 boolean r300_get_texture_buffer(struct pipe_texture* texture,
321 struct pipe_buffer** buffer,
322 unsigned* stride)
323 {
324 struct r300_texture* tex = (struct r300_texture*)texture;
325 if (!tex) {
326 return FALSE;
327 }
328
329 pipe_buffer_reference(buffer, tex->buffer);
330
331 if (stride) {
332 *stride = r300_texture_get_stride(tex, 0);
333 }
334
335 return TRUE;
336 }