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