Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sp_texture.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**************************************************************************
  2. *
  3. * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * 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, sub license, 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 portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors:
  29. * Keith Whitwell <keith@tungstengraphics.com>
  30. * Michel Dänzer <michel@tungstengraphics.com>
  31. */
  32. #include "pipe/p_context.h"
  33. #include "pipe/p_defines.h"
  34. #include "pipe/p_inlines.h"
  35. #include "pipe/p_util.h"
  36. #include "pipe/p_winsys.h"
  37. #include "sp_context.h"
  38. #include "sp_state.h"
  39. #include "sp_texture.h"
  40. #include "sp_tile_cache.h"
  41. /* Simple, maximally packed layout.
  42. */
  43. static unsigned minify( unsigned d )
  44. {
  45. return MAX2(1, d>>1);
  46. }
  47. static void
  48. softpipe_texture_layout(struct softpipe_texture * spt)
  49. {
  50. struct pipe_texture *pt = &spt->base;
  51. unsigned level;
  52. unsigned width = pt->width[0];
  53. unsigned height = pt->height[0];
  54. unsigned depth = pt->depth[0];
  55. spt->buffer_size = 0;
  56. for (level = 0; level <= pt->last_level; level++) {
  57. pt->width[level] = width;
  58. pt->height[level] = height;
  59. pt->depth[level] = depth;
  60. spt->level_offset[level] = spt->buffer_size;
  61. spt->buffer_size += ((pt->compressed) ? MAX2(1, height/4) : height) *
  62. ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
  63. width * pt->cpp;
  64. width = minify(width);
  65. height = minify(height);
  66. depth = minify(depth);
  67. }
  68. }
  69. struct pipe_texture *
  70. softpipe_texture_create(struct pipe_context *pipe,
  71. const struct pipe_texture *templat)
  72. {
  73. struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
  74. if (!spt)
  75. return NULL;
  76. spt->base = *templat;
  77. spt->base.refcount = 1;
  78. softpipe_texture_layout(spt);
  79. spt->buffer = pipe->winsys->buffer_create(pipe->winsys, 32,
  80. PIPE_BUFFER_USAGE_PIXEL,
  81. spt->buffer_size);
  82. if (!spt->buffer) {
  83. FREE(spt);
  84. return NULL;
  85. }
  86. assert(spt->base.refcount == 1);
  87. return &spt->base;
  88. }
  89. void
  90. softpipe_texture_release(struct pipe_context *pipe, struct pipe_texture **pt)
  91. {
  92. if (!*pt)
  93. return;
  94. /*
  95. DBG("%s %p refcount will be %d\n",
  96. __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
  97. */
  98. if (--(*pt)->refcount <= 0) {
  99. struct softpipe_texture *spt = softpipe_texture(*pt);
  100. /*
  101. DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
  102. */
  103. pipe_buffer_reference(pipe->winsys, &spt->buffer, NULL);
  104. FREE(spt);
  105. }
  106. *pt = NULL;
  107. }
  108. void
  109. softpipe_texture_update(struct pipe_context *pipe,
  110. struct pipe_texture *texture)
  111. {
  112. struct softpipe_context *softpipe = softpipe_context(pipe);
  113. uint unit;
  114. for (unit = 0; unit < PIPE_MAX_SAMPLERS; unit++) {
  115. if (softpipe->texture[unit] == texture) {
  116. sp_flush_tile_cache(softpipe, softpipe->tex_cache[unit]);
  117. }
  118. }
  119. }
  120. /**
  121. * Called via pipe->get_tex_surface()
  122. */
  123. struct pipe_surface *
  124. softpipe_get_tex_surface(struct pipe_context *pipe,
  125. struct pipe_texture *pt,
  126. unsigned face, unsigned level, unsigned zslice)
  127. {
  128. struct softpipe_texture *spt = softpipe_texture(pt);
  129. struct pipe_surface *ps;
  130. assert(level <= pt->last_level);
  131. ps = pipe->winsys->surface_alloc(pipe->winsys);
  132. if (ps) {
  133. assert(ps->refcount);
  134. assert(ps->winsys);
  135. pipe_buffer_reference(pipe->winsys, &ps->buffer, spt->buffer);
  136. ps->format = pt->format;
  137. ps->cpp = pt->cpp;
  138. ps->width = pt->width[level];
  139. ps->height = pt->height[level];
  140. ps->pitch = ps->width;
  141. ps->offset = spt->level_offset[level];
  142. if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
  143. ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
  144. (pt->compressed ? ps->height/4 : ps->height) *
  145. ps->width * ps->cpp;
  146. } else {
  147. assert(face == 0);
  148. assert(zslice == 0);
  149. }
  150. }
  151. return ps;
  152. }