Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / svga / svga_state_fs.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 #include "util/u_math.h"
29 #include "util/u_bitmask.h"
30
31 #include "svga_context.h"
32 #include "svga_state.h"
33 #include "svga_cmd.h"
34 #include "svga_tgsi.h"
35
36 #include "svga_hw_reg.h"
37
38
39
40 static INLINE int compare_fs_keys( const struct svga_fs_compile_key *a,
41                                    const struct svga_fs_compile_key *b )
42 {
43    unsigned keysize_a = svga_fs_key_size( a );
44    unsigned keysize_b = svga_fs_key_size( b );
45
46    if (keysize_a != keysize_b) {
47       return (int)(keysize_a - keysize_b);
48    }
49    return memcmp( a, b, keysize_a );
50 }
51
52
53 static struct svga_shader_result *search_fs_key( struct svga_fragment_shader *fs,
54                                                  const struct svga_fs_compile_key *key )
55 {
56    struct svga_shader_result *result = fs->base.results;
57
58    assert(key);
59
60    for ( ; result; result = result->next) {
61       if (compare_fs_keys( key, &result->key.fkey ) == 0)
62          return result;
63    }
64    
65    return NULL;
66 }
67
68
69 static enum pipe_error compile_fs( struct svga_context *svga,
70                                    struct svga_fragment_shader *fs,
71                                    const struct svga_fs_compile_key *key,
72                                    struct svga_shader_result **out_result )
73 {
74    struct svga_shader_result *result;
75    enum pipe_error ret = PIPE_ERROR;
76
77    result = svga_translate_fragment_program( fs, key );
78    if (result == NULL) {
79       ret = PIPE_ERROR_OUT_OF_MEMORY;
80       goto fail;
81    }
82
83    result->id = util_bitmask_add(svga->fs_bm);
84    if(result->id == UTIL_BITMASK_INVALID_INDEX) {
85       ret = PIPE_ERROR_OUT_OF_MEMORY;
86       goto fail;
87    }
88
89    ret = SVGA3D_DefineShader(svga->swc, 
90                              result->id,
91                              SVGA3D_SHADERTYPE_PS,
92                              result->tokens, 
93                              result->nr_tokens * sizeof result->tokens[0]);
94    if (ret)
95       goto fail;
96
97    *out_result = result;
98    result->next = fs->base.results;
99    fs->base.results = result;
100    return PIPE_OK;
101
102 fail:
103    if (result) {
104       if (result->id != UTIL_BITMASK_INVALID_INDEX)
105          util_bitmask_clear( svga->fs_bm, result->id );
106       svga_destroy_shader_result( result );
107    }
108    return ret;
109 }
110
111
112 /* SVGA_NEW_TEXTURE_BINDING
113  * SVGA_NEW_RAST
114  * SVGA_NEW_NEED_SWTNL
115  * SVGA_NEW_SAMPLER
116  */
117 static int make_fs_key( const struct svga_context *svga,
118                         struct svga_fs_compile_key *key )
119 {
120    int i;
121    int idx = 0;
122
123    memset(key, 0, sizeof *key);
124
125    /* Only need fragment shader fixup for twoside lighting if doing
126     * hwtnl.  Otherwise the draw module does the whole job for us.
127     *
128     * SVGA_NEW_SWTNL
129     */
130    if (!svga->state.sw.need_swtnl) {
131       /* SVGA_NEW_RAST
132        */
133       key->light_twoside = svga->curr.rast->templ.light_twoside;
134       key->front_ccw = svga->curr.rast->templ.front_ccw;
135    }
136
137    /* The blend workaround for simulating logicop xor behaviour
138     * requires that the incoming fragment color be white.  This change
139     * achieves that by creating a variant of the current fragment
140     * shader that overrides all output colors with 1,1,1,1
141     *   
142     * This will work for most shaders, including those containing
143     * TEXKIL and/or depth-write.  However, it will break on the
144     * combination of xor-logicop plus alphatest.
145     *
146     * Ultimately, we could implement alphatest in the shader using
147     * texkil prior to overriding the outgoing fragment color.
148     *   
149     * SVGA_NEW_BLEND
150     */
151    if (svga->curr.blend->need_white_fragments) {
152       key->white_fragments = 1;
153    }
154    
155    /* XXX: want to limit this to the textures that the shader actually
156     * refers to.
157     *
158     * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
159     */
160    for (i = 0; i < svga->curr.num_sampler_views; i++) {
161       if (svga->curr.sampler_views[i]) {
162          assert(svga->curr.sampler[i]);
163          assert(svga->curr.sampler_views[i]->texture);
164          key->tex[i].texture_target = svga->curr.sampler_views[i]->texture->target;
165          if (!svga->curr.sampler[i]->normalized_coords) {
166             key->tex[i].width_height_idx = idx++;
167             key->tex[i].unnormalized = TRUE;
168             ++key->num_unnormalized_coords;
169          }
170       }
171    }
172    key->num_textures = svga->curr.num_sampler_views;
173
174    idx = 0;
175    for (i = 0; i < svga->curr.num_samplers; ++i) {
176       if (svga->curr.sampler[i]) {
177          key->tex[i].compare_mode = svga->curr.sampler[i]->compare_mode;
178          key->tex[i].compare_func = svga->curr.sampler[i]->compare_func;
179       }
180    }
181
182    return 0;
183 }
184
185
186
187 static int emit_hw_fs( struct svga_context *svga,
188                        unsigned dirty )
189 {
190    struct svga_shader_result *result = NULL;
191    unsigned id = SVGA3D_INVALID_ID;
192    int ret = 0;
193
194    struct svga_fragment_shader *fs = svga->curr.fs;
195    struct svga_fs_compile_key key;
196
197    /* SVGA_NEW_BLEND
198     * SVGA_NEW_TEXTURE_BINDING
199     * SVGA_NEW_RAST
200     * SVGA_NEW_NEED_SWTNL
201     * SVGA_NEW_SAMPLER
202     */
203    ret = make_fs_key( svga, &key );
204    if (ret)
205       return ret;
206
207    result = search_fs_key( fs, &key );
208    if (!result) {
209       ret = compile_fs( svga, fs, &key, &result );
210       if (ret)
211          return ret;
212    }
213
214    assert (result);
215    id = result->id;
216
217    assert(id != SVGA3D_INVALID_ID);
218
219    if (result != svga->state.hw_draw.fs) {
220       ret = SVGA3D_SetShader(svga->swc,
221                              SVGA3D_SHADERTYPE_PS,
222                              id );
223       if (ret)
224          return ret;
225
226       svga->dirty |= SVGA_NEW_FS_RESULT;
227       svga->state.hw_draw.fs = result;      
228    }
229
230    return 0;
231 }
232
233 struct svga_tracked_state svga_hw_fs = 
234 {
235    "fragment shader (hwtnl)",
236    (SVGA_NEW_FS |
237     SVGA_NEW_TEXTURE_BINDING |
238     SVGA_NEW_NEED_SWTNL |
239     SVGA_NEW_RAST |
240     SVGA_NEW_SAMPLER |
241     SVGA_NEW_BLEND),
242    emit_hw_fs
243 };
244
245
246