• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "util/list.h"
34 #include "util/compiler.h"
35 #include "pipe/p_state.h"
36 #include "gallivm/lp_bld_sample.h" /* for struct lp_sampler_static_state */
37 #include "gallivm/lp_bld_jit_sample.h"
38 #include "gallivm/lp_bld_tgsi.h" /* for lp_tgsi_info */
39 #include "lp_bld_interp.h" /* for struct lp_shader_input */
40 #include "util/u_inlines.h"
41 #include "lp_jit.h"
42 
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_depth_state
62 {
63    unsigned enabled:1;         /**< depth test enabled? */
64    unsigned writemask:1;       /**< allow depth buffer writes? */
65    unsigned func:3;            /**< depth test func (PIPE_FUNC_x) */
66 };
67 
68 struct lp_fragment_shader_variant_key
69 {
70    struct lp_depth_state depth;
71    struct pipe_stencil_state stencil[2];
72    struct pipe_blend_state blend;
73 
74    struct {
75       unsigned enabled:1;
76       unsigned func:3;
77    } alpha;
78 
79    unsigned nr_cbufs:8;
80    unsigned nr_samplers:8;      /* actually derivable from just the shader */
81    unsigned nr_sampler_views:8; /* actually derivable from just the shader */
82    unsigned nr_images:8;        /* actually derivable from just the shader */
83    unsigned flatshade:1;
84    unsigned occlusion_count:1;
85    unsigned resource_1d:1;
86    unsigned depth_clamp:1;
87    unsigned multisample:1;
88    unsigned no_ms_sample_mask_out:1;
89    unsigned restrict_depth_values:1;
90 
91    enum pipe_format zsbuf_format;
92    enum pipe_format cbuf_format[PIPE_MAX_COLOR_BUFS];
93 
94    uint8_t cbuf_nr_samples[PIPE_MAX_COLOR_BUFS];
95    uint8_t zsbuf_nr_samples;
96    uint8_t coverage_samples;
97    uint8_t min_samples;
98    /* followed by variable number of samplers + images */
99 };
100 
101 #define LP_FS_MAX_VARIANT_KEY_SIZE                                      \
102    (sizeof(struct lp_fragment_shader_variant_key) +                     \
103     PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct lp_sampler_static_state) +\
104     PIPE_MAX_SHADER_IMAGES * sizeof(struct lp_image_static_state))
105 
106 static inline size_t
lp_fs_variant_key_size(unsigned nr_samplers,unsigned nr_images)107 lp_fs_variant_key_size(unsigned nr_samplers, unsigned nr_images)
108 {
109    return (sizeof(struct lp_fragment_shader_variant_key) +
110            nr_samplers * sizeof(struct lp_sampler_static_state) +
111            nr_images * sizeof(struct lp_image_static_state));
112 }
113 
114 static inline struct lp_sampler_static_state *
lp_fs_variant_key_samplers(const struct lp_fragment_shader_variant_key * key)115 lp_fs_variant_key_samplers(const struct lp_fragment_shader_variant_key *key)
116 {
117    return (struct lp_sampler_static_state *)&(key[1]);
118 }
119 
120 static inline struct lp_sampler_static_state *
lp_fs_variant_key_sampler_idx(const struct lp_fragment_shader_variant_key * key,int idx)121 lp_fs_variant_key_sampler_idx(const struct lp_fragment_shader_variant_key *key, int idx)
122 {
123    if (idx >= key->nr_samplers)
124       return NULL;
125    return &lp_fs_variant_key_samplers(key)[idx];
126 }
127 
128 static inline struct lp_image_static_state *
lp_fs_variant_key_images(struct lp_fragment_shader_variant_key * key)129 lp_fs_variant_key_images(struct lp_fragment_shader_variant_key *key)
130 {
131    return (struct lp_image_static_state *)
132       &(lp_fs_variant_key_samplers(key)[MAX2(key->nr_samplers,
133                                              key->nr_sampler_views)]);
134 }
135 
136 /** doubly-linked list item */
137 struct lp_fs_variant_list_item
138 {
139    struct list_head list;
140    struct lp_fragment_shader_variant *base;
141 };
142 
143 
144 struct lp_fragment_shader_variant
145 {
146    /*
147     * Whether some primitives can be opaque.
148     */
149    unsigned potentially_opaque:1;
150 
151    unsigned opaque:1;
152    unsigned blit:1;
153    unsigned linear_input_mask:16;
154    struct pipe_reference reference;
155 
156    struct gallivm_state *gallivm;
157 
158    LLVMTypeRef jit_context_type;
159    LLVMTypeRef jit_context_ptr_type;
160    LLVMTypeRef jit_thread_data_type;
161    LLVMTypeRef jit_resources_type;
162    LLVMTypeRef jit_resources_ptr_type;
163    LLVMTypeRef jit_thread_data_ptr_type;
164    LLVMTypeRef jit_linear_context_type;
165    LLVMTypeRef jit_linear_context_ptr_type;
166    LLVMTypeRef jit_linear_func_type;
167    LLVMTypeRef jit_linear_inputs_type;
168    LLVMTypeRef jit_linear_textures_type;
169 
170    LLVMValueRef function[2]; // [RAST_WHOLE], [RAST_EDGE_TEST]
171 
172    lp_jit_frag_func jit_function[2]; // [RAST_WHOLE], [RAST_EDGE_TEST]
173 
174    lp_jit_linear_func jit_linear;
175    lp_jit_linear_func jit_linear_blit;
176 
177    /* Functions within the linear path:
178     */
179    LLVMValueRef linear_function;
180    lp_jit_linear_llvm_func jit_linear_llvm;
181 
182    /* Bitmask to say what cbufs are unswizzled */
183    unsigned unswizzled_cbufs;
184 
185    /* Total number of LLVM instructions generated */
186    unsigned nr_instrs;
187 
188    struct lp_fs_variant_list_item list_item_global, list_item_local;
189    struct lp_fragment_shader *shader;
190 
191    /* For debugging/profiling purposes */
192    unsigned no;
193 
194    /* key is variable-sized, must be last */
195    struct lp_fragment_shader_variant_key key;
196 };
197 
198 
199 /** Subclass of pipe_shader_state */
200 struct lp_fragment_shader
201 {
202    struct pipe_shader_state base;
203 
204    struct pipe_reference reference;
205    struct lp_tgsi_info info;
206 
207    /* Analysis results */
208    enum lp_fs_kind kind;
209 
210    struct lp_fs_variant_list_item variants;
211 
212    struct draw_fragment_shader *draw_data;
213 
214    /* For debugging/profiling purposes */
215    unsigned variant_key_size;
216    unsigned no;
217    unsigned variants_created;
218    unsigned variants_cached;
219 
220    /** Fragment shader input interpolation info */
221    struct lp_shader_input inputs[PIPE_MAX_SHADER_INPUTS];
222 };
223 
224 
225 void
226 llvmpipe_fs_analyse_nir(struct lp_fragment_shader *shader);
227 
228 void
229 llvmpipe_fs_variant_fastpath(struct lp_fragment_shader_variant *variant);
230 
231 void
232 llvmpipe_fs_variant_linear_fastpath(struct lp_fragment_shader_variant *variant);
233 
234 void
235 llvmpipe_fs_variant_linear_llvm(struct llvmpipe_context *lp,
236                                 struct lp_fragment_shader *shader,
237                                 struct lp_fragment_shader_variant *variant);
238 
239 void
240 lp_debug_fs_variant(struct lp_fragment_shader_variant *variant);
241 
242 const char *
243 lp_debug_fs_kind(enum lp_fs_kind kind);
244 
245 void
246 lp_linear_check_variant(struct lp_fragment_shader_variant *variant);
247 
248 void
249 llvmpipe_destroy_fs(struct llvmpipe_context *llvmpipe,
250                     struct lp_fragment_shader *shader);
251 
252 static inline void
lp_fs_reference(struct llvmpipe_context * llvmpipe,struct lp_fragment_shader ** ptr,struct lp_fragment_shader * shader)253 lp_fs_reference(struct llvmpipe_context *llvmpipe,
254                 struct lp_fragment_shader **ptr,
255                 struct lp_fragment_shader *shader)
256 {
257    struct lp_fragment_shader *old_ptr = *ptr;
258    if (pipe_reference(old_ptr ? &(*ptr)->reference : NULL,
259                       shader ? &shader->reference : NULL)) {
260       llvmpipe_destroy_fs(llvmpipe, old_ptr);
261    }
262    *ptr = shader;
263 }
264 
265 void
266 llvmpipe_destroy_shader_variant(struct llvmpipe_context *lp,
267                                 struct lp_fragment_shader_variant *variant);
268 
269 static inline void
lp_fs_variant_reference(struct llvmpipe_context * llvmpipe,struct lp_fragment_shader_variant ** ptr,struct lp_fragment_shader_variant * variant)270 lp_fs_variant_reference(struct llvmpipe_context *llvmpipe,
271                         struct lp_fragment_shader_variant **ptr,
272                         struct lp_fragment_shader_variant *variant)
273 {
274    struct lp_fragment_shader_variant *old_ptr = *ptr;
275    if (pipe_reference(old_ptr ? &(*ptr)->reference : NULL,
276                       variant ? &variant->reference : NULL)) {
277       llvmpipe_destroy_shader_variant(llvmpipe, old_ptr);
278    }
279    *ptr = variant;
280 }
281 
282 #endif /* LP_STATE_FS_H_ */
283