Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / svga / svga_state_constants.c
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
29 #include "svga_context.h"
30 #include "svga_state.h"
31 #include "svga_cmd.h"
32 #include "svga_tgsi.h"
33 #include "svga_debug.h"
34
35 #include "svga_hw_reg.h"
36
37 /***********************************************************************
38  * Hardware update 
39  */
40
41 /* Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
42  */
43 static int svga_shader_type( int shader )
44 {
45    assert(PIPE_SHADER_VERTEX + 1 == SVGA3D_SHADERTYPE_VS);
46    assert(PIPE_SHADER_FRAGMENT + 1 == SVGA3D_SHADERTYPE_PS);
47    assert(shader <= PIPE_SHADER_FRAGMENT);
48    return shader + 1;
49 }
50
51
52 static int emit_const( struct svga_context *svga,
53                        int unit,
54                        int i,
55                        const float *value )
56 {
57    int ret = PIPE_OK;
58
59    if (memcmp(svga->state.hw_draw.cb[unit][i], value, 4 * sizeof(float)) != 0) {
60       if (SVGA_DEBUG & DEBUG_CONSTS)
61          debug_printf("%s %s %d: %f %f %f %f\n",
62                       __FUNCTION__,
63                       unit == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
64                       i,
65                       value[0],
66                       value[1],
67                       value[2],
68                       value[3]);
69
70       ret = SVGA3D_SetShaderConst( svga->swc, 
71                                    i,
72                                    svga_shader_type(unit),
73                                    SVGA3D_CONST_TYPE_FLOAT,
74                                    value );
75       if (ret)
76          return ret;
77
78       memcpy(svga->state.hw_draw.cb[unit][i], value, 4 * sizeof(float));
79    }
80    
81    return ret;
82 }
83
84 static int emit_consts( struct svga_context *svga,
85                         int offset,
86                         int unit )
87 {
88    struct pipe_transfer *transfer = NULL;
89    unsigned count;
90    const float (*data)[4] = NULL;
91    unsigned i;
92    int ret = PIPE_OK;
93
94    if (svga->curr.cb[unit] == NULL)
95       goto done;
96
97    count = svga->curr.cb[unit]->width0 / (4 * sizeof(float));
98
99    data = (const float (*)[4])pipe_buffer_map(&svga->pipe,
100                                               svga->curr.cb[unit],
101                                               PIPE_TRANSFER_READ,
102                                               &transfer);
103    if (data == NULL) {
104       ret = PIPE_ERROR_OUT_OF_MEMORY;
105       goto done;
106    }
107
108    for (i = 0; i < count; i++) {
109       ret = emit_const( svga, unit, offset + i, data[i] );
110       if (ret)
111          goto done;
112    }
113
114 done:
115    if (data)
116       pipe_buffer_unmap(&svga->pipe, transfer);
117
118    return ret;
119 }
120    
121 static int emit_fs_consts( struct svga_context *svga,
122                            unsigned dirty )
123 {
124    const struct svga_shader_result *result = svga->state.hw_draw.fs;
125    const struct svga_fs_compile_key *key = &result->key.fkey;
126    int ret = 0;
127
128    ret = emit_consts( svga, 0, PIPE_SHADER_FRAGMENT );
129    if (ret)
130       return ret;
131
132    /* The internally generated fragment shader for xor blending
133     * doesn't have a 'result' struct.  It should be fixed to avoid
134     * this special case, but work around it with a NULL check:
135     */
136    if (result != NULL &&
137        key->num_unnormalized_coords)
138    {
139       unsigned offset = result->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
140       int i;
141
142       for (i = 0; i < key->num_textures; i++) {
143          if (key->tex[i].unnormalized) {
144             struct pipe_resource *tex = svga->curr.sampler_views[i]->texture;
145             float data[4];
146
147             data[0] = 1.0 / (float)tex->width0;
148             data[1] = 1.0 / (float)tex->height0;
149             data[2] = 1.0;
150             data[3] = 1.0;
151
152             ret = emit_const( svga,
153                               PIPE_SHADER_FRAGMENT,
154                               key->tex[i].width_height_idx + offset,
155                               data );
156             if (ret)
157                return ret;
158          }
159       }
160
161       offset += key->num_unnormalized_coords;
162    }
163
164    return 0;
165 }
166
167
168 struct svga_tracked_state svga_hw_fs_parameters = 
169 {
170    "hw fs params",
171    (SVGA_NEW_FS_CONST_BUFFER |
172     SVGA_NEW_FS_RESULT |
173     SVGA_NEW_TEXTURE_BINDING),
174    emit_fs_consts
175 };
176
177 /***********************************************************************
178  */
179
180 static int emit_vs_consts( struct svga_context *svga,
181                            unsigned dirty )
182 {
183    const struct svga_shader_result *result = svga->state.hw_draw.vs;
184    const struct svga_vs_compile_key *key = &result->key.vkey;
185    int ret = 0;
186    unsigned offset;
187
188    /* SVGA_NEW_VS_RESULT
189     */
190    if (result == NULL) 
191       return 0;
192
193    /* SVGA_NEW_VS_CONST_BUFFER 
194     */
195    ret = emit_consts( svga, 0, PIPE_SHADER_VERTEX );
196    if (ret)
197       return ret;
198
199    offset = result->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
200
201    /* SVGA_NEW_VS_RESULT
202     */
203    if (key->need_prescale) {
204       ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
205                         svga->state.hw_clear.prescale.scale );
206       if (ret)
207          return ret;
208
209       ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
210                         svga->state.hw_clear.prescale.translate );
211       if (ret)
212          return ret;
213    }
214
215    /* SVGA_NEW_ZERO_STRIDE
216     */
217    if (key->zero_stride_vertex_elements) {
218       unsigned i, curr_zero_stride = 0;
219       for (i = 0; i < PIPE_MAX_ATTRIBS; ++i) {
220          if (key->zero_stride_vertex_elements & (1 << i)) {
221             ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
222                               svga->curr.zero_stride_constants +
223                               4 * curr_zero_stride );
224             if (ret)
225                return ret;
226             ++curr_zero_stride;
227          }
228       }
229    }
230
231    return 0;
232 }
233
234
235 struct svga_tracked_state svga_hw_vs_parameters = 
236 {
237    "hw vs params",
238    (SVGA_NEW_PRESCALE |
239     SVGA_NEW_VS_CONST_BUFFER |
240     SVGA_NEW_ZERO_STRIDE |
241     SVGA_NEW_VS_RESULT),
242    emit_vs_consts
243 };
244