Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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.  
  26. #include "util/u_inlines.h"
  27. #include "pipe/p_defines.h"
  28. #include "util/u_format.h"
  29. #include "util/u_math.h"
  30. #include "util/u_memory.h"
  31. #include "util/u_bitmask.h"
  32. #include "translate/translate.h"
  33. #include "tgsi/tgsi_ureg.h"
  34.  
  35. #include "svga_context.h"
  36. #include "svga_state.h"
  37. #include "svga_cmd.h"
  38. #include "svga_shader.h"
  39. #include "svga_tgsi.h"
  40.  
  41. #include "svga_hw_reg.h"
  42.  
  43.  
  44. static INLINE int
  45. compare_vs_keys(const struct svga_vs_compile_key *a,
  46.                 const struct svga_vs_compile_key *b)
  47. {
  48.    unsigned keysize = svga_vs_key_size( a );
  49.    return memcmp( a, b, keysize );
  50. }
  51.  
  52.  
  53. /** Search for a vertex shader variant */
  54. static struct svga_shader_variant *
  55. search_vs_key(const struct svga_vertex_shader *vs,
  56.               const struct svga_vs_compile_key *key)
  57. {
  58.    struct svga_shader_variant *variant = vs->base.variants;
  59.  
  60.    assert(key);
  61.  
  62.    for ( ; variant; variant = variant->next) {
  63.       if (compare_vs_keys( key, &variant->key.vkey ) == 0)
  64.          return variant;
  65.    }
  66.    
  67.    return NULL;
  68. }
  69.  
  70.  
  71. /**
  72.  * If we fail to compile a vertex shader we'll use a dummy/fallback shader
  73.  * that simply emits a (0,0,0,1) vertex position.
  74.  */
  75. static const struct tgsi_token *
  76. get_dummy_vertex_shader(void)
  77. {
  78.    static const float zero[4] = { 0.0, 0.0, 0.0, 1.0 };
  79.    struct ureg_program *ureg;
  80.    const struct tgsi_token *tokens;
  81.    struct ureg_src src;
  82.    struct ureg_dst dst;
  83.    unsigned num_tokens;
  84.  
  85.    ureg = ureg_create(TGSI_PROCESSOR_VERTEX);
  86.    if (!ureg)
  87.       return NULL;
  88.  
  89.    dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
  90.    src = ureg_DECL_immediate(ureg, zero, 4);
  91.    ureg_MOV(ureg, dst, src);
  92.    ureg_END(ureg);
  93.  
  94.    tokens = ureg_get_tokens(ureg, &num_tokens);
  95.  
  96.    ureg_destroy(ureg);
  97.  
  98.    return tokens;
  99. }
  100.  
  101.  
  102. /**
  103.  * Replace the given shader's instruction with a simple / dummy shader.
  104.  * We use this when normal shader translation fails.
  105.  */
  106. static struct svga_shader_variant *
  107. get_compiled_dummy_vertex_shader(struct svga_vertex_shader *vs,
  108.                                  const struct svga_vs_compile_key *key)
  109. {
  110.    const struct tgsi_token *dummy = get_dummy_vertex_shader();
  111.    struct svga_shader_variant *variant;
  112.  
  113.    if (!dummy) {
  114.       return NULL;
  115.    }
  116.  
  117.    FREE((void *) vs->base.tokens);
  118.    vs->base.tokens = dummy;
  119.  
  120.    variant = svga_translate_vertex_program(vs, key);
  121.    return variant;
  122. }
  123.  
  124.  
  125. /**
  126.  * Translate TGSI shader into an svga shader variant.
  127.  */
  128. static enum pipe_error
  129. compile_vs(struct svga_context *svga,
  130.            struct svga_vertex_shader *vs,
  131.            const struct svga_vs_compile_key *key,
  132.            struct svga_shader_variant **out_variant)
  133. {
  134.    struct svga_shader_variant *variant;
  135.    enum pipe_error ret = PIPE_ERROR;
  136.  
  137.    variant = svga_translate_vertex_program( vs, key );
  138.    if (variant == NULL) {
  139.       /* some problem during translation, try the dummy shader */
  140.       variant = get_compiled_dummy_vertex_shader(vs, key);
  141.       if (!variant) {
  142.          ret = PIPE_ERROR;
  143.          goto fail;
  144.       }
  145.    }
  146.  
  147.    if (svga_shader_too_large(svga, variant)) {
  148.       /* too big, use dummy shader */
  149.       debug_printf("Shader too large (%lu bytes),"
  150.                    " using dummy shader instead.\n",
  151.                    (unsigned long ) variant->nr_tokens
  152.                    * sizeof(variant->tokens[0]));
  153.       variant = get_compiled_dummy_vertex_shader(vs, key);
  154.       if (!variant) {
  155.          ret = PIPE_ERROR;
  156.          goto fail;
  157.       }
  158.    }
  159.  
  160.    ret = svga_define_shader(svga, SVGA3D_SHADERTYPE_VS, variant);
  161.    if (ret != PIPE_OK)
  162.       goto fail;
  163.  
  164.    *out_variant = variant;
  165.  
  166.    /* insert variants at head of linked list */
  167.    variant->next = vs->base.variants;
  168.    vs->base.variants = variant;
  169.  
  170.    return PIPE_OK;
  171.  
  172. fail:
  173.    if (variant) {
  174.       svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_VS, variant);
  175.    }
  176.    return ret;
  177. }
  178.  
  179. /* SVGA_NEW_PRESCALE, SVGA_NEW_RAST, SVGA_NEW_FS
  180.  */
  181. static void
  182. make_vs_key(struct svga_context *svga, struct svga_vs_compile_key *key)
  183. {
  184.    memset(key, 0, sizeof *key);
  185.    key->need_prescale = svga->state.hw_clear.prescale.enabled;
  186.    key->allow_psiz = svga->curr.rast->templ.point_size_per_vertex;
  187.  
  188.    /* SVGA_NEW_FS */
  189.    key->fs_generic_inputs = svga->curr.fs->generic_inputs;
  190.  
  191.    /* SVGA_NEW_VELEMENT */
  192.    key->adjust_attrib_range = svga->curr.velems->adjust_attrib_range;
  193.    key->adjust_attrib_w_1 = svga->curr.velems->adjust_attrib_w_1;
  194. }
  195.  
  196.  
  197. /**
  198.  * svga_reemit_vs_bindings - Reemit the vertex shader bindings
  199.  */
  200. enum pipe_error
  201. svga_reemit_vs_bindings(struct svga_context *svga)
  202. {
  203.    enum pipe_error ret;
  204.    struct svga_winsys_gb_shader *gbshader =
  205.       svga->state.hw_draw.vs ? svga->state.hw_draw.vs->gb_shader : NULL;
  206.  
  207.    assert(svga->rebind.vs);
  208.    assert(svga_have_gb_objects(svga));
  209.  
  210.    ret = SVGA3D_SetGBShader(svga->swc, SVGA3D_SHADERTYPE_VS, gbshader);
  211.    if (ret != PIPE_OK)
  212.       return ret;
  213.  
  214.    svga->rebind.vs = FALSE;
  215.    return PIPE_OK;
  216. }
  217.  
  218.  
  219. static enum pipe_error
  220. emit_hw_vs(struct svga_context *svga, unsigned dirty)
  221. {
  222.    struct svga_shader_variant *variant = NULL;
  223.    enum pipe_error ret = PIPE_OK;
  224.  
  225.    /* SVGA_NEW_NEED_SWTNL */
  226.    if (!svga->state.sw.need_swtnl) {
  227.       struct svga_vertex_shader *vs = svga->curr.vs;
  228.       struct svga_vs_compile_key key;
  229.  
  230.       make_vs_key( svga, &key );
  231.  
  232.       variant = search_vs_key( vs, &key );
  233.       if (!variant) {
  234.          ret = compile_vs( svga, vs, &key, &variant );
  235.          if (ret != PIPE_OK)
  236.             return ret;
  237.       }
  238.  
  239.       assert(variant);
  240.    }
  241.  
  242.    if (variant != svga->state.hw_draw.vs) {
  243.       if (svga_have_gb_objects(svga)) {
  244.          struct svga_winsys_gb_shader *gbshader =
  245.             variant ? variant->gb_shader : NULL;
  246.          ret = SVGA3D_SetGBShader(svga->swc, SVGA3D_SHADERTYPE_VS, gbshader);
  247.          if (ret != PIPE_OK)
  248.             return ret;
  249.  
  250.          svga->rebind.vs = FALSE;
  251.       }
  252.       else {
  253.          unsigned id = variant ? variant->id : SVGA_ID_INVALID;
  254.          ret = SVGA3D_SetShader(svga->swc, SVGA3D_SHADERTYPE_VS, id);
  255.          if (ret != PIPE_OK)
  256.             return ret;
  257.       }
  258.  
  259.       svga->dirty |= SVGA_NEW_VS_VARIANT;
  260.       svga->state.hw_draw.vs = variant;      
  261.    }
  262.  
  263.    return PIPE_OK;
  264. }
  265.  
  266. struct svga_tracked_state svga_hw_vs =
  267. {
  268.    "vertex shader (hwtnl)",
  269.    (SVGA_NEW_VS |
  270.     SVGA_NEW_FS |
  271.     SVGA_NEW_PRESCALE |
  272.     SVGA_NEW_VELEMENT |
  273.     SVGA_NEW_NEED_SWTNL),
  274.    emit_hw_vs
  275. };
  276.