Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / src / gallium / drivers / i965simple / brw_tex_layout.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 /* Code to layout images in a mipmap tree for i965.
34 */
35
36 #include "pipe/p_state.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_inlines.h"
40 #include "pipe/internal/p_winsys_screen.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "brw_context.h"
44 #include "brw_tex_layout.h"
45
46
47 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
48
49 #if 0
50 unsigned intel_compressed_alignment(unsigned internalFormat)
51 {
52 unsigned alignment = 4;
53
54 switch (internalFormat) {
55 case GL_COMPRESSED_RGB_FXT1_3DFX:
56 case GL_COMPRESSED_RGBA_FXT1_3DFX:
57 alignment = 8;
58 break;
59
60 default:
61 break;
62 }
63
64 return alignment;
65 }
66 #endif
67
68 static unsigned minify( unsigned d )
69 {
70 return MAX2(1, d>>1);
71 }
72
73
74 static void intel_miptree_set_image_offset(struct brw_texture *tex,
75 unsigned level,
76 unsigned img,
77 unsigned x, unsigned y)
78 {
79 struct pipe_texture *pt = &tex->base;
80 if (img == 0 && level == 0)
81 assert(x == 0 && y == 0);
82 assert(img < tex->nr_images[level]);
83
84 tex->image_offset[level][img] = y * tex->stride + x * pt->block.size;
85 }
86
87 static void intel_miptree_set_level_info(struct brw_texture *tex,
88 unsigned level,
89 unsigned nr_images,
90 unsigned x, unsigned y,
91 unsigned w, unsigned h, unsigned d)
92 {
93 struct pipe_texture *pt = &tex->base;
94
95 assert(level < PIPE_MAX_TEXTURE_LEVELS);
96
97 pt->width[level] = w;
98 pt->height[level] = h;
99 pt->depth[level] = d;
100
101 pt->nblocksx[level] = pf_get_nblocksx(&pt->block, w);
102 pt->nblocksy[level] = pf_get_nblocksy(&pt->block, h);
103
104 tex->level_offset[level] = y * tex->stride + x * tex->base.block.size;
105 tex->nr_images[level] = nr_images;
106
107 /*
108 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
109 level, w, h, d, x, y, tex->level_offset[level]);
110 */
111
112 /* Not sure when this would happen, but anyway:
113 */
114 if (tex->image_offset[level]) {
115 FREE(tex->image_offset[level]);
116 tex->image_offset[level] = NULL;
117 }
118
119 assert(nr_images);
120 assert(!tex->image_offset[level]);
121
122 tex->image_offset[level] = (unsigned *) MALLOC(nr_images * sizeof(unsigned));
123 tex->image_offset[level][0] = 0;
124 }
125
126 static void i945_miptree_layout_2d(struct brw_texture *tex)
127 {
128 struct pipe_texture *pt = &tex->base;
129 const int align_x = 2, align_y = 4;
130 unsigned level;
131 unsigned x = 0;
132 unsigned y = 0;
133 unsigned width = pt->width[0];
134 unsigned height = pt->height[0];
135 unsigned nblocksx = pt->nblocksx[0];
136 unsigned nblocksy = pt->nblocksy[0];
137
138 tex->stride = align(pt->nblocksx[0] * pt->block.size, 4);
139
140 /* May need to adjust pitch to accomodate the placement of
141 * the 2nd mipmap level. This occurs when the alignment
142 * constraints of mipmap placement push the right edge of the
143 * 2nd mipmap level out past the width of its parent.
144 */
145 if (pt->last_level > 0) {
146 unsigned mip1_nblocksx
147 = align(pf_get_nblocksx(&pt->block, minify(width)), align_x)
148 + pf_get_nblocksx(&pt->block, minify(minify(width)));
149
150 if (mip1_nblocksx > nblocksx)
151 tex->stride = mip1_nblocksx * pt->block.size;
152 }
153
154 /* Pitch must be a whole number of dwords
155 */
156 tex->stride = align(tex->stride, 64);
157 tex->total_nblocksy = 0;
158
159 for (level = 0; level <= pt->last_level; level++) {
160 intel_miptree_set_level_info(tex, level, 1, x, y, width,
161 height, 1);
162
163 nblocksy = align(nblocksy, align_y);
164
165 /* Because the images are packed better, the final offset
166 * might not be the maximal one:
167 */
168 tex->total_nblocksy = MAX2(tex->total_nblocksy, y + nblocksy);
169
170 /* Layout_below: step right after second mipmap level.
171 */
172 if (level == 1) {
173 x += align(nblocksx, align_x);
174 }
175 else {
176 y += nblocksy;
177 }
178
179 width = minify(width);
180 height = minify(height);
181 nblocksx = pf_get_nblocksx(&pt->block, width);
182 nblocksy = pf_get_nblocksy(&pt->block, height);
183 }
184 }
185
186 static boolean brw_miptree_layout(struct brw_texture *tex)
187 {
188 struct pipe_texture *pt = &tex->base;
189 /* XXX: these vary depending on image format:
190 */
191 /* int align_w = 4; */
192
193 switch (pt->target) {
194 case PIPE_TEXTURE_CUBE:
195 case PIPE_TEXTURE_3D: {
196 unsigned width = pt->width[0];
197 unsigned height = pt->height[0];
198 unsigned depth = pt->depth[0];
199 unsigned nblocksx = pt->nblocksx[0];
200 unsigned nblocksy = pt->nblocksy[0];
201 unsigned pack_x_pitch, pack_x_nr;
202 unsigned pack_y_pitch;
203 unsigned level;
204 unsigned align_h = 2;
205 unsigned align_w = 4;
206
207 tex->total_nblocksy = 0;
208
209 tex->stride = align(pt->nblocksx[0], 4);
210 pack_y_pitch = align(pt->nblocksy[0], align_h);
211
212 pack_x_pitch = tex->stride / pt->block.size;
213 pack_x_nr = 1;
214
215 for (level = 0; level <= pt->last_level; level++) {
216 unsigned nr_images = pt->target == PIPE_TEXTURE_3D ? depth : 6;
217 int x = 0;
218 int y = 0;
219 uint q, j;
220
221 intel_miptree_set_level_info(tex, level, nr_images,
222 0, tex->total_nblocksy,
223 width, height, depth);
224
225 for (q = 0; q < nr_images;) {
226 for (j = 0; j < pack_x_nr && q < nr_images; j++, q++) {
227 intel_miptree_set_image_offset(tex, level, q, x, y);
228 x += pack_x_pitch;
229 }
230
231 x = 0;
232 y += pack_y_pitch;
233 }
234
235
236 tex->total_nblocksy += y;
237 width = minify(width);
238 height = minify(height);
239 depth = minify(depth);
240 nblocksx = pf_get_nblocksx(&pt->block, width);
241 nblocksy = pf_get_nblocksy(&pt->block, height);
242
243 if (pt->compressed) {
244 pack_y_pitch = (height + 3) / 4;
245
246 if (pack_x_pitch > align(width, align_w)) {
247 pack_x_pitch = align(width, align_w);
248 pack_x_nr <<= 1;
249 }
250 } else {
251 if (pack_x_pitch > 4) {
252 pack_x_pitch >>= 1;
253 pack_x_nr <<= 1;
254 assert(pack_x_pitch * pack_x_nr * pt->block.size <= tex->stride);
255 }
256
257 if (pack_y_pitch > 2) {
258 pack_y_pitch >>= 1;
259 pack_y_pitch = align(pack_y_pitch, align_h);
260 }
261 }
262
263 }
264 break;
265 }
266
267 default:
268 i945_miptree_layout_2d(tex);
269 break;
270 }
271 #if 0
272 PRINT("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
273 pt->pitch,
274 pt->total_nblocksy,
275 pt->block.size,
276 pt->stride * pt->total_nblocksy );
277 #endif
278
279 return TRUE;
280 }
281
282
283 static struct pipe_texture *
284 brw_texture_create_screen(struct pipe_screen *screen,
285 const struct pipe_texture *templat)
286 {
287 struct brw_texture *tex = CALLOC_STRUCT(brw_texture);
288
289 if (tex) {
290 tex->base = *templat;
291 pipe_reference_init(&tex->base.reference, 1);
292
293 tex->base.nblocksx[0] = pf_get_nblocksx(&tex->base.block, tex->base.width[0]);
294 tex->base.nblocksy[0] = pf_get_nblocksy(&tex->base.block, tex->base.height[0]);
295
296 if (brw_miptree_layout(tex))
297 tex->buffer = screen->buffer_create(screen, 64,
298 PIPE_BUFFER_USAGE_PIXEL,
299 tex->stride *
300 tex->total_nblocksy);
301
302 if (!tex->buffer) {
303 FREE(tex);
304 return NULL;
305 }
306 }
307
308 return &tex->base;
309 }
310
311
312 static void
313 brw_texture_destroy_screen(struct pipe_texture *pt)
314 {
315 struct brw_texture *tex = (struct brw_texture *)pt;
316 uint i;
317
318 /*
319 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
320 */
321
322 pipe_buffer_reference(&tex->buffer, NULL);
323
324 for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++)
325 if (tex->image_offset[i])
326 free(tex->image_offset[i]);
327
328 free(tex);
329 }
330
331
332 static struct pipe_surface *
333 brw_get_tex_surface_screen(struct pipe_screen *screen,
334 struct pipe_texture *pt,
335 unsigned face, unsigned level, unsigned zslice)
336 {
337 struct brw_texture *tex = (struct brw_texture *)pt;
338 struct pipe_surface *ps;
339 unsigned offset; /* in bytes */
340
341 offset = tex->level_offset[level];
342
343 if (pt->target == PIPE_TEXTURE_CUBE) {
344 offset += tex->image_offset[level][face];
345 }
346 else if (pt->target == PIPE_TEXTURE_3D) {
347 offset += tex->image_offset[level][zslice];
348 }
349 else {
350 assert(face == 0);
351 assert(zslice == 0);
352 }
353
354 ps = CALLOC_STRUCT(pipe_surface);
355 if (ps) {
356 pipe_reference_init(&ps->reference, 1);
357 pipe_texture_reference(&ps->texture, pt);
358 ps->format = pt->format;
359 ps->width = pt->width[level];
360 ps->height = pt->height[level];
361 ps->block = pt->block;
362 ps->nblocksx = pt->nblocksx[level];
363 ps->nblocksy = pt->nblocksy[level];
364 ps->stride = tex->stride;
365 ps->offset = offset;
366 ps->status = PIPE_SURFACE_STATUS_DEFINED;
367 }
368 return ps;
369 }
370
371
372 void
373 brw_init_texture_functions(struct brw_context *brw)
374 {
375 // brw->pipe.texture_update = brw_texture_update;
376 }
377
378
379 void
380 brw_init_screen_texture_funcs(struct pipe_screen *screen)
381 {
382 screen->texture_create = brw_texture_create_screen;
383 screen->texture_destroy = brw_texture_destroy_screen;
384 screen->get_tex_surface = brw_get_tex_surface_screen;
385 }
386