Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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.  
  28. /* Authors:
  29.  *  Brian Paul
  30.  */
  31.  
  32. #include "util/u_memory.h"
  33. #include "util/u_inlines.h"
  34.  
  35. #include "draw/draw_context.h"
  36.  
  37. #include "sp_context.h"
  38. #include "sp_state.h"
  39. #include "sp_texture.h"
  40. #include "sp_tex_sample.h"
  41. #include "sp_tex_tile_cache.h"
  42.  
  43.  
  44. /**
  45.  * Bind a range [start, start+num-1] of samplers for a shader stage.
  46.  */
  47. static void
  48. softpipe_bind_sampler_states(struct pipe_context *pipe,
  49.                              unsigned shader,
  50.                              unsigned start,
  51.                              unsigned num,
  52.                              void **samplers)
  53. {
  54.    struct softpipe_context *softpipe = softpipe_context(pipe);
  55.    unsigned i;
  56.  
  57.    assert(shader < PIPE_SHADER_TYPES);
  58.    assert(start + num <= Elements(softpipe->samplers[shader]));
  59.  
  60.    /* Check for no-op */
  61.    if (start + num <= softpipe->num_samplers[shader] &&
  62.        !memcmp(softpipe->samplers[shader] + start, samplers,
  63.                num * sizeof(void *))) {
  64.       return;
  65.    }
  66.  
  67.    draw_flush(softpipe->draw);
  68.  
  69.    /* set the new samplers */
  70.    for (i = 0; i < num; i++) {
  71.       softpipe->samplers[shader][start + i] = samplers[i];
  72.    }
  73.  
  74.    /* find highest non-null samplers[] entry */
  75.    {
  76.       unsigned j = MAX2(softpipe->num_samplers[shader], start + num);
  77.       while (j > 0 && softpipe->samplers[shader][j - 1] == NULL)
  78.          j--;
  79.       softpipe->num_samplers[shader] = j;
  80.    }
  81.  
  82.    if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
  83.       draw_set_samplers(softpipe->draw,
  84.                         shader,
  85.                         softpipe->samplers[shader],
  86.                         softpipe->num_samplers[shader]);
  87.    }
  88.  
  89.    softpipe->dirty |= SP_NEW_SAMPLER;
  90. }
  91.  
  92.  
  93.  
  94. static void
  95. softpipe_bind_fragment_sampler_states(struct pipe_context *pipe,
  96.                                       unsigned num, void **samplers)
  97. {
  98.    softpipe_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, num, samplers);
  99. }
  100.  
  101.  
  102. static void
  103. softpipe_bind_vertex_sampler_states(struct pipe_context *pipe,
  104.                                     unsigned num,
  105.                                     void **samplers)
  106. {
  107.    softpipe_bind_sampler_states(pipe, PIPE_SHADER_VERTEX, 0, num, samplers);
  108. }
  109.  
  110.  
  111. static void
  112. softpipe_bind_geometry_sampler_states(struct pipe_context *pipe,
  113.                                       unsigned num,
  114.                                       void **samplers)
  115. {
  116.    softpipe_bind_sampler_states(pipe, PIPE_SHADER_GEOMETRY, 0, num, samplers);
  117. }
  118.  
  119.  
  120.  
  121. static void
  122. softpipe_sampler_view_destroy(struct pipe_context *pipe,
  123.                               struct pipe_sampler_view *view)
  124. {
  125.    pipe_resource_reference(&view->texture, NULL);
  126.    FREE(view);
  127. }
  128.  
  129.  
  130. void
  131. softpipe_set_sampler_views(struct pipe_context *pipe,
  132.                            unsigned shader,
  133.                            unsigned start,
  134.                            unsigned num,
  135.                            struct pipe_sampler_view **views)
  136. {
  137.    struct softpipe_context *softpipe = softpipe_context(pipe);
  138.    uint i;
  139.  
  140.    assert(shader < PIPE_SHADER_TYPES);
  141.    assert(start + num <= Elements(softpipe->sampler_views[shader]));
  142.  
  143.    /* Check for no-op */
  144.    if (start + num <= softpipe->num_sampler_views[shader] &&
  145.        !memcmp(softpipe->sampler_views[shader] + start, views,
  146.                num * sizeof(struct pipe_sampler_view *))) {
  147.       return;
  148.    }
  149.  
  150.    draw_flush(softpipe->draw);
  151.  
  152.    /* set the new sampler views */
  153.    for (i = 0; i < num; i++) {
  154.       struct sp_sampler_view *sp_sviewsrc;
  155.       struct sp_sampler_view *sp_sviewdst =
  156.          &softpipe->tgsi.sampler[shader]->sp_sview[start + i];
  157.       struct pipe_sampler_view **pview = &softpipe->sampler_views[shader][start + i];
  158.       pipe_sampler_view_reference(pview, views[i]);
  159.       sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[shader][start + i],
  160.                                          views[i]);
  161.       /*
  162.        * We don't really have variants, however some bits are different per shader,
  163.        * so just copy?
  164.        */
  165.       sp_sviewsrc = (struct sp_sampler_view *)*pview;
  166.       if (sp_sviewsrc) {
  167.          memcpy(sp_sviewdst, sp_sviewsrc, sizeof(*sp_sviewsrc));
  168.          sp_sviewdst->compute_lambda = softpipe_get_lambda_func(&sp_sviewdst->base, shader);
  169.          sp_sviewdst->cache = softpipe->tex_cache[shader][start + i];
  170.       }
  171.       else {
  172.          memset(sp_sviewdst, 0,  sizeof(*sp_sviewsrc));
  173.       }
  174.    }
  175.  
  176.  
  177.    /* find highest non-null sampler_views[] entry */
  178.    {
  179.       unsigned j = MAX2(softpipe->num_sampler_views[shader], start + num);
  180.       while (j > 0 && softpipe->sampler_views[shader][j - 1] == NULL)
  181.          j--;
  182.       softpipe->num_sampler_views[shader] = j;
  183.    }
  184.  
  185.    if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
  186.       draw_set_sampler_views(softpipe->draw,
  187.                              shader,
  188.                              softpipe->sampler_views[shader],
  189.                              softpipe->num_sampler_views[shader]);
  190.    }
  191.  
  192.    softpipe->dirty |= SP_NEW_TEXTURE;
  193. }
  194.  
  195.  
  196. static void
  197. softpipe_set_fragment_sampler_views(struct pipe_context *pipe,
  198.                                     unsigned num,
  199.                                     struct pipe_sampler_view **views)
  200. {
  201.    softpipe_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, num, views);
  202. }
  203.  
  204.  
  205. static void
  206. softpipe_set_vertex_sampler_views(struct pipe_context *pipe,
  207.                                   unsigned num,
  208.                                   struct pipe_sampler_view **views)
  209. {
  210.    softpipe_set_sampler_views(pipe, PIPE_SHADER_VERTEX, 0, num, views);
  211. }
  212.  
  213.  
  214. static void
  215. softpipe_set_geometry_sampler_views(struct pipe_context *pipe,
  216.                                     unsigned num,
  217.                                     struct pipe_sampler_view **views)
  218. {
  219.    softpipe_set_sampler_views(pipe, PIPE_SHADER_GEOMETRY, 0, num, views);
  220. }
  221.  
  222.  
  223. static void
  224. softpipe_delete_sampler_state(struct pipe_context *pipe,
  225.                               void *sampler)
  226. {
  227.    FREE( sampler );
  228. }
  229.  
  230.  
  231. void
  232. softpipe_init_sampler_funcs(struct pipe_context *pipe)
  233. {
  234.    pipe->create_sampler_state = softpipe_create_sampler_state;
  235.    pipe->bind_fragment_sampler_states  = softpipe_bind_fragment_sampler_states;
  236.    pipe->bind_vertex_sampler_states = softpipe_bind_vertex_sampler_states;
  237.    pipe->bind_geometry_sampler_states = softpipe_bind_geometry_sampler_states;
  238.    pipe->delete_sampler_state = softpipe_delete_sampler_state;
  239.  
  240.    pipe->set_fragment_sampler_views = softpipe_set_fragment_sampler_views;
  241.    pipe->set_vertex_sampler_views = softpipe_set_vertex_sampler_views;
  242.    pipe->set_geometry_sampler_views = softpipe_set_geometry_sampler_views;
  243.  
  244.    pipe->create_sampler_view = softpipe_create_sampler_view;
  245.    pipe->sampler_view_destroy = softpipe_sampler_view_destroy;
  246. }
  247.  
  248.