Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | 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. /*
  29.  * Authors:
  30.  *   Keith Whitwell <keith@tungstengraphics.com>
  31.  *   Brian Paul
  32.  */
  33.  
  34. #include "main/imports.h"
  35. #include "program/prog_parameter.h"
  36. #include "program/prog_print.h"
  37.  
  38. #include "pipe/p_context.h"
  39. #include "pipe/p_defines.h"
  40. #include "util/u_inlines.h"
  41. #include "util/u_upload_mgr.h"
  42. #include "cso_cache/cso_context.h"
  43.  
  44. #include "st_debug.h"
  45. #include "st_context.h"
  46. #include "st_atom.h"
  47. #include "st_atom_constbuf.h"
  48. #include "st_program.h"
  49. #include "st_cb_bufferobjects.h"
  50.  
  51. /**
  52.  * Pass the given program parameters to the graphics pipe as a
  53.  * constant buffer.
  54.  * \param shader_type  either PIPE_SHADER_VERTEX or PIPE_SHADER_FRAGMENT
  55.  */
  56. void st_upload_constants( struct st_context *st,
  57.                           struct gl_program_parameter_list *params,
  58.                           unsigned shader_type)
  59. {
  60.    assert(shader_type == PIPE_SHADER_VERTEX ||
  61.           shader_type == PIPE_SHADER_FRAGMENT ||
  62.           shader_type == PIPE_SHADER_GEOMETRY);
  63.  
  64.    /* update constants */
  65.    if (params && params->NumParameters) {
  66.       struct pipe_constant_buffer cb;
  67.       const uint paramBytes = params->NumParameters * sizeof(GLfloat) * 4;
  68.  
  69.       /* Update the constants which come from fixed-function state, such as
  70.        * transformation matrices, fog factors, etc.  The rest of the values in
  71.        * the parameters list are explicitly set by the user with glUniform,
  72.        * glProgramParameter(), etc.
  73.        */
  74.       _mesa_load_state_parameters(st->ctx, params);
  75.  
  76.       /* We always need to get a new buffer, to keep the drivers simple and
  77.        * avoid gratuitous rendering synchronization.
  78.        * Let's use a user buffer to avoid an unnecessary copy.
  79.        */
  80.       if (st->constbuf_uploader) {
  81.          cb.buffer = NULL;
  82.          cb.user_buffer = NULL;
  83.          u_upload_data(st->constbuf_uploader, 0, paramBytes,
  84.                        params->ParameterValues, &cb.buffer_offset, &cb.buffer);
  85.          u_upload_unmap(st->constbuf_uploader);
  86.       } else {
  87.          cb.buffer = NULL;
  88.          cb.user_buffer = params->ParameterValues;
  89.          cb.buffer_offset = 0;
  90.       }
  91.       cb.buffer_size = paramBytes;
  92.  
  93.       if (ST_DEBUG & DEBUG_CONSTANTS) {
  94.          debug_printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n",
  95.                       __FUNCTION__, shader_type, params->NumParameters,
  96.                       params->StateFlags);
  97.          _mesa_print_parameter_list(params);
  98.       }
  99.  
  100.       cso_set_constant_buffer(st->cso_context, shader_type, 0, &cb);
  101.       pipe_resource_reference(&cb.buffer, NULL);
  102.  
  103.       st->state.constants[shader_type].ptr = params->ParameterValues;
  104.       st->state.constants[shader_type].size = paramBytes;
  105.    }
  106.    else if (st->state.constants[shader_type].ptr) {
  107.       /* Unbind. */
  108.       st->state.constants[shader_type].ptr = NULL;
  109.       st->state.constants[shader_type].size = 0;
  110.       cso_set_constant_buffer(st->cso_context, shader_type, 0, NULL);
  111.    }
  112. }
  113.  
  114.  
  115. /**
  116.  * Vertex shader:
  117.  */
  118. static void update_vs_constants(struct st_context *st )
  119. {
  120.    struct st_vertex_program *vp = st->vp;
  121.    struct gl_program_parameter_list *params = vp->Base.Base.Parameters;
  122.  
  123.    st_upload_constants( st, params, PIPE_SHADER_VERTEX );
  124. }
  125.  
  126.  
  127. const struct st_tracked_state st_update_vs_constants = {
  128.    "st_update_vs_constants",                            /* name */
  129.    {                                                    /* dirty */
  130.       _NEW_PROGRAM_CONSTANTS,                           /* mesa */
  131.       ST_NEW_VERTEX_PROGRAM,                            /* st */
  132.    },
  133.    update_vs_constants                                  /* update */
  134. };
  135.  
  136.  
  137.  
  138. /**
  139.  * Fragment shader:
  140.  */
  141. static void update_fs_constants(struct st_context *st )
  142. {
  143.    struct st_fragment_program *fp = st->fp;
  144.    struct gl_program_parameter_list *params = fp->Base.Base.Parameters;
  145.  
  146.    st_upload_constants( st, params, PIPE_SHADER_FRAGMENT );
  147. }
  148.  
  149.  
  150. const struct st_tracked_state st_update_fs_constants = {
  151.    "st_update_fs_constants",                            /* name */
  152.    {                                                    /* dirty */
  153.       _NEW_PROGRAM_CONSTANTS,                           /* mesa */
  154.       ST_NEW_FRAGMENT_PROGRAM,                          /* st */
  155.    },
  156.    update_fs_constants                                  /* update */
  157. };
  158.  
  159. /* Geometry shader:
  160.  */
  161. static void update_gs_constants(struct st_context *st )
  162. {
  163.    struct st_geometry_program *gp = st->gp;
  164.    struct gl_program_parameter_list *params;
  165.  
  166.    if (gp) {
  167.       params = gp->Base.Base.Parameters;
  168.       st_upload_constants( st, params, PIPE_SHADER_GEOMETRY );
  169.    }
  170. }
  171.  
  172. const struct st_tracked_state st_update_gs_constants = {
  173.    "st_update_gs_constants",                            /* name */
  174.    {                                                    /* dirty */
  175.       _NEW_PROGRAM_CONSTANTS,                           /* mesa */
  176.       ST_NEW_GEOMETRY_PROGRAM,                          /* st */
  177.    },
  178.    update_gs_constants                                  /* update */
  179. };
  180.  
  181. static void st_bind_ubos(struct st_context *st,
  182.                            struct gl_shader *shader,
  183.                            unsigned shader_type)
  184. {
  185.    unsigned i;
  186.    struct pipe_constant_buffer cb = { 0 };
  187.  
  188.    if (!shader)
  189.       return;
  190.  
  191.    for (i = 0; i < shader->NumUniformBlocks; i++) {
  192.       struct gl_uniform_buffer_binding *binding;
  193.       struct st_buffer_object *st_obj;
  194.  
  195.       binding = &st->ctx->UniformBufferBindings[shader->UniformBlocks[i].Binding];
  196.       st_obj = st_buffer_object(binding->BufferObject);
  197.  
  198.       cb.buffer = st_obj->buffer;
  199.  
  200.       if (cb.buffer) {
  201.          cb.buffer_offset = binding->Offset;
  202.          cb.buffer_size = cb.buffer->width0 - binding->Offset;
  203.  
  204.          /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
  205.           * Take the minimum just to be sure.
  206.           */
  207.          if (!binding->AutomaticSize)
  208.             cb.buffer_size = MIN2(cb.buffer_size, (unsigned) binding->Size);
  209.       }
  210.       else {
  211.          cb.buffer_offset = 0;
  212.          cb.buffer_size = 0;
  213.       }
  214.  
  215.       cso_set_constant_buffer(st->cso_context, shader_type, 1 + i, &cb);
  216.    }
  217. }
  218.  
  219. static void bind_vs_ubos(struct st_context *st)
  220. {
  221.    struct gl_shader_program *prog = st->ctx->Shader.CurrentVertexProgram;
  222.  
  223.    if (!prog)
  224.       return;
  225.  
  226.    st_bind_ubos(st, prog->_LinkedShaders[MESA_SHADER_VERTEX], PIPE_SHADER_VERTEX);
  227. }
  228.  
  229. const struct st_tracked_state st_bind_vs_ubos = {
  230.    "st_bind_vs_ubos",
  231.    {
  232.       0,
  233.       ST_NEW_VERTEX_PROGRAM | ST_NEW_UNIFORM_BUFFER,
  234.    },
  235.    bind_vs_ubos
  236. };
  237.  
  238. static void bind_fs_ubos(struct st_context *st)
  239. {
  240.    struct gl_shader_program *prog = st->ctx->Shader.CurrentFragmentProgram;
  241.  
  242.    if (!prog)
  243.       return;
  244.  
  245.    st_bind_ubos(st, prog->_LinkedShaders[MESA_SHADER_FRAGMENT], PIPE_SHADER_FRAGMENT);
  246. }
  247.  
  248. const struct st_tracked_state st_bind_fs_ubos = {
  249.    "st_bind_fs_ubos",
  250.    {
  251.       0,
  252.       ST_NEW_FRAGMENT_PROGRAM | ST_NEW_UNIFORM_BUFFER,
  253.    },
  254.    bind_fs_ubos
  255. };
  256.  
  257.  
  258.