Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
  2.  
  3. /*
  4.  * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice (including the next
  14.  * paragraph) shall be included in all copies or substantial portions of the
  15.  * Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  * Authors:
  26.  *    Rob Clark <robclark@freedesktop.org>
  27.  */
  28.  
  29. #include "pipe/p_state.h"
  30. #include "util/u_string.h"
  31. #include "util/u_memory.h"
  32. #include "util/u_inlines.h"
  33. #include "util/u_format.h"
  34.  
  35. #include "fd4_texture.h"
  36. #include "fd4_format.h"
  37.  
  38. /* TODO do we need to emulate clamp-to-edge like a3xx? */
  39. static enum a4xx_tex_clamp
  40. tex_clamp(unsigned wrap)
  41. {
  42.         /* hardware probably supports more, but we can't coax all the
  43.          * wrap/clamp modes out of the GLESv2 blob driver.
  44.          *
  45.          * TODO once we have basics working, go back and just try
  46.          * different values and see what happens
  47.          */
  48.         switch (wrap) {
  49.         case PIPE_TEX_WRAP_REPEAT:
  50.                 return A4XX_TEX_REPEAT;
  51.         case PIPE_TEX_WRAP_CLAMP:
  52.         case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
  53.                 return A4XX_TEX_CLAMP_TO_EDGE;
  54.         case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
  55. // TODO
  56. //              return A4XX_TEX_CLAMP_TO_BORDER;
  57.         case PIPE_TEX_WRAP_MIRROR_CLAMP:
  58.         case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
  59.         case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
  60. // TODO
  61. //              return A4XX_TEX_MIRROR_CLAMP;
  62.         case PIPE_TEX_WRAP_MIRROR_REPEAT:
  63.                 return A4XX_TEX_MIRROR_REPEAT;
  64.         default:
  65.                 DBG("invalid wrap: %u", wrap);
  66.                 return 0;
  67.         }
  68. }
  69.  
  70. static enum a4xx_tex_filter
  71. tex_filter(unsigned filter, bool aniso)
  72. {
  73.         switch (filter) {
  74.         case PIPE_TEX_FILTER_NEAREST:
  75.                 return A4XX_TEX_NEAREST;
  76.         case PIPE_TEX_FILTER_LINEAR:
  77.                 return aniso ? A4XX_TEX_ANISO : A4XX_TEX_LINEAR;
  78.         default:
  79.                 DBG("invalid filter: %u", filter);
  80.                 return 0;
  81.         }
  82. }
  83.  
  84. static void *
  85. fd4_sampler_state_create(struct pipe_context *pctx,
  86.                 const struct pipe_sampler_state *cso)
  87. {
  88.         struct fd4_sampler_stateobj *so = CALLOC_STRUCT(fd4_sampler_stateobj);
  89.         unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));
  90.         bool miplinear = false;
  91.  
  92.         if (!so)
  93.                 return NULL;
  94.  
  95.         if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
  96.                 miplinear = true;
  97.  
  98.         so->base = *cso;
  99.  
  100.         so->texsamp0 =
  101.                 COND(miplinear, A4XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
  102.                 A4XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
  103.                 A4XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
  104.                 A4XX_TEX_SAMP_0_ANISO(aniso) |
  105.                 A4XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s)) |
  106.                 A4XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t)) |
  107.                 A4XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r));
  108.  
  109.         so->texsamp1 =
  110. //              COND(miplinear, A4XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR) |
  111.                 COND(!cso->normalized_coords, A4XX_TEX_SAMP_1_UNNORM_COORDS);
  112.  
  113.         if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
  114.                 so->texsamp1 |=
  115.                         A4XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
  116.                         A4XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
  117.         }
  118.  
  119.         if (cso->compare_mode)
  120.                 so->texsamp1 |= A4XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
  121.  
  122.         return so;
  123. }
  124.  
  125. static enum a4xx_tex_type
  126. tex_type(unsigned target)
  127. {
  128.         switch (target) {
  129.         default:
  130.                 assert(0);
  131.         case PIPE_BUFFER:
  132.         case PIPE_TEXTURE_1D:
  133.         case PIPE_TEXTURE_1D_ARRAY:
  134.                 return A4XX_TEX_1D;
  135.         case PIPE_TEXTURE_RECT:
  136.         case PIPE_TEXTURE_2D:
  137.         case PIPE_TEXTURE_2D_ARRAY:
  138.                 return A4XX_TEX_2D;
  139.         case PIPE_TEXTURE_3D:
  140.                 return A4XX_TEX_3D;
  141.         case PIPE_TEXTURE_CUBE:
  142.         case PIPE_TEXTURE_CUBE_ARRAY:
  143.                 return A4XX_TEX_CUBE;
  144.         }
  145. }
  146.  
  147. static struct pipe_sampler_view *
  148. fd4_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
  149.                 const struct pipe_sampler_view *cso)
  150. {
  151.         struct fd4_pipe_sampler_view *so = CALLOC_STRUCT(fd4_pipe_sampler_view);
  152.         struct fd_resource *rsc = fd_resource(prsc);
  153.         unsigned lvl = cso->u.tex.first_level;
  154.         unsigned miplevels = cso->u.tex.last_level - lvl;
  155.  
  156.         if (!so)
  157.                 return NULL;
  158.  
  159.         so->base = *cso;
  160.         pipe_reference(NULL, &prsc->reference);
  161.         so->base.texture = prsc;
  162.         so->base.reference.count = 1;
  163.         so->base.context = pctx;
  164.  
  165.         so->texconst0 =
  166.                 A4XX_TEX_CONST_0_TYPE(tex_type(prsc->target)) |
  167.                 A4XX_TEX_CONST_0_FMT(fd4_pipe2tex(cso->format)) |
  168.                 A4XX_TEX_CONST_0_MIPLVLS(miplevels) |
  169.                 fd4_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
  170.                                 cso->swizzle_b, cso->swizzle_a);
  171.  
  172.         if (util_format_is_srgb(cso->format))
  173.                 so->texconst0 |= A4XX_TEX_CONST_0_SRGB;
  174.  
  175.         so->texconst1 =
  176.                 A4XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
  177.                 A4XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
  178.         so->texconst2 =
  179.                 A4XX_TEX_CONST_2_FETCHSIZE(fd4_pipe2fetchsize(cso->format)) |
  180.                 A4XX_TEX_CONST_2_PITCH(rsc->slices[lvl].pitch * rsc->cpp);
  181.  
  182.         switch (prsc->target) {
  183.         case PIPE_TEXTURE_1D_ARRAY:
  184.         case PIPE_TEXTURE_2D_ARRAY:
  185.                 so->texconst3 =
  186.                         A4XX_TEX_CONST_3_DEPTH(prsc->array_size) |
  187.                         A4XX_TEX_CONST_3_LAYERSZ(rsc->layer_size);
  188.                 break;
  189.         case PIPE_TEXTURE_CUBE:
  190.         case PIPE_TEXTURE_CUBE_ARRAY:  /* ?? not sure about _CUBE_ARRAY */
  191.                 so->texconst3 =
  192.                         A4XX_TEX_CONST_3_DEPTH(1) |
  193.                         A4XX_TEX_CONST_3_LAYERSZ(rsc->layer_size);
  194.                 break;
  195.         case PIPE_TEXTURE_3D:
  196.                 so->texconst3 =
  197.                         A4XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |
  198.                         A4XX_TEX_CONST_3_LAYERSZ(rsc->slices[0].size0);
  199.                 break;
  200.         default:
  201.                 so->texconst3 = 0x00000000;
  202.                 break;
  203.         }
  204.  
  205.         return &so->base;
  206. }
  207.  
  208. static void
  209. fd4_set_sampler_views(struct pipe_context *pctx, unsigned shader,
  210.                 unsigned start, unsigned nr, struct pipe_sampler_view **views)
  211. {
  212.         struct fd_context *ctx = fd_context(pctx);
  213.         struct fd4_context *fd4_ctx = fd4_context(ctx);
  214.         struct fd_texture_stateobj *tex;
  215.         uint16_t integer_s = 0, *ptr;
  216.         int i;
  217.  
  218.         fd_set_sampler_views(pctx, shader, start, nr, views);
  219.  
  220.         switch (shader) {
  221.         case PIPE_SHADER_FRAGMENT:
  222.                 tex = &ctx->fragtex;
  223.                 ptr = &fd4_ctx->finteger_s;
  224.                 break;
  225.         case PIPE_SHADER_VERTEX:
  226.                 tex = &ctx->verttex;
  227.                 ptr = &fd4_ctx->vinteger_s;
  228.                 break;
  229.         default:
  230.                 return;
  231.         }
  232.  
  233.         for (i = 0; i < tex->num_textures; i++)
  234.                 if (util_format_is_pure_integer(tex->textures[i]->format))
  235.                         integer_s |= 1 << i;
  236.  
  237.         *ptr = integer_s;
  238. }
  239.  
  240. void
  241. fd4_texture_init(struct pipe_context *pctx)
  242. {
  243.         pctx->create_sampler_state = fd4_sampler_state_create;
  244.         pctx->bind_sampler_states = fd_sampler_states_bind;
  245.         pctx->create_sampler_view = fd4_sampler_view_create;
  246.         pctx->set_sampler_views = fd4_set_sampler_views;
  247. }
  248.