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.

fd3_texture.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
  2. /*
  3. * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Authors:
  25. * Rob Clark <robclark@freedesktop.org>
  26. */
  27. #include "pipe/p_state.h"
  28. #include "util/u_string.h"
  29. #include "util/u_memory.h"
  30. #include "util/u_inlines.h"
  31. #include "util/u_format.h"
  32. #include "fd3_texture.h"
  33. #include "fd3_util.h"
  34. static enum a3xx_tex_clamp
  35. tex_clamp(unsigned wrap)
  36. {
  37. /* hardware probably supports more, but we can't coax all the
  38. * wrap/clamp modes out of the GLESv2 blob driver.
  39. *
  40. * TODO once we have basics working, go back and just try
  41. * different values and see what happens
  42. */
  43. switch (wrap) {
  44. case PIPE_TEX_WRAP_REPEAT:
  45. return A3XX_TEX_REPEAT;
  46. case PIPE_TEX_WRAP_CLAMP:
  47. case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
  48. return A3XX_TEX_CLAMP_TO_EDGE;
  49. case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
  50. return A3XX_TEX_CLAMP_TO_BORDER;
  51. case PIPE_TEX_WRAP_MIRROR_CLAMP:
  52. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
  53. /* these two we should emulate! */
  54. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
  55. /* only works for PoT.. need to emulate otherwise! */
  56. return A3XX_TEX_MIRROR_CLAMP;
  57. case PIPE_TEX_WRAP_MIRROR_REPEAT:
  58. return A3XX_TEX_MIRROR_REPEAT;
  59. default:
  60. DBG("invalid wrap: %u", wrap);
  61. return 0;
  62. }
  63. }
  64. static enum a3xx_tex_filter
  65. tex_filter(unsigned filter)
  66. {
  67. switch (filter) {
  68. case PIPE_TEX_FILTER_NEAREST:
  69. return A3XX_TEX_NEAREST;
  70. case PIPE_TEX_FILTER_LINEAR:
  71. return A3XX_TEX_LINEAR;
  72. default:
  73. DBG("invalid filter: %u", filter);
  74. return 0;
  75. }
  76. }
  77. static void *
  78. fd3_sampler_state_create(struct pipe_context *pctx,
  79. const struct pipe_sampler_state *cso)
  80. {
  81. struct fd3_sampler_stateobj *so = CALLOC_STRUCT(fd3_sampler_stateobj);
  82. bool miplinear = false;
  83. if (!so)
  84. return NULL;
  85. if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
  86. miplinear = true;
  87. so->base = *cso;
  88. so->texsamp0 =
  89. COND(!cso->normalized_coords, A3XX_TEX_SAMP_0_UNNORM_COORDS) |
  90. COND(miplinear, A3XX_TEX_SAMP_0_MIPFILTER_LINEAR) |
  91. A3XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter)) |
  92. A3XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter)) |
  93. A3XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s)) |
  94. A3XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t)) |
  95. A3XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r));
  96. if (cso->compare_mode)
  97. so->texsamp0 |= A3XX_TEX_SAMP_0_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
  98. if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
  99. so->texsamp1 =
  100. A3XX_TEX_SAMP_1_LOD_BIAS(cso->lod_bias) |
  101. A3XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
  102. A3XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
  103. } else {
  104. so->texsamp1 = 0x00000000;
  105. }
  106. return so;
  107. }
  108. static enum a3xx_tex_type
  109. tex_type(unsigned target)
  110. {
  111. switch (target) {
  112. default:
  113. assert(0);
  114. case PIPE_BUFFER:
  115. case PIPE_TEXTURE_1D:
  116. case PIPE_TEXTURE_1D_ARRAY:
  117. return A3XX_TEX_1D;
  118. case PIPE_TEXTURE_RECT:
  119. case PIPE_TEXTURE_2D:
  120. case PIPE_TEXTURE_2D_ARRAY:
  121. return A3XX_TEX_2D;
  122. case PIPE_TEXTURE_3D:
  123. return A3XX_TEX_3D;
  124. case PIPE_TEXTURE_CUBE:
  125. case PIPE_TEXTURE_CUBE_ARRAY:
  126. return A3XX_TEX_CUBE;
  127. }
  128. }
  129. static struct pipe_sampler_view *
  130. fd3_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
  131. const struct pipe_sampler_view *cso)
  132. {
  133. struct fd3_pipe_sampler_view *so = CALLOC_STRUCT(fd3_pipe_sampler_view);
  134. struct fd_resource *rsc = fd_resource(prsc);
  135. unsigned lvl = cso->u.tex.first_level;
  136. unsigned miplevels = cso->u.tex.last_level - lvl;
  137. if (!so)
  138. return NULL;
  139. so->base = *cso;
  140. pipe_reference(NULL, &prsc->reference);
  141. so->base.texture = prsc;
  142. so->base.reference.count = 1;
  143. so->base.context = pctx;
  144. so->tex_resource = rsc;
  145. so->texconst0 =
  146. A3XX_TEX_CONST_0_TYPE(tex_type(prsc->target)) |
  147. A3XX_TEX_CONST_0_FMT(fd3_pipe2tex(cso->format)) |
  148. A3XX_TEX_CONST_0_MIPLVLS(miplevels) |
  149. fd3_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
  150. cso->swizzle_b, cso->swizzle_a);
  151. if (util_format_is_srgb(cso->format))
  152. so->texconst0 |= A3XX_TEX_CONST_0_SRGB;
  153. so->texconst1 =
  154. A3XX_TEX_CONST_1_FETCHSIZE(fd3_pipe2fetchsize(cso->format)) |
  155. A3XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
  156. A3XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
  157. /* when emitted, A3XX_TEX_CONST_2_INDX() must be OR'd in: */
  158. so->texconst2 =
  159. A3XX_TEX_CONST_2_PITCH(rsc->slices[lvl].pitch * rsc->cpp);
  160. switch (prsc->target) {
  161. case PIPE_TEXTURE_1D_ARRAY:
  162. case PIPE_TEXTURE_2D_ARRAY:
  163. so->texconst3 =
  164. A3XX_TEX_CONST_3_DEPTH(prsc->array_size - 1) |
  165. A3XX_TEX_CONST_3_LAYERSZ1(rsc->slices[0].size0) |
  166. A3XX_TEX_CONST_3_LAYERSZ2(rsc->slices[0].size0);
  167. break;
  168. case PIPE_TEXTURE_3D:
  169. so->texconst3 =
  170. A3XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |
  171. A3XX_TEX_CONST_3_LAYERSZ1(rsc->slices[0].size0) |
  172. A3XX_TEX_CONST_3_LAYERSZ2(rsc->slices[0].size0);
  173. break;
  174. default:
  175. so->texconst3 = 0x00000000;
  176. break;
  177. }
  178. return &so->base;
  179. }
  180. void
  181. fd3_texture_init(struct pipe_context *pctx)
  182. {
  183. pctx->create_sampler_state = fd3_sampler_state_create;
  184. pctx->create_sampler_view = fd3_sampler_view_create;
  185. }