slang: initialize the context
[mesa.git] / src / mesa / drivers / dri / r300 / r300_texmem.c
1 /**************************************************************************
2
3 Copyright (C) Tungsten Graphics 2002. All Rights Reserved.
4 The Weather Channel, Inc. funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86
6 license. This notice must be preserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation on the rights to use, copy, modify, merge, publish,
12 distribute, sub license, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NON-INFRINGEMENT. IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR THEIR
24 SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
26 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28
29 **************************************************************************/
30
31 /**
32 * \file
33 *
34 * \author Gareth Hughes <gareth@valinux.com>
35 *
36 * \author Kevin E. Martin <martin@valinux.com>
37 */
38
39 #include <errno.h>
40
41 #include "main/glheader.h"
42 #include "main/imports.h"
43 #include "main/context.h"
44 #include "main/colormac.h"
45 #include "main/macros.h"
46 #include "main/simple_list.h"
47 #include "radeon_reg.h" /* gets definition for usleep */
48 #include "r300_context.h"
49 #include "r300_state.h"
50 #include "r300_cmdbuf.h"
51 #include "radeon_ioctl.h"
52 #include "r300_tex.h"
53 #include "r300_ioctl.h"
54 #include <unistd.h> /* for usleep() */
55
56 #ifdef USER_BUFFERS
57 #include "r300_mem.h"
58 #endif
59
60 /**
61 * Destroy any device-dependent state associated with the texture. This may
62 * include NULLing out hardware state that points to the texture.
63 */
64 void r300DestroyTexObj(r300ContextPtr rmesa, r300TexObjPtr t)
65 {
66 int i;
67
68 if (RADEON_DEBUG & DEBUG_TEXTURE) {
69 fprintf(stderr, "%s( %p, %p )\n", __FUNCTION__,
70 (void *)t, (void *)t->base.tObj);
71 }
72
73 for (i = 0; i < rmesa->radeon.glCtx->Const.MaxTextureUnits; i++) {
74 if (rmesa->state.texture.unit[i].texobj == t) {
75 rmesa->state.texture.unit[i].texobj = NULL;
76 }
77 }
78 }
79
80 /* ------------------------------------------------------------
81 * Texture image conversions
82 */
83
84 static void r300UploadGARTClientSubImage(r300ContextPtr rmesa,
85 r300TexObjPtr t,
86 struct gl_texture_image *texImage,
87 GLint hwlevel,
88 GLint x, GLint y,
89 GLint width, GLint height)
90 {
91 const struct gl_texture_format *texFormat = texImage->TexFormat;
92 GLuint srcPitch, dstPitch;
93 int blit_format;
94 int srcOffset;
95
96 /*
97 * XXX it appears that we always upload the full image, not a subimage.
98 * I.e. x==0, y==0, width=texWidth, height=texWidth. If this is ever
99 * changed, the src pitch will have to change.
100 */
101 switch (texFormat->TexelBytes) {
102 case 1:
103 blit_format = R300_CP_COLOR_FORMAT_CI8;
104 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
105 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
106 break;
107 case 2:
108 blit_format = R300_CP_COLOR_FORMAT_RGB565;
109 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
110 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
111 break;
112 case 4:
113 blit_format = R300_CP_COLOR_FORMAT_ARGB8888;
114 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
115 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
116 break;
117 case 8:
118 case 16:
119 blit_format = R300_CP_COLOR_FORMAT_CI8;
120 srcPitch = t->image[0][0].width * texFormat->TexelBytes;
121 dstPitch = t->image[0][0].width * texFormat->TexelBytes;
122 break;
123 default:
124 return;
125 }
126
127 t->image[0][hwlevel].data = texImage->Data;
128 srcOffset = r300GartOffsetFromVirtual(rmesa, texImage->Data);
129
130 assert(srcOffset != ~0);
131
132 /* Don't currently need to cope with small pitches?
133 */
134 width = texImage->Width;
135 height = texImage->Height;
136
137 if (texFormat->TexelBytes > 4) {
138 width *= texFormat->TexelBytes;
139 }
140
141 r300EmitWait(rmesa, R300_WAIT_3D);
142
143 r300EmitBlit(rmesa, blit_format,
144 srcPitch,
145 srcOffset,
146 dstPitch,
147 t->bufAddr,
148 x,
149 y,
150 t->image[0][hwlevel].x + x,
151 t->image[0][hwlevel].y + y, width, height);
152
153 r300EmitWait(rmesa, R300_WAIT_2D);
154 }
155
156 static void r300UploadRectSubImage(r300ContextPtr rmesa,
157 r300TexObjPtr t,
158 struct gl_texture_image *texImage,
159 GLint x, GLint y, GLint width, GLint height)
160 {
161 const struct gl_texture_format *texFormat = texImage->TexFormat;
162 int blit_format, dstPitch, done;
163
164 switch (texFormat->TexelBytes) {
165 case 1:
166 blit_format = R300_CP_COLOR_FORMAT_CI8;
167 break;
168 case 2:
169 blit_format = R300_CP_COLOR_FORMAT_RGB565;
170 break;
171 case 4:
172 blit_format = R300_CP_COLOR_FORMAT_ARGB8888;
173 break;
174 case 8:
175 case 16:
176 blit_format = R300_CP_COLOR_FORMAT_CI8;
177 break;
178 default:
179 return;
180 }
181
182 t->image[0][0].data = texImage->Data;
183
184 /* Currently don't need to cope with small pitches.
185 */
186 width = texImage->Width;
187 height = texImage->Height;
188 dstPitch = t->pitch;
189
190 if (texFormat->TexelBytes > 4) {
191 width *= texFormat->TexelBytes;
192 }
193
194 if (rmesa->prefer_gart_client_texturing && texImage->IsClientData) {
195 /* In this case, could also use GART texturing. This is
196 * currently disabled, but has been tested & works.
197 */
198 t->offset = r300GartOffsetFromVirtual(rmesa, texImage->Data);
199 t->pitch = texImage->RowStride * texFormat->TexelBytes - 32;
200
201 if (RADEON_DEBUG & DEBUG_TEXTURE)
202 fprintf(stderr,
203 "Using GART texturing for rectangular client texture\n");
204
205 /* Release FB memory allocated for this image:
206 */
207 /* FIXME This may not be correct as driSwapOutTextureObject sets
208 * FIXME dirty_images. It may be fine, though.
209 */
210 if (t->base.memBlock) {
211 driSwapOutTextureObject((driTextureObject *) t);
212 }
213 } else if (texImage->IsClientData) {
214 /* Data already in GART memory, with usable pitch.
215 */
216 GLuint srcPitch;
217 srcPitch = texImage->RowStride * texFormat->TexelBytes;
218 r300EmitBlit(rmesa,
219 blit_format,
220 srcPitch,
221 r300GartOffsetFromVirtual(rmesa, texImage->Data),
222 dstPitch, t->bufAddr, 0, 0, 0, 0, width, height);
223 } else {
224 /* Data not in GART memory, or bad pitch.
225 */
226 for (done = 0; done < height;) {
227 struct r300_dma_region region;
228 int lines =
229 MIN2(height - done, RADEON_BUFFER_SIZE / dstPitch);
230 int src_pitch;
231 char *tex;
232
233 src_pitch = texImage->RowStride * texFormat->TexelBytes;
234
235 tex = (char *)texImage->Data + done * src_pitch;
236
237 memset(&region, 0, sizeof(region));
238 r300AllocDmaRegion(rmesa, &region, lines * dstPitch,
239 1024);
240
241 /* Copy texdata to dma:
242 */
243 if (RADEON_DEBUG & DEBUG_TEXTURE)
244 fprintf(stderr,
245 "%s: src_pitch %d dst_pitch %d\n",
246 __FUNCTION__, src_pitch, dstPitch);
247
248 if (src_pitch == dstPitch) {
249 memcpy(region.address + region.start, tex,
250 lines * src_pitch);
251 } else {
252 char *buf = region.address + region.start;
253 int i;
254 for (i = 0; i < lines; i++) {
255 memcpy(buf, tex, src_pitch);
256 buf += dstPitch;
257 tex += src_pitch;
258 }
259 }
260
261 r300EmitWait(rmesa, R300_WAIT_3D);
262
263 /* Blit to framebuffer
264 */
265 r300EmitBlit(rmesa,
266 blit_format,
267 dstPitch, GET_START(&region),
268 dstPitch | (t->tile_bits >> 16),
269 t->bufAddr, 0, 0, 0, done, width, lines);
270
271 r300EmitWait(rmesa, R300_WAIT_2D);
272 #ifdef USER_BUFFERS
273 r300_mem_use(rmesa, region.buf->id);
274 #endif
275
276 r300ReleaseDmaRegion(rmesa, &region, __FUNCTION__);
277 done += lines;
278 }
279 }
280 }
281
282 /**
283 * Upload the texture image associated with texture \a t at the specified
284 * level at the address relative to \a start.
285 */
286 static void r300UploadSubImage(r300ContextPtr rmesa, r300TexObjPtr t,
287 GLint hwlevel,
288 GLint x, GLint y, GLint width, GLint height,
289 GLuint face)
290 {
291 struct gl_texture_image *texImage = NULL;
292 GLuint offset;
293 GLint imageWidth, imageHeight;
294 GLint ret;
295 drm_radeon_texture_t tex;
296 drm_radeon_tex_image_t tmp;
297 const int level = hwlevel + t->base.firstLevel;
298
299 if (RADEON_DEBUG & DEBUG_TEXTURE) {
300 fprintf(stderr,
301 "%s( %p, %p ) level/width/height/face = %d/%d/%d/%u\n",
302 __FUNCTION__, (void *)t, (void *)t->base.tObj, level,
303 width, height, face);
304 }
305
306 ASSERT(face < 6);
307
308 /* Ensure we have a valid texture to upload */
309 if ((hwlevel < 0) || (hwlevel >= RADEON_MAX_TEXTURE_LEVELS)) {
310 _mesa_problem(NULL, "bad texture level in %s", __FUNCTION__);
311 return;
312 }
313
314 texImage = t->base.tObj->Image[face][level];
315
316 if (!texImage) {
317 if (RADEON_DEBUG & DEBUG_TEXTURE)
318 fprintf(stderr, "%s: texImage %d is NULL!\n",
319 __FUNCTION__, level);
320 return;
321 }
322 if (!texImage->Data) {
323 if (RADEON_DEBUG & DEBUG_TEXTURE)
324 fprintf(stderr, "%s: image data is NULL!\n",
325 __FUNCTION__);
326 return;
327 }
328
329 if (t->base.tObj->Target == GL_TEXTURE_RECTANGLE_NV) {
330 assert(level == 0);
331 assert(hwlevel == 0);
332 if (RADEON_DEBUG & DEBUG_TEXTURE)
333 fprintf(stderr, "%s: image data is rectangular\n",
334 __FUNCTION__);
335 r300UploadRectSubImage(rmesa, t, texImage, x, y, width, height);
336 return;
337 } else if (texImage->IsClientData) {
338 if (RADEON_DEBUG & DEBUG_TEXTURE)
339 fprintf(stderr,
340 "%s: image data is in GART client storage\n",
341 __FUNCTION__);
342 r300UploadGARTClientSubImage(rmesa, t, texImage, hwlevel, x, y,
343 width, height);
344 return;
345 } else if (RADEON_DEBUG & DEBUG_TEXTURE)
346 fprintf(stderr, "%s: image data is in normal memory\n",
347 __FUNCTION__);
348
349 imageWidth = texImage->Width;
350 imageHeight = texImage->Height;
351
352 offset = t->bufAddr;
353
354 if (RADEON_DEBUG & (DEBUG_TEXTURE | DEBUG_IOCTL)) {
355 GLint imageX = 0;
356 GLint imageY = 0;
357 GLint blitX = t->image[face][hwlevel].x;
358 GLint blitY = t->image[face][hwlevel].y;
359 GLint blitWidth = t->image[face][hwlevel].width;
360 GLint blitHeight = t->image[face][hwlevel].height;
361 fprintf(stderr, " upload image: %d,%d at %d,%d\n",
362 imageWidth, imageHeight, imageX, imageY);
363 fprintf(stderr, " upload blit: %d,%d at %d,%d\n",
364 blitWidth, blitHeight, blitX, blitY);
365 fprintf(stderr, " blit ofs: 0x%07x level: %d/%d\n",
366 (GLuint) offset, hwlevel, level);
367 }
368
369 t->image[face][hwlevel].data = texImage->Data;
370
371 /* Init the DRM_RADEON_TEXTURE command / drm_radeon_texture_t struct.
372 * NOTE: we're always use a 1KB-wide blit and I8 texture format.
373 * We used to use 1, 2 and 4-byte texels and used to use the texture
374 * width to dictate the blit width - but that won't work for compressed
375 * textures. (Brian)
376 * NOTE: can't do that with texture tiling. (sroland)
377 */
378 tex.offset = offset;
379 tex.image = &tmp;
380 /* copy (x,y,width,height,data) */
381 memcpy(&tmp, &t->image[face][hwlevel], sizeof(tmp));
382
383 if (texImage->TexFormat->TexelBytes > 4) {
384 const int log2TexelBytes =
385 (3 + (texImage->TexFormat->TexelBytes >> 4));
386 tex.format = RADEON_TXFORMAT_I8; /* any 1-byte texel format */
387 tex.pitch =
388 MAX2((texImage->Width * texImage->TexFormat->TexelBytes) /
389 64, 1);
390 tex.height = imageHeight;
391 tex.width = imageWidth << log2TexelBytes;
392 tex.offset += (tmp.x << log2TexelBytes) & ~1023;
393 tmp.x = tmp.x % (1024 >> log2TexelBytes);
394 tmp.width = tmp.width << log2TexelBytes;
395 } else if (texImage->TexFormat->TexelBytes) {
396 /* use multi-byte upload scheme */
397 tex.height = imageHeight;
398 tex.width = imageWidth;
399 switch (texImage->TexFormat->TexelBytes) {
400 case 1:
401 tex.format = RADEON_TXFORMAT_I8;
402 break;
403 case 2:
404 tex.format = RADEON_TXFORMAT_AI88;
405 break;
406 case 4:
407 tex.format = RADEON_TXFORMAT_ARGB8888;
408 break;
409 }
410 tex.pitch =
411 MAX2((texImage->Width * texImage->TexFormat->TexelBytes) /
412 64, 1);
413 tex.offset += tmp.x & ~1023;
414 tmp.x = tmp.x % 1024;
415
416 if (t->tile_bits & R300_TXO_MICRO_TILE) {
417 /* need something like "tiled coordinates" ? */
418 tmp.y = tmp.x / (tex.pitch * 128) * 2;
419 tmp.x =
420 tmp.x % (tex.pitch * 128) / 2 /
421 texImage->TexFormat->TexelBytes;
422 tex.pitch |= RADEON_DST_TILE_MICRO >> 22;
423 } else {
424 tmp.x = tmp.x >> (texImage->TexFormat->TexelBytes >> 1);
425 }
426 #if 1
427 if ((t->tile_bits & R300_TXO_MACRO_TILE) &&
428 (texImage->Width * texImage->TexFormat->TexelBytes >= 256)
429 && ((!(t->tile_bits & R300_TXO_MICRO_TILE)
430 && (texImage->Height >= 8))
431 || (texImage->Height >= 16))) {
432 /* weird: R200 disables macro tiling if mip width is smaller than 256 bytes,
433 OR if height is smaller than 8 automatically, but if micro tiling is active
434 the limit is height 16 instead ? */
435 tex.pitch |= RADEON_DST_TILE_MACRO >> 22;
436 }
437 #endif
438 } else {
439 /* In case of for instance 8x8 texture (2x2 dxt blocks),
440 padding after the first two blocks is needed (only
441 with dxt1 since 2 dxt3/dxt5 blocks already use 32 Byte). */
442 /* set tex.height to 1/4 since 1 "macropixel" (dxt-block)
443 has 4 real pixels. Needed so the kernel module reads
444 the right amount of data. */
445 tex.format = RADEON_TXFORMAT_I8; /* any 1-byte texel format */
446 tex.pitch = (R300_BLIT_WIDTH_BYTES / 64);
447 tex.height = (imageHeight + 3) / 4;
448 tex.width = (imageWidth + 3) / 4;
449 if ((t->format & R300_TX_FORMAT_DXT1) == R300_TX_FORMAT_DXT1) {
450 tex.width *= 8;
451 } else {
452 tex.width *= 16;
453 }
454 }
455
456 LOCK_HARDWARE(&rmesa->radeon);
457 do {
458 ret =
459 drmCommandWriteRead(rmesa->radeon.dri.fd,
460 DRM_RADEON_TEXTURE, &tex,
461 sizeof(drm_radeon_texture_t));
462 if (ret) {
463 if (RADEON_DEBUG & DEBUG_IOCTL)
464 fprintf(stderr,
465 "DRM_RADEON_TEXTURE: again!\n");
466 usleep(1);
467 }
468 } while (ret == -EAGAIN);
469
470 UNLOCK_HARDWARE(&rmesa->radeon);
471
472 if (ret) {
473 fprintf(stderr, "DRM_RADEON_TEXTURE: return = %d\n", ret);
474 fprintf(stderr, " offset=0x%08x\n", offset);
475 fprintf(stderr, " image width=%d height=%d\n",
476 imageWidth, imageHeight);
477 fprintf(stderr, " blit width=%d height=%d data=%p\n",
478 t->image[face][hwlevel].width,
479 t->image[face][hwlevel].height,
480 t->image[face][hwlevel].data);
481 _mesa_exit(-1);
482 }
483 }
484
485 /**
486 * Upload the texture images associated with texture \a t. This might
487 * require the allocation of texture memory.
488 *
489 * \param rmesa Context pointer
490 * \param t Texture to be uploaded
491 * \param face Cube map face to be uploaded. Zero for non-cube maps.
492 */
493
494 int r300UploadTexImages(r300ContextPtr rmesa, r300TexObjPtr t, GLuint face)
495 {
496 const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;
497
498 if (t->image_override)
499 return 0;
500
501 if (RADEON_DEBUG & (DEBUG_TEXTURE | DEBUG_IOCTL)) {
502 fprintf(stderr, "%s( %p, %p ) sz=%d lvls=%d-%d\n", __FUNCTION__,
503 (void *)rmesa->radeon.glCtx, (void *)t->base.tObj,
504 t->base.totalSize, t->base.firstLevel,
505 t->base.lastLevel);
506 }
507
508 if (t->base.totalSize == 0)
509 return 0;
510
511 if (RADEON_DEBUG & DEBUG_SYNC) {
512 fprintf(stderr, "%s: Syncing\n", __FUNCTION__);
513 radeonFinish(rmesa->radeon.glCtx);
514 }
515
516 LOCK_HARDWARE(&rmesa->radeon);
517
518 if (t->base.memBlock == NULL) {
519 int heap;
520
521 heap = driAllocateTexture(rmesa->texture_heaps, rmesa->nr_heaps,
522 (driTextureObject *) t);
523 if (heap == -1) {
524 UNLOCK_HARDWARE(&rmesa->radeon);
525 return -1;
526 }
527
528 /* Set the base offset of the texture image */
529 t->bufAddr = rmesa->radeon.radeonScreen->texOffset[heap]
530 + t->base.memBlock->ofs;
531 t->offset = t->bufAddr;
532
533 if (!(t->base.tObj->Image[0][0]->IsClientData)) {
534 /* hope it's safe to add that here... */
535 t->offset |= t->tile_bits;
536 }
537 }
538
539 /* Let the world know we've used this memory recently.
540 */
541 driUpdateTextureLRU((driTextureObject *) t);
542 UNLOCK_HARDWARE(&rmesa->radeon);
543
544 /* Upload any images that are new */
545 if (t->base.dirty_images[face]) {
546 int i;
547 for (i = 0; i < numLevels; i++) {
548 if ((t->base.
549 dirty_images[face] & (1 <<
550 (i + t->base.firstLevel))) !=
551 0) {
552 r300UploadSubImage(rmesa, t, i, 0, 0,
553 t->image[face][i].width,
554 t->image[face][i].height,
555 face);
556 }
557 }
558 t->base.dirty_images[face] = 0;
559 }
560
561 if (RADEON_DEBUG & DEBUG_SYNC) {
562 fprintf(stderr, "%s: Syncing\n", __FUNCTION__);
563 radeonFinish(rmesa->radeon.glCtx);
564 }
565
566 return 0;
567 }