1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  **************************************************************************/
27 
28 
29 #ifndef LP_STATE_FS_H_
30 #define LP_STATE_FS_H_
31 
32 
33 #include "pipe/p_compiler.h"
34 #include "pipe/p_state.h"
35 #include "tgsi/tgsi_scan.h" /* for tgsi_shader_info */
36 #include "gallivm/lp_bld_sample.h" /* for struct lp_sampler_static_state */
37 #include "gallivm/lp_bld_tgsi.h" /* for lp_tgsi_info */
38 #include "lp_bld_interp.h" /* for struct lp_shader_input */
39 #include "util/u_inlines.h"
40 #include "lp_jit.h"
41 
42 struct tgsi_token;
43 struct lp_fragment_shader;
44 
45 
46 /** Indexes into jit_function[] array */
47 #define RAST_WHOLE 0
48 #define RAST_EDGE_TEST 1
49 
50 
51 enum lp_fs_kind
52 {
53    LP_FS_KIND_GENERAL = 0,
54    LP_FS_KIND_BLIT_RGBA,
55    LP_FS_KIND_BLIT_RGB1,
56    LP_FS_KIND_AERO_MINIFICATION,
57    LP_FS_KIND_LLVM_LINEAR
58 };
59 
60 
61 struct lp_sampler_static_state
62 {
63    /*
64     * These attributes are effectively interleaved for more sane key handling.
65     * However, there might be lots of null space if the amount of samplers and
66     * textures isn't the same.
67     */
68    struct lp_static_sampler_state sampler_state;
69    struct lp_static_texture_state texture_state;
70 };
71 
72 
73 struct lp_image_static_state
74 {
75    struct lp_static_texture_state image_state;
76 };
77 
78 struct lp_depth_state
79 {
80    unsigned enabled:1;         /**< depth test enabled? */
81    unsigned writemask:1;       /**< allow depth buffer writes? */
82    unsigned func:3;            /**< depth test func (PIPE_FUNC_x) */
83 };
84 
85 struct lp_fragment_shader_variant_key
86 {
87    struct lp_depth_state depth;
88    struct pipe_stencil_state stencil[2];
89    struct pipe_blend_state blend;
90 
91    struct {
92       unsigned enabled:1;
93       unsigned func:3;
94    } alpha;
95 
96    unsigned nr_cbufs:8;
97    unsigned nr_samplers:8;      /* actually derivable from just the shader */
98    unsigned nr_sampler_views:8; /* actually derivable from just the shader */
99    unsigned nr_images:8;        /* actually derivable from just the shader */
100    unsigned flatshade:1;
101    unsigned occlusion_count:1;
102    unsigned resource_1d:1;
103    unsigned depth_clamp:1;
104    unsigned multisample:1;
105    unsigned no_ms_sample_mask_out:1;
106 
107    enum pipe_format zsbuf_format;
108    enum pipe_format cbuf_format[PIPE_MAX_COLOR_BUFS];
109 
110    uint8_t cbuf_nr_samples[PIPE_MAX_COLOR_BUFS];
111    uint8_t zsbuf_nr_samples;
112    uint8_t coverage_samples;
113    uint8_t min_samples;
114    /* followed by variable number of samplers + images */
115 };
116 
117 #define LP_FS_MAX_VARIANT_KEY_SIZE                                      \
118    (sizeof(struct lp_fragment_shader_variant_key) +                     \
119     PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct lp_sampler_static_state) +\
120     PIPE_MAX_SHADER_IMAGES * sizeof(struct lp_image_static_state))
121 
122 static inline size_t
lp_fs_variant_key_size(unsigned nr_samplers,unsigned nr_images)123 lp_fs_variant_key_size(unsigned nr_samplers, unsigned nr_images)
124 {
125    return (sizeof(struct lp_fragment_shader_variant_key) +
126            nr_samplers * sizeof(struct lp_sampler_static_state) +
127            nr_images * sizeof(struct lp_image_static_state));
128 }
129 
130 static inline struct lp_sampler_static_state *
lp_fs_variant_key_samplers(const struct lp_fragment_shader_variant_key * key)131 lp_fs_variant_key_samplers(const struct lp_fragment_shader_variant_key *key)
132 {
133    return (struct lp_sampler_static_state *)&(key[1]);
134 }
135 
136 static inline struct lp_sampler_static_state *
lp_fs_variant_key_sampler_idx(const struct lp_fragment_shader_variant_key * key,int idx)137 lp_fs_variant_key_sampler_idx(const struct lp_fragment_shader_variant_key *key, int idx)
138 {
139    if (idx >= key->nr_samplers)
140       return NULL;
141    return &lp_fs_variant_key_samplers(key)[idx];
142 }
143 
144 static inline struct lp_image_static_state *
lp_fs_variant_key_images(struct lp_fragment_shader_variant_key * key)145 lp_fs_variant_key_images(struct lp_fragment_shader_variant_key *key)
146 {
147    return (struct lp_image_static_state *)
148       &(lp_fs_variant_key_samplers(key)[key->nr_samplers]);
149 }
150 
151 /** doubly-linked list item */
152 struct lp_fs_variant_list_item
153 {
154    struct lp_fragment_shader_variant *base;
155    struct lp_fs_variant_list_item *next, *prev;
156 };
157 
158 
159 struct lp_fragment_shader_variant
160 {
161    /*
162     * Whether some primitives can be opaque.
163     */
164    unsigned potentially_opaque:1;
165 
166    unsigned blit:1;
167    unsigned linear_input_mask:16;
168    struct pipe_reference reference;
169    boolean opaque;
170 
171    struct gallivm_state *gallivm;
172 
173    LLVMTypeRef jit_context_ptr_type;
174    LLVMTypeRef jit_thread_data_ptr_type;
175    LLVMTypeRef jit_linear_context_ptr_type;
176 
177    LLVMValueRef function[2];
178 
179    lp_jit_frag_func jit_function[2];
180 
181    lp_jit_linear_func jit_linear;
182    lp_jit_linear_func jit_linear_blit;
183 
184    /* Functions within the linear path:
185     */
186    LLVMValueRef linear_function;
187    lp_jit_linear_llvm_func jit_linear_llvm;
188 
189    /* Bitmask to say what cbufs are unswizzled */
190    unsigned unswizzled_cbufs;
191 
192    /* Total number of LLVM instructions generated */
193    unsigned nr_instrs;
194 
195    struct lp_fs_variant_list_item list_item_global, list_item_local;
196    struct lp_fragment_shader *shader;
197 
198    /* For debugging/profiling purposes */
199    unsigned no;
200 
201    /* key is variable-sized, must be last */
202    struct lp_fragment_shader_variant_key key;
203 };
204 
205 
206 /** Subclass of pipe_shader_state */
207 struct lp_fragment_shader
208 {
209    struct pipe_shader_state base;
210 
211    struct pipe_reference reference;
212    struct lp_tgsi_info info;
213 
214    /*
215     * Analysis results
216     */
217 
218    enum lp_fs_kind kind;
219 
220 
221    struct lp_fs_variant_list_item variants;
222 
223    struct draw_fragment_shader *draw_data;
224 
225    /* For debugging/profiling purposes */
226    unsigned variant_key_size;
227    unsigned no;
228    unsigned variants_created;
229    unsigned variants_cached;
230 
231    /** Fragment shader input interpolation info */
232    struct lp_shader_input inputs[PIPE_MAX_SHADER_INPUTS];
233 };
234 
235 
236 void
237 llvmpipe_fs_analyse(struct lp_fragment_shader *shader,
238                     const struct tgsi_token *tokens);
239 
240 void
241 llvmpipe_fs_variant_fastpath(struct lp_fragment_shader_variant *variant);
242 
243 void
244 llvmpipe_fs_variant_linear_fastpath(struct lp_fragment_shader_variant *variant);
245 
246 void
247 llvmpipe_fs_variant_linear_llvm(struct llvmpipe_context *lp,
248                                 struct lp_fragment_shader *shader,
249                                 struct lp_fragment_shader_variant *variant);
250 
251 void
252 lp_debug_fs_variant(struct lp_fragment_shader_variant *variant);
253 
254 const char *
255 lp_debug_fs_kind(enum lp_fs_kind kind);
256 
257 
258 void
259 lp_linear_check_variant(struct lp_fragment_shader_variant *variant);
260 
261 void
262 llvmpipe_destroy_fs(struct llvmpipe_context *llvmpipe,
263                     struct lp_fragment_shader *shader);
264 
265 static inline void
lp_fs_reference(struct llvmpipe_context * llvmpipe,struct lp_fragment_shader ** ptr,struct lp_fragment_shader * shader)266 lp_fs_reference(struct llvmpipe_context *llvmpipe,
267                 struct lp_fragment_shader **ptr,
268                 struct lp_fragment_shader *shader)
269 {
270    struct lp_fragment_shader *old_ptr = *ptr;
271    if (pipe_reference(old_ptr ? &(*ptr)->reference : NULL, shader ? &shader->reference : NULL)) {
272       llvmpipe_destroy_fs(llvmpipe, old_ptr);
273    }
274    *ptr = shader;
275 }
276 
277 void
278 llvmpipe_destroy_shader_variant(struct llvmpipe_context *lp,
279                                 struct lp_fragment_shader_variant *variant);
280 
281 static inline void
lp_fs_variant_reference(struct llvmpipe_context * llvmpipe,struct lp_fragment_shader_variant ** ptr,struct lp_fragment_shader_variant * variant)282 lp_fs_variant_reference(struct llvmpipe_context *llvmpipe,
283                         struct lp_fragment_shader_variant **ptr,
284                         struct lp_fragment_shader_variant *variant)
285 {
286    struct lp_fragment_shader_variant *old_ptr = *ptr;
287    if (pipe_reference(old_ptr ? &(*ptr)->reference : NULL, variant ? &variant->reference : NULL)) {
288       llvmpipe_destroy_shader_variant(llvmpipe, old_ptr);
289    }
290    *ptr = variant;
291 }
292 
293 #endif /* LP_STATE_FS_H_ */
294