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_pipe_sampler.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "util/u_inlines.h"
  26. #include "pipe/p_defines.h"
  27. #include "util/u_format.h"
  28. #include "util/u_math.h"
  29. #include "util/u_memory.h"
  30. #include "tgsi/tgsi_parse.h"
  31. #include "svga_context.h"
  32. #include "svga_resource_texture.h"
  33. #include "svga_debug.h"
  34. static INLINE unsigned
  35. translate_wrap_mode(unsigned wrap)
  36. {
  37. switch (wrap) {
  38. case PIPE_TEX_WRAP_REPEAT:
  39. return SVGA3D_TEX_ADDRESS_WRAP;
  40. case PIPE_TEX_WRAP_CLAMP:
  41. return SVGA3D_TEX_ADDRESS_CLAMP;
  42. case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
  43. /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by
  44. * hardware.
  45. */
  46. return SVGA3D_TEX_ADDRESS_CLAMP;
  47. case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
  48. return SVGA3D_TEX_ADDRESS_BORDER;
  49. case PIPE_TEX_WRAP_MIRROR_REPEAT:
  50. return SVGA3D_TEX_ADDRESS_MIRROR;
  51. case PIPE_TEX_WRAP_MIRROR_CLAMP:
  52. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
  53. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
  54. return SVGA3D_TEX_ADDRESS_MIRRORONCE;
  55. default:
  56. assert(0);
  57. return SVGA3D_TEX_ADDRESS_WRAP;
  58. }
  59. }
  60. static INLINE unsigned translate_img_filter( unsigned filter )
  61. {
  62. switch (filter) {
  63. case PIPE_TEX_FILTER_NEAREST: return SVGA3D_TEX_FILTER_NEAREST;
  64. case PIPE_TEX_FILTER_LINEAR: return SVGA3D_TEX_FILTER_LINEAR;
  65. default:
  66. assert(0);
  67. return SVGA3D_TEX_FILTER_NEAREST;
  68. }
  69. }
  70. static INLINE unsigned translate_mip_filter( unsigned filter )
  71. {
  72. switch (filter) {
  73. case PIPE_TEX_MIPFILTER_NONE: return SVGA3D_TEX_FILTER_NONE;
  74. case PIPE_TEX_MIPFILTER_NEAREST: return SVGA3D_TEX_FILTER_NEAREST;
  75. case PIPE_TEX_MIPFILTER_LINEAR: return SVGA3D_TEX_FILTER_LINEAR;
  76. default:
  77. assert(0);
  78. return SVGA3D_TEX_FILTER_NONE;
  79. }
  80. }
  81. static void *
  82. svga_create_sampler_state(struct pipe_context *pipe,
  83. const struct pipe_sampler_state *sampler)
  84. {
  85. struct svga_context *svga = svga_context(pipe);
  86. struct svga_sampler_state *cso = CALLOC_STRUCT( svga_sampler_state );
  87. cso->mipfilter = translate_mip_filter(sampler->min_mip_filter);
  88. cso->magfilter = translate_img_filter( sampler->mag_img_filter );
  89. cso->minfilter = translate_img_filter( sampler->min_img_filter );
  90. cso->aniso_level = MAX2( sampler->max_anisotropy, 1 );
  91. if(sampler->max_anisotropy)
  92. cso->magfilter = cso->minfilter = SVGA3D_TEX_FILTER_ANISOTROPIC;
  93. cso->lod_bias = sampler->lod_bias;
  94. cso->addressu = translate_wrap_mode(sampler->wrap_s);
  95. cso->addressv = translate_wrap_mode(sampler->wrap_t);
  96. cso->addressw = translate_wrap_mode(sampler->wrap_r);
  97. cso->normalized_coords = sampler->normalized_coords;
  98. cso->compare_mode = sampler->compare_mode;
  99. cso->compare_func = sampler->compare_func;
  100. {
  101. uint32 r = float_to_ubyte(sampler->border_color.f[0]);
  102. uint32 g = float_to_ubyte(sampler->border_color.f[1]);
  103. uint32 b = float_to_ubyte(sampler->border_color.f[2]);
  104. uint32 a = float_to_ubyte(sampler->border_color.f[3]);
  105. cso->bordercolor = (a << 24) | (r << 16) | (g << 8) | b;
  106. }
  107. /* No SVGA3D support for:
  108. * - min/max LOD clamping
  109. */
  110. cso->min_lod = 0;
  111. cso->view_min_lod = MAX2(sampler->min_lod, 0);
  112. cso->view_max_lod = MAX2(sampler->max_lod, 0);
  113. /* Use min_mipmap */
  114. if (svga->debug.use_min_mipmap) {
  115. if (cso->view_min_lod == cso->view_max_lod) {
  116. cso->min_lod = cso->view_min_lod;
  117. cso->view_min_lod = 0;
  118. cso->view_max_lod = 1000; /* Just a high number */
  119. cso->mipfilter = SVGA3D_TEX_FILTER_NONE;
  120. }
  121. }
  122. SVGA_DBG(DEBUG_VIEWS, "min %u, view(min %u, max %u) lod, mipfilter %s\n",
  123. cso->min_lod, cso->view_min_lod, cso->view_max_lod,
  124. cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
  125. return cso;
  126. }
  127. static void
  128. svga_bind_fragment_sampler_states(struct pipe_context *pipe,
  129. unsigned num, void **sampler)
  130. {
  131. struct svga_context *svga = svga_context(pipe);
  132. unsigned i;
  133. assert(num <= PIPE_MAX_SAMPLERS);
  134. /* Check for no-op */
  135. if (num == svga->curr.num_samplers &&
  136. !memcmp(svga->curr.sampler, sampler, num * sizeof(void *))) {
  137. if (0) debug_printf("sampler noop\n");
  138. return;
  139. }
  140. for (i = 0; i < num; i++)
  141. svga->curr.sampler[i] = sampler[i];
  142. for (i = num; i < svga->curr.num_samplers; i++)
  143. svga->curr.sampler[i] = NULL;
  144. svga->curr.num_samplers = num;
  145. svga->dirty |= SVGA_NEW_SAMPLER;
  146. }
  147. static void svga_delete_sampler_state(struct pipe_context *pipe,
  148. void *sampler)
  149. {
  150. FREE(sampler);
  151. }
  152. static struct pipe_sampler_view *
  153. svga_create_sampler_view(struct pipe_context *pipe,
  154. struct pipe_resource *texture,
  155. const struct pipe_sampler_view *templ)
  156. {
  157. struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
  158. if (view) {
  159. *view = *templ;
  160. view->reference.count = 1;
  161. view->texture = NULL;
  162. pipe_resource_reference(&view->texture, texture);
  163. view->context = pipe;
  164. }
  165. return view;
  166. }
  167. static void
  168. svga_sampler_view_destroy(struct pipe_context *pipe,
  169. struct pipe_sampler_view *view)
  170. {
  171. pipe_resource_reference(&view->texture, NULL);
  172. FREE(view);
  173. }
  174. static void
  175. svga_set_fragment_sampler_views(struct pipe_context *pipe,
  176. unsigned num,
  177. struct pipe_sampler_view **views)
  178. {
  179. struct svga_context *svga = svga_context(pipe);
  180. unsigned flag_1d = 0;
  181. unsigned flag_srgb = 0;
  182. uint i;
  183. assert(num <= PIPE_MAX_SAMPLERS);
  184. /* Check for no-op */
  185. if (num == svga->curr.num_sampler_views &&
  186. !memcmp(svga->curr.sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
  187. if (0) debug_printf("texture noop\n");
  188. return;
  189. }
  190. for (i = 0; i < num; i++) {
  191. pipe_sampler_view_reference(&svga->curr.sampler_views[i],
  192. views[i]);
  193. if (!views[i])
  194. continue;
  195. if (util_format_is_srgb(views[i]->format))
  196. flag_srgb |= 1 << i;
  197. if (views[i]->texture->target == PIPE_TEXTURE_1D)
  198. flag_1d |= 1 << i;
  199. }
  200. for (i = num; i < svga->curr.num_sampler_views; i++)
  201. pipe_sampler_view_reference(&svga->curr.sampler_views[i],
  202. NULL);
  203. svga->curr.num_sampler_views = num;
  204. svga->dirty |= SVGA_NEW_TEXTURE_BINDING;
  205. if (flag_srgb != svga->curr.tex_flags.flag_srgb ||
  206. flag_1d != svga->curr.tex_flags.flag_1d)
  207. {
  208. svga->dirty |= SVGA_NEW_TEXTURE_FLAGS;
  209. svga->curr.tex_flags.flag_1d = flag_1d;
  210. svga->curr.tex_flags.flag_srgb = flag_srgb;
  211. }
  212. }
  213. void svga_init_sampler_functions( struct svga_context *svga )
  214. {
  215. svga->pipe.create_sampler_state = svga_create_sampler_state;
  216. svga->pipe.bind_fragment_sampler_states = svga_bind_fragment_sampler_states;
  217. svga->pipe.delete_sampler_state = svga_delete_sampler_state;
  218. svga->pipe.set_fragment_sampler_views = svga_set_fragment_sampler_views;
  219. svga->pipe.create_sampler_view = svga_create_sampler_view;
  220. svga->pipe.sampler_view_destroy = svga_sampler_view_destroy;
  221. }