r300: Zero-initialize register for NV_vertex_program
[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 void r300_setup_texture_state(struct r300_texture* tex,
26 unsigned width,
27 unsigned height,
28 unsigned pitch,
29 unsigned levels)
30 {
31 struct r300_texture_state* state = &tex->state;
32
33 state->format0 = R300_TX_WIDTH((width - 1) & 0x7ff) |
34 R300_TX_HEIGHT((height - 1) & 0x7ff) |
35 R300_TX_NUM_LEVELS(levels) |
36 R300_TX_PITCH_EN;
37
38 /* XXX */
39 state->format1 = r300_translate_texformat(tex->tex.format);
40
41 state->format2 = pitch - 1;
42
43 /* Assume (somewhat foolishly) that oversized textures will
44 * not be permitted by the state tracker. */
45 if (width > 2048) {
46 state->format2 |= R500_TXWIDTH_BIT11;
47 }
48 if (height > 2048) {
49 state->format2 |= R500_TXHEIGHT_BIT11;
50 }
51
52 debug_printf("r300: Set texture state (%dx%d, pitch %d, %d levels)\n",
53 width, height, pitch, levels);
54 }
55
56 static void r300_setup_miptree(struct r300_texture* tex)
57 {
58 struct pipe_texture* base = &tex->tex;
59 int stride, size, offset;
60 int i;
61
62 for (i = 0; i <= base->last_level; i++) {
63 if (i > 0) {
64 base->width[i] = minify(base->width[i-1]);
65 base->height[i] = minify(base->height[i-1]);
66 base->depth[i] = minify(base->depth[i-1]);
67 }
68
69 base->nblocksx[i] = pf_get_nblocksx(&base->block, base->width[i]);
70 base->nblocksy[i] = pf_get_nblocksy(&base->block, base->height[i]);
71
72 /* Radeons enjoy things in multiples of 64.
73 *
74 * XXX
75 * POT, uncompressed, unmippmapped textures can be aligned to 32,
76 * instead of 64. */
77 stride = align(pf_get_stride(&base->block, base->width[i]), 32);
78 size = stride * base->nblocksy[i] * base->depth[i];
79
80 tex->offset[i] = align(tex->size, 32);
81 tex->size = tex->offset[i] + size;
82
83 debug_printf("r300: Texture miptree: Level %d "
84 "(%dx%dx%d px, pitch %d bytes)\n",
85 i, base->width[i], base->height[i], base->depth[i],
86 stride);
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, 1024,
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 }