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 "draw/draw_context.h"
  27. #include "draw/draw_vbuf.h"
  28. #include "util/u_inlines.h"
  29. #include "pipe/p_state.h"
  30.  
  31. #include "svga_context.h"
  32. #include "svga_swtnl.h"
  33. #include "svga_state.h"
  34. #include "svga_tgsi.h"
  35. #include "svga_swtnl_private.h"
  36.  
  37.  
  38. #define SVGA_POINT_ADJ_X -0.375f
  39. #define SVGA_POINT_ADJ_Y -0.5f
  40.  
  41. #define SVGA_LINE_ADJ_X -0.5f
  42. #define SVGA_LINE_ADJ_Y -0.5f
  43.  
  44. #define SVGA_TRIANGLE_ADJ_X -0.375f
  45. #define SVGA_TRIANGLE_ADJ_Y -0.5f
  46.  
  47.  
  48. static void set_draw_viewport( struct svga_context *svga )
  49. {
  50.    struct pipe_viewport_state vp = svga->curr.viewport;
  51.    float adjx = 0.0f;
  52.    float adjy = 0.0f;
  53.  
  54.    switch (svga->curr.reduced_prim) {
  55.    case PIPE_PRIM_POINTS:
  56.       adjx = SVGA_POINT_ADJ_X;
  57.       adjy = SVGA_POINT_ADJ_Y;
  58.       break;
  59.    case PIPE_PRIM_LINES:
  60.       /* XXX: This is to compensate for the fact that wide lines are
  61.        * going to be drawn with triangles, but we're not catching all
  62.        * cases where that will happen.
  63.        */
  64.       if (svga->curr.rast->need_pipeline & SVGA_PIPELINE_FLAG_LINES)
  65.       {
  66.          adjx = SVGA_LINE_ADJ_X + 0.175f;
  67.          adjy = SVGA_LINE_ADJ_Y - 0.175f;
  68.       }
  69.       else {
  70.          adjx = SVGA_LINE_ADJ_X;
  71.          adjy = SVGA_LINE_ADJ_Y;
  72.       }
  73.       break;
  74.    case PIPE_PRIM_TRIANGLES:
  75.       adjx += SVGA_TRIANGLE_ADJ_X;
  76.       adjy += SVGA_TRIANGLE_ADJ_Y;
  77.       break;
  78.    }
  79.  
  80.    vp.translate[0] += adjx;
  81.    vp.translate[1] += adjy;
  82.  
  83.    draw_set_viewport_states(svga->swtnl.draw, 0, 1, &vp);
  84. }
  85.  
  86. static enum pipe_error
  87. update_swtnl_draw( struct svga_context *svga,
  88.                    unsigned dirty )
  89. {
  90.    draw_flush( svga->swtnl.draw );
  91.  
  92.    if (dirty & SVGA_NEW_VS)
  93.       draw_bind_vertex_shader(svga->swtnl.draw,
  94.                               svga->curr.vs->draw_shader);
  95.  
  96.    if (dirty & SVGA_NEW_FS)
  97.       draw_bind_fragment_shader(svga->swtnl.draw,
  98.                                 svga->curr.fs->draw_shader);
  99.  
  100.    if (dirty & SVGA_NEW_VBUFFER)
  101.       draw_set_vertex_buffers(svga->swtnl.draw, 0,
  102.                               svga->curr.num_vertex_buffers,
  103.                               svga->curr.vb);
  104.  
  105.    if (dirty & SVGA_NEW_VELEMENT)
  106.       draw_set_vertex_elements(svga->swtnl.draw,
  107.                                svga->curr.velems->count,
  108.                                svga->curr.velems->velem );
  109.  
  110.    if (dirty & SVGA_NEW_CLIP)
  111.       draw_set_clip_state(svga->swtnl.draw,
  112.                           &svga->curr.clip);
  113.  
  114.    if (dirty & (SVGA_NEW_VIEWPORT |
  115.                 SVGA_NEW_REDUCED_PRIMITIVE |
  116.                 SVGA_NEW_RAST))
  117.       set_draw_viewport( svga );
  118.  
  119.    if (dirty & SVGA_NEW_RAST)
  120.       draw_set_rasterizer_state(svga->swtnl.draw,
  121.                                 &svga->curr.rast->templ,
  122.                                 (void *) svga->curr.rast);
  123.  
  124.    /* Tell the draw module how deep the Z/depth buffer is.
  125.     *
  126.     * If no depth buffer is bound, send the utility function the
  127.     * format for no bound depth (PIPE_FORMAT_NONE).
  128.     */
  129.    if (dirty & SVGA_NEW_FRAME_BUFFER)
  130.       draw_set_zs_format(svga->swtnl.draw,
  131.          (svga->curr.framebuffer.zsbuf) ?
  132.              svga->curr.framebuffer.zsbuf->format : PIPE_FORMAT_NONE);
  133.  
  134.    return PIPE_OK;
  135. }
  136.  
  137.  
  138. struct svga_tracked_state svga_update_swtnl_draw =
  139. {
  140.    "update draw module state",
  141.    (SVGA_NEW_VS |
  142.     SVGA_NEW_VBUFFER |
  143.     SVGA_NEW_VELEMENT |
  144.     SVGA_NEW_CLIP |
  145.     SVGA_NEW_VIEWPORT |
  146.     SVGA_NEW_RAST |
  147.     SVGA_NEW_FRAME_BUFFER |
  148.     SVGA_NEW_REDUCED_PRIMITIVE),
  149.    update_swtnl_draw
  150. };
  151.  
  152.  
  153. enum pipe_error
  154. svga_swtnl_update_vdecl( struct svga_context *svga )
  155. {
  156.    struct svga_vbuf_render *svga_render = svga_vbuf_render(svga->swtnl.backend);
  157.    struct draw_context *draw = svga->swtnl.draw;
  158.    struct vertex_info *vinfo = &svga_render->vertex_info;
  159.    SVGA3dVertexDecl vdecl[PIPE_MAX_ATTRIBS];
  160.    const enum interp_mode colorInterp =
  161.       svga->curr.rast->templ.flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
  162.    struct svga_fragment_shader *fs = svga->curr.fs;
  163.    int offset = 0;
  164.    int nr_decls = 0;
  165.    int src;
  166.    unsigned i;
  167.  
  168.    memset(vinfo, 0, sizeof(*vinfo));
  169.    memset(vdecl, 0, sizeof(vdecl));
  170.  
  171.    draw_prepare_shader_outputs(draw);
  172.    /* always add position */
  173.    src = draw_find_shader_output(draw, TGSI_SEMANTIC_POSITION, 0);
  174.    draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_LINEAR, src);
  175.    vinfo->attrib[0].emit = EMIT_4F;
  176.    vdecl[0].array.offset = offset;
  177.    vdecl[0].identity.type = SVGA3D_DECLTYPE_FLOAT4;
  178.    vdecl[0].identity.usage = SVGA3D_DECLUSAGE_POSITIONT;
  179.    vdecl[0].identity.usageIndex = 0;
  180.    offset += 16;
  181.    nr_decls++;
  182.  
  183.    for (i = 0; i < fs->base.info.num_inputs; i++) {
  184.       const unsigned sem_name = fs->base.info.input_semantic_name[i];
  185.       const unsigned sem_index = fs->base.info.input_semantic_index[i];
  186.  
  187.       src = draw_find_shader_output(draw, sem_name, sem_index);
  188.  
  189.       vdecl[nr_decls].array.offset = offset;
  190.       vdecl[nr_decls].identity.usageIndex = sem_index;
  191.  
  192.       switch (sem_name) {
  193.       case TGSI_SEMANTIC_COLOR:
  194.          draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
  195.          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_COLOR;
  196.          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT4;
  197.          offset += 16;
  198.          nr_decls++;
  199.          break;
  200.       case TGSI_SEMANTIC_GENERIC:
  201.          draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
  202.          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_TEXCOORD;
  203.          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT4;
  204.          vdecl[nr_decls].identity.usageIndex =
  205.             svga_remap_generic_index(fs->generic_remap_table, sem_index);
  206.          offset += 16;
  207.          nr_decls++;
  208.          break;
  209.       case TGSI_SEMANTIC_FOG:
  210.          draw_emit_vertex_attr(vinfo, EMIT_1F, INTERP_PERSPECTIVE, src);
  211.          vdecl[nr_decls].identity.usage = SVGA3D_DECLUSAGE_TEXCOORD;
  212.          vdecl[nr_decls].identity.type = SVGA3D_DECLTYPE_FLOAT1;
  213.          assert(vdecl[nr_decls].identity.usageIndex == 0);
  214.          offset += 4;
  215.          nr_decls++;
  216.          break;
  217.       case TGSI_SEMANTIC_POSITION:
  218.          /* generated internally, not a vertex shader output */
  219.          break;
  220.       default:
  221.          assert(0);
  222.       }
  223.    }
  224.  
  225.    draw_compute_vertex_size(vinfo);
  226.  
  227.    svga_render->vdecl_count = nr_decls;
  228.    for (i = 0; i < svga_render->vdecl_count; i++)
  229.       vdecl[i].array.stride = offset;
  230.  
  231.    if (memcmp(svga_render->vdecl, vdecl, sizeof(vdecl)) == 0)
  232.       return PIPE_OK;
  233.  
  234.    memcpy(svga_render->vdecl, vdecl, sizeof(vdecl));
  235.    svga->swtnl.new_vdecl = TRUE;
  236.  
  237.    return PIPE_OK;
  238. }
  239.  
  240.  
  241. static enum pipe_error
  242. update_swtnl_vdecl( struct svga_context *svga,
  243.                     unsigned dirty )
  244. {
  245.    return svga_swtnl_update_vdecl( svga );
  246. }
  247.  
  248.  
  249. struct svga_tracked_state svga_update_swtnl_vdecl =
  250. {
  251.    "update draw module vdecl",
  252.    (SVGA_NEW_VS |
  253.     SVGA_NEW_FS),
  254.    update_swtnl_vdecl
  255. };
  256.