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_state_sampler.c 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**************************************************************************
  2. *
  3. * Copyright 2007 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. /* Authors:
  28. * Brian Paul
  29. */
  30. #include "util/u_memory.h"
  31. #include "draw/draw_context.h"
  32. #include "draw/draw_context.h"
  33. #include "sp_context.h"
  34. #include "sp_state.h"
  35. #include "sp_texture.h"
  36. #include "sp_tex_sample.h"
  37. #include "sp_tex_tile_cache.h"
  38. struct sp_sampler {
  39. struct pipe_sampler_state base;
  40. struct sp_sampler_varient *varients;
  41. struct sp_sampler_varient *current;
  42. };
  43. static struct sp_sampler *sp_sampler( struct pipe_sampler_state *sampler )
  44. {
  45. return (struct sp_sampler *)sampler;
  46. }
  47. void *
  48. softpipe_create_sampler_state(struct pipe_context *pipe,
  49. const struct pipe_sampler_state *sampler)
  50. {
  51. struct sp_sampler *sp_sampler = CALLOC_STRUCT(sp_sampler);
  52. sp_sampler->base = *sampler;
  53. sp_sampler->varients = NULL;
  54. return (void *)sp_sampler;
  55. }
  56. void
  57. softpipe_bind_sampler_states(struct pipe_context *pipe,
  58. unsigned num, void **sampler)
  59. {
  60. struct softpipe_context *softpipe = softpipe_context(pipe);
  61. unsigned i;
  62. assert(num <= PIPE_MAX_SAMPLERS);
  63. /* Check for no-op */
  64. if (num == softpipe->num_samplers &&
  65. !memcmp(softpipe->sampler, sampler, num * sizeof(void *)))
  66. return;
  67. draw_flush(softpipe->draw);
  68. for (i = 0; i < num; ++i)
  69. softpipe->sampler[i] = sampler[i];
  70. for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
  71. softpipe->sampler[i] = NULL;
  72. softpipe->num_samplers = num;
  73. softpipe->dirty |= SP_NEW_SAMPLER;
  74. }
  75. void
  76. softpipe_bind_vertex_sampler_states(struct pipe_context *pipe,
  77. unsigned num_samplers,
  78. void **samplers)
  79. {
  80. struct softpipe_context *softpipe = softpipe_context(pipe);
  81. unsigned i;
  82. assert(num_samplers <= PIPE_MAX_VERTEX_SAMPLERS);
  83. /* Check for no-op */
  84. if (num_samplers == softpipe->num_vertex_samplers &&
  85. !memcmp(softpipe->vertex_samplers, samplers, num_samplers * sizeof(void *)))
  86. return;
  87. draw_flush(softpipe->draw);
  88. for (i = 0; i < num_samplers; ++i)
  89. softpipe->vertex_samplers[i] = samplers[i];
  90. for (i = num_samplers; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
  91. softpipe->vertex_samplers[i] = NULL;
  92. softpipe->num_vertex_samplers = num_samplers;
  93. softpipe->dirty |= SP_NEW_SAMPLER;
  94. }
  95. struct pipe_sampler_view *
  96. softpipe_create_sampler_view(struct pipe_context *pipe,
  97. struct pipe_texture *texture,
  98. const struct pipe_sampler_view *templ)
  99. {
  100. struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
  101. if (view) {
  102. *view = *templ;
  103. view->reference.count = 1;
  104. view->texture = NULL;
  105. pipe_texture_reference(&view->texture, texture);
  106. view->context = pipe;
  107. }
  108. return view;
  109. }
  110. void
  111. softpipe_sampler_view_destroy(struct pipe_context *pipe,
  112. struct pipe_sampler_view *view)
  113. {
  114. pipe_texture_reference(&view->texture, NULL);
  115. FREE(view);
  116. }
  117. void
  118. softpipe_set_sampler_views(struct pipe_context *pipe,
  119. unsigned num,
  120. struct pipe_sampler_view **views)
  121. {
  122. struct softpipe_context *softpipe = softpipe_context(pipe);
  123. uint i;
  124. assert(num <= PIPE_MAX_SAMPLERS);
  125. /* Check for no-op */
  126. if (num == softpipe->num_sampler_views &&
  127. !memcmp(softpipe->sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
  128. return;
  129. draw_flush(softpipe->draw);
  130. for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
  131. struct pipe_sampler_view *view = i < num ? views[i] : NULL;
  132. pipe_sampler_view_reference(&softpipe->sampler_views[i], view);
  133. sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[i], view);
  134. }
  135. softpipe->num_sampler_views = num;
  136. softpipe->dirty |= SP_NEW_TEXTURE;
  137. }
  138. void
  139. softpipe_set_vertex_sampler_views(struct pipe_context *pipe,
  140. unsigned num,
  141. struct pipe_sampler_view **views)
  142. {
  143. struct softpipe_context *softpipe = softpipe_context(pipe);
  144. uint i;
  145. assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
  146. /* Check for no-op */
  147. if (num == softpipe->num_vertex_sampler_views &&
  148. !memcmp(softpipe->vertex_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
  149. return;
  150. }
  151. draw_flush(softpipe->draw);
  152. for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
  153. struct pipe_sampler_view *view = i < num ? views[i] : NULL;
  154. pipe_sampler_view_reference(&softpipe->vertex_sampler_views[i], view);
  155. sp_tex_tile_cache_set_sampler_view(softpipe->vertex_tex_cache[i], view);
  156. }
  157. softpipe->num_vertex_sampler_views = num;
  158. softpipe->dirty |= SP_NEW_TEXTURE;
  159. }
  160. /**
  161. * Find/create an sp_sampler_varient object for sampling the given texture,
  162. * sampler and tex unit.
  163. *
  164. * Note that the tex unit is significant. We can't re-use a sampler
  165. * varient for multiple texture units because the sampler varient contains
  166. * the texture object pointer. If the texture object pointer were stored
  167. * somewhere outside the sampler varient, we could re-use samplers for
  168. * multiple texture units.
  169. */
  170. static struct sp_sampler_varient *
  171. get_sampler_varient( unsigned unit,
  172. struct sp_sampler *sampler,
  173. struct pipe_texture *texture,
  174. unsigned processor )
  175. {
  176. struct softpipe_texture *sp_texture = softpipe_texture(texture);
  177. struct sp_sampler_varient *v = NULL;
  178. union sp_sampler_key key;
  179. /* if this fails, widen the key.unit field and update this assertion */
  180. assert(PIPE_MAX_SAMPLERS <= 16);
  181. key.bits.target = sp_texture->base.target;
  182. key.bits.is_pot = sp_texture->pot;
  183. key.bits.processor = processor;
  184. key.bits.unit = unit;
  185. key.bits.pad = 0;
  186. if (sampler->current &&
  187. key.value == sampler->current->key.value) {
  188. v = sampler->current;
  189. }
  190. if (v == NULL) {
  191. for (v = sampler->varients; v; v = v->next)
  192. if (v->key.value == key.value)
  193. break;
  194. if (v == NULL) {
  195. v = sp_create_sampler_varient( &sampler->base, key );
  196. v->next = sampler->varients;
  197. sampler->varients = v;
  198. }
  199. }
  200. sampler->current = v;
  201. return v;
  202. }
  203. void
  204. softpipe_reset_sampler_varients(struct softpipe_context *softpipe)
  205. {
  206. int i;
  207. /* It's a bit hard to build these samplers ahead of time -- don't
  208. * really know which samplers are going to be used for vertex and
  209. * fragment programs.
  210. */
  211. for (i = 0; i <= softpipe->vs->max_sampler; i++) {
  212. if (softpipe->vertex_samplers[i]) {
  213. struct pipe_texture *texture = NULL;
  214. if (softpipe->vertex_sampler_views[i]) {
  215. texture = softpipe->vertex_sampler_views[i]->texture;
  216. }
  217. softpipe->tgsi.vert_samplers_list[i] =
  218. get_sampler_varient( i,
  219. sp_sampler(softpipe->vertex_samplers[i]),
  220. texture,
  221. TGSI_PROCESSOR_VERTEX );
  222. sp_sampler_varient_bind_texture( softpipe->tgsi.vert_samplers_list[i],
  223. softpipe->vertex_tex_cache[i],
  224. texture );
  225. }
  226. }
  227. for (i = 0; i <= softpipe->fs->info.file_max[TGSI_FILE_SAMPLER]; i++) {
  228. if (softpipe->sampler[i]) {
  229. struct pipe_texture *texture = NULL;
  230. if (softpipe->sampler_views[i]) {
  231. texture = softpipe->sampler_views[i]->texture;
  232. }
  233. softpipe->tgsi.frag_samplers_list[i] =
  234. get_sampler_varient( i,
  235. sp_sampler(softpipe->sampler[i]),
  236. texture,
  237. TGSI_PROCESSOR_FRAGMENT );
  238. sp_sampler_varient_bind_texture( softpipe->tgsi.frag_samplers_list[i],
  239. softpipe->tex_cache[i],
  240. texture );
  241. }
  242. }
  243. }
  244. void
  245. softpipe_delete_sampler_state(struct pipe_context *pipe,
  246. void *sampler)
  247. {
  248. struct sp_sampler *sp_sampler = (struct sp_sampler *)sampler;
  249. struct sp_sampler_varient *v, *tmp;
  250. for (v = sp_sampler->varients; v; v = tmp) {
  251. tmp = v->next;
  252. sp_sampler_varient_destroy(v);
  253. }
  254. FREE( sampler );
  255. }