Merge remote branch 'origin/master' into radeon-rewrite
[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 /* XXX struct r300_screen* r300screen = r300_screen(screen); */
90
91 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
92
93 if (!tex) {
94 return NULL;
95 }
96
97 tex->tex = *template;
98 pipe_reference_init(&tex->tex.reference, 1);
99 tex->tex.screen = screen;
100
101 r300_setup_miptree(tex);
102
103 /* XXX */
104 r300_setup_texture_state(tex, tex->tex.width[0], tex->tex.height[0],
105 tex->tex.width[0]);
106
107 tex->buffer = screen->buffer_create(screen, 64,
108 PIPE_BUFFER_USAGE_PIXEL,
109 tex->size);
110
111 if (!tex->buffer) {
112 FREE(tex);
113 return NULL;
114 }
115
116 return (struct pipe_texture*)tex;
117 }
118
119 static void r300_texture_destroy(struct pipe_texture* texture)
120 {
121 struct r300_texture* tex = (struct r300_texture*)texture;
122
123 pipe_buffer_reference(&tex->buffer, NULL);
124
125 FREE(tex);
126 }
127
128 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
129 struct pipe_texture* texture,
130 unsigned face,
131 unsigned level,
132 unsigned zslice,
133 unsigned flags)
134 {
135 struct r300_texture* tex = (struct r300_texture*)texture;
136 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
137 unsigned offset;
138
139 /* XXX this is certainly dependent on tex target */
140 offset = tex->offset[level];
141
142 if (surface) {
143 pipe_reference_init(&surface->reference, 1);
144 pipe_texture_reference(&surface->texture, texture);
145 surface->format = texture->format;
146 surface->width = texture->width[level];
147 surface->height = texture->height[level];
148 surface->offset = offset;
149 surface->usage = flags;
150 }
151
152 return surface;
153 }
154
155 static void r300_tex_surface_destroy(struct pipe_surface* s)
156 {
157 pipe_texture_reference(&s->texture, NULL);
158 FREE(s);
159 }
160
161 static struct pipe_texture*
162 r300_texture_blanket(struct pipe_screen* screen,
163 const struct pipe_texture* base,
164 const unsigned* stride,
165 struct pipe_buffer* buffer)
166 {
167 struct r300_texture* tex;
168
169 if (base->target != PIPE_TEXTURE_2D ||
170 base->last_level != 0 ||
171 base->depth[0] != 1) {
172 return NULL;
173 }
174
175 tex = CALLOC_STRUCT(r300_texture);
176 if (!tex) {
177 return NULL;
178 }
179
180 tex->tex = *base;
181 pipe_reference_init(&tex->tex.reference, 1);
182 tex->tex.screen = screen;
183
184 tex->stride = *stride;
185
186 r300_setup_texture_state(tex, tex->tex.width[0], tex->tex.height[0],
187 tex->stride);
188
189 pipe_buffer_reference(&tex->buffer, buffer);
190
191 return (struct pipe_texture*)tex;
192 }
193
194 void r300_init_screen_texture_functions(struct pipe_screen* screen)
195 {
196 screen->texture_create = r300_texture_create;
197 screen->texture_destroy = r300_texture_destroy;
198 screen->get_tex_surface = r300_get_tex_surface;
199 screen->tex_surface_destroy = r300_tex_surface_destroy;
200 screen->texture_blanket = r300_texture_blanket;
201 }
202
203 boolean r300_get_texture_buffer(struct pipe_texture* texture,
204 struct pipe_buffer** buffer,
205 unsigned* stride)
206 {
207 struct r300_texture* tex = (struct r300_texture*)texture;
208 if (!tex) {
209 return FALSE;
210 }
211
212 pipe_buffer_reference(buffer, tex->buffer);
213
214 if (stride) {
215 *stride = tex->stride;
216 }
217
218 return TRUE;
219 }