Squashed commit of the following:
[profile/ivi/mesa.git] / src / mesa / state_tracker / st_atom_constbuf.c
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 "shader/prog_parameter.h"
36 #include "shader/prog_print.h"
37
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "util/u_inlines.h"
41
42 #include "st_debug.h"
43 #include "st_context.h"
44 #include "st_atom.h"
45 #include "st_atom_constbuf.h"
46 #include "st_program.h"
47 #include "st_inlines.h"
48
49
50 /**
51  * Pass the given program parameters to the graphics pipe as a
52  * constant buffer.
53  * \param shader_type  either PIPE_SHADER_VERTEX or PIPE_SHADER_FRAGMENT
54  */
55 void st_upload_constants( struct st_context *st,
56                           struct gl_program_parameter_list *params,
57                           unsigned shader_type)
58 {
59    struct pipe_context *pipe = st->pipe;
60    struct pipe_resource **cbuf = &st->state.constants[shader_type];
61
62    assert(shader_type == PIPE_SHADER_VERTEX ||
63           shader_type == PIPE_SHADER_FRAGMENT);
64
65    /* update constants */
66    if (params && params->NumParameters) {
67       const uint paramBytes = params->NumParameters * sizeof(GLfloat) * 4;
68
69       _mesa_load_state_parameters(st->ctx, params);
70
71       /* We always need to get a new buffer, to keep the drivers simple and
72        * avoid gratuitous rendering synchronization.
73        */
74       pipe_resource_reference(cbuf, NULL );
75       *cbuf = pipe_buffer_create(pipe->screen,
76                                  PIPE_BIND_CONSTANT_BUFFER,
77                                  paramBytes );
78
79       if (ST_DEBUG & DEBUG_CONSTANTS) {
80          debug_printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n", 
81                       __FUNCTION__, shader_type, params->NumParameters,
82                       params->StateFlags);
83          _mesa_print_parameter_list(params);
84       }
85
86       /* load Mesa constants into the constant buffer */
87       if (cbuf)
88          st_no_flush_pipe_buffer_write(st, *cbuf,
89                                        0, paramBytes,
90                                        params->ParameterValues);
91
92       st->pipe->set_constant_buffer(st->pipe, shader_type, 0, *cbuf);
93    }
94    else {
95       st->constants.tracked_state[shader_type].dirty.mesa = 0x0;
96    }
97 }
98
99
100 /**
101  * Vertex shader:
102  */
103 static void update_vs_constants(struct st_context *st )
104 {
105    struct st_vertex_program *vp = st->vp;
106    struct gl_program_parameter_list *params = vp->Base.Base.Parameters;
107
108    st_upload_constants( st, params, PIPE_SHADER_VERTEX );
109 }
110
111
112 const struct st_tracked_state st_update_vs_constants = {
113    "st_update_vs_constants",                            /* name */
114    {                                                    /* dirty */
115       (_NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS),          /* mesa */
116       ST_NEW_VERTEX_PROGRAM,                            /* st */
117    },
118    update_vs_constants                                  /* update */
119 };
120
121
122
123 /**
124  * Fragment shader:
125  */
126 static void update_fs_constants(struct st_context *st )
127 {
128    struct st_fragment_program *fp = st->fp;
129    struct gl_program_parameter_list *params = fp->Base.Base.Parameters;
130
131    st_upload_constants( st, params, PIPE_SHADER_FRAGMENT );
132 }
133
134
135 const struct st_tracked_state st_update_fs_constants = {
136    "st_update_fs_constants",                            /* name */
137    {                                                    /* dirty */
138       (_NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS),          /* mesa */
139       ST_NEW_FRAGMENT_PROGRAM,                          /* st */
140    },
141    update_fs_constants                                  /* update */
142 };
143