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.

svga_sampler_view.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**********************************************************
  2. * Copyright 2008-2009 VMware, Inc. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. **********************************************************/
  25. #include "svga_cmd.h"
  26. #include "pipe/p_state.h"
  27. #include "pipe/p_defines.h"
  28. #include "util/u_inlines.h"
  29. #include "os/os_thread.h"
  30. #include "util/u_format.h"
  31. #include "util/u_math.h"
  32. #include "util/u_memory.h"
  33. #include "svga_screen.h"
  34. #include "svga_context.h"
  35. #include "svga_resource_texture.h"
  36. #include "svga_sampler_view.h"
  37. #include "svga_debug.h"
  38. #include "svga_surface.h"
  39. struct svga_sampler_view *
  40. svga_get_tex_sampler_view(struct pipe_context *pipe,
  41. struct pipe_resource *pt,
  42. unsigned min_lod, unsigned max_lod)
  43. {
  44. struct svga_screen *ss = svga_screen(pt->screen);
  45. struct svga_texture *tex = svga_texture(pt);
  46. struct svga_sampler_view *sv = NULL;
  47. SVGA3dSurfaceFormat format = svga_translate_format(pt->format);
  48. boolean view = TRUE;
  49. assert(pt);
  50. assert(min_lod >= 0);
  51. assert(min_lod <= max_lod);
  52. assert(max_lod <= pt->last_level);
  53. /* Is a view needed */
  54. {
  55. /*
  56. * Can't control max lod. For first level views and when we only
  57. * look at one level we disable mip filtering to achive the same
  58. * results as a view.
  59. */
  60. if (min_lod == 0 && max_lod >= pt->last_level)
  61. view = FALSE;
  62. if (util_format_is_s3tc(pt->format) && view) {
  63. format = svga_translate_format_render(pt->format);
  64. }
  65. if (ss->debug.no_sampler_view)
  66. view = FALSE;
  67. if (ss->debug.force_sampler_view)
  68. view = TRUE;
  69. }
  70. /* First try the cache */
  71. if (view) {
  72. pipe_mutex_lock(ss->tex_mutex);
  73. if (tex->cached_view &&
  74. tex->cached_view->min_lod == min_lod &&
  75. tex->cached_view->max_lod == max_lod) {
  76. svga_sampler_view_reference(&sv, tex->cached_view);
  77. pipe_mutex_unlock(ss->tex_mutex);
  78. SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",
  79. pt, min_lod, max_lod, pt->last_level);
  80. svga_validate_sampler_view(svga_context(pipe), sv);
  81. return sv;
  82. }
  83. pipe_mutex_unlock(ss->tex_mutex);
  84. }
  85. sv = CALLOC_STRUCT(svga_sampler_view);
  86. pipe_reference_init(&sv->reference, 1);
  87. pipe_resource_reference(&sv->texture, pt);
  88. sv->min_lod = min_lod;
  89. sv->max_lod = max_lod;
  90. /* No view needed just use the whole texture */
  91. if (!view) {
  92. SVGA_DBG(DEBUG_VIEWS,
  93. "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
  94. pt, min_lod, max_lod,
  95. max_lod - min_lod + 1,
  96. pt->width0,
  97. pt->height0,
  98. pt->depth0,
  99. pt->last_level);
  100. sv->key.cachable = 0;
  101. sv->handle = tex->handle;
  102. return sv;
  103. }
  104. SVGA_DBG(DEBUG_VIEWS,
  105. "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
  106. pt, min_lod, max_lod,
  107. max_lod - min_lod + 1,
  108. pt->width0,
  109. pt->height0,
  110. pt->depth0,
  111. pt->last_level);
  112. sv->age = tex->age;
  113. sv->handle = svga_texture_view_surface(pipe, tex, format,
  114. min_lod,
  115. max_lod - min_lod + 1,
  116. -1, -1,
  117. &sv->key);
  118. if (!sv->handle) {
  119. assert(0);
  120. sv->key.cachable = 0;
  121. sv->handle = tex->handle;
  122. return sv;
  123. }
  124. pipe_mutex_lock(ss->tex_mutex);
  125. svga_sampler_view_reference(&tex->cached_view, sv);
  126. pipe_mutex_unlock(ss->tex_mutex);
  127. return sv;
  128. }
  129. void
  130. svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *v)
  131. {
  132. struct svga_texture *tex = svga_texture(v->texture);
  133. unsigned numFaces;
  134. unsigned age = 0;
  135. int i, k;
  136. assert(svga);
  137. if (v->handle == tex->handle)
  138. return;
  139. age = tex->age;
  140. if(tex->b.b.target == PIPE_TEXTURE_CUBE)
  141. numFaces = 6;
  142. else
  143. numFaces = 1;
  144. for (i = v->min_lod; i <= v->max_lod; i++) {
  145. for (k = 0; k < numFaces; k++) {
  146. if (v->age < tex->view_age[i])
  147. svga_texture_copy_handle(svga,
  148. tex->handle, 0, 0, 0, i, k,
  149. v->handle, 0, 0, 0, i - v->min_lod, k,
  150. u_minify(tex->b.b.width0, i),
  151. u_minify(tex->b.b.height0, i),
  152. u_minify(tex->b.b.depth0, i));
  153. }
  154. }
  155. v->age = age;
  156. }
  157. void
  158. svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
  159. {
  160. struct svga_texture *tex = svga_texture(v->texture);
  161. if(v->handle != tex->handle) {
  162. struct svga_screen *ss = svga_screen(v->texture->screen);
  163. SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
  164. svga_screen_surface_destroy(ss, &v->key, &v->handle);
  165. }
  166. pipe_resource_reference(&v->texture, NULL);
  167. FREE(v);
  168. }