1/*
2 Copyright (C) Intel Corp.  2006.  All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28  * Authors:
29  *   Keith Whitwell <keithw@vmware.com>
30  */
31
32
33#ifndef BRW_STATE_H
34#define BRW_STATE_H
35
36#include "brw_context.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42enum intel_msaa_layout;
43
44extern const struct brw_tracked_state brw_blend_constant_color;
45extern const struct brw_tracked_state brw_cc_vp;
46extern const struct brw_tracked_state brw_cc_unit;
47extern const struct brw_tracked_state brw_clip_unit;
48extern const struct brw_tracked_state brw_vs_pull_constants;
49extern const struct brw_tracked_state brw_tcs_pull_constants;
50extern const struct brw_tracked_state brw_tes_pull_constants;
51extern const struct brw_tracked_state brw_gs_pull_constants;
52extern const struct brw_tracked_state brw_wm_pull_constants;
53extern const struct brw_tracked_state brw_cs_pull_constants;
54extern const struct brw_tracked_state brw_constant_buffer;
55extern const struct brw_tracked_state brw_curbe_offsets;
56extern const struct brw_tracked_state brw_invariant_state;
57extern const struct brw_tracked_state brw_fs_samplers;
58extern const struct brw_tracked_state brw_gs_unit;
59extern const struct brw_tracked_state brw_line_stipple;
60extern const struct brw_tracked_state brw_binding_table_pointers;
61extern const struct brw_tracked_state brw_depthbuffer;
62extern const struct brw_tracked_state brw_polygon_stipple_offset;
63extern const struct brw_tracked_state brw_polygon_stipple;
64extern const struct brw_tracked_state brw_recalculate_urb_fence;
65extern const struct brw_tracked_state brw_sf_unit;
66extern const struct brw_tracked_state brw_sf_vp;
67extern const struct brw_tracked_state brw_vs_samplers;
68extern const struct brw_tracked_state brw_tcs_samplers;
69extern const struct brw_tracked_state brw_tes_samplers;
70extern const struct brw_tracked_state brw_gs_samplers;
71extern const struct brw_tracked_state brw_cs_samplers;
72extern const struct brw_tracked_state brw_cs_texture_surfaces;
73extern const struct brw_tracked_state brw_vs_ubo_surfaces;
74extern const struct brw_tracked_state brw_vs_abo_surfaces;
75extern const struct brw_tracked_state brw_vs_image_surfaces;
76extern const struct brw_tracked_state brw_tcs_ubo_surfaces;
77extern const struct brw_tracked_state brw_tcs_abo_surfaces;
78extern const struct brw_tracked_state brw_tcs_image_surfaces;
79extern const struct brw_tracked_state brw_tes_ubo_surfaces;
80extern const struct brw_tracked_state brw_tes_abo_surfaces;
81extern const struct brw_tracked_state brw_tes_image_surfaces;
82extern const struct brw_tracked_state brw_gs_ubo_surfaces;
83extern const struct brw_tracked_state brw_gs_abo_surfaces;
84extern const struct brw_tracked_state brw_gs_image_surfaces;
85extern const struct brw_tracked_state brw_vs_unit;
86extern const struct brw_tracked_state brw_renderbuffer_surfaces;
87extern const struct brw_tracked_state brw_renderbuffer_read_surfaces;
88extern const struct brw_tracked_state brw_texture_surfaces;
89extern const struct brw_tracked_state brw_wm_binding_table;
90extern const struct brw_tracked_state brw_gs_binding_table;
91extern const struct brw_tracked_state brw_tes_binding_table;
92extern const struct brw_tracked_state brw_tcs_binding_table;
93extern const struct brw_tracked_state brw_vs_binding_table;
94extern const struct brw_tracked_state brw_wm_ubo_surfaces;
95extern const struct brw_tracked_state brw_wm_abo_surfaces;
96extern const struct brw_tracked_state brw_wm_image_surfaces;
97extern const struct brw_tracked_state brw_cs_ubo_surfaces;
98extern const struct brw_tracked_state brw_cs_abo_surfaces;
99extern const struct brw_tracked_state brw_cs_image_surfaces;
100extern const struct brw_tracked_state brw_wm_unit;
101
102extern const struct brw_tracked_state brw_psp_urb_cbs;
103
104extern const struct brw_tracked_state brw_drawing_rect;
105extern const struct brw_tracked_state brw_indices;
106extern const struct brw_tracked_state brw_vertices;
107extern const struct brw_tracked_state brw_index_buffer;
108extern const struct brw_tracked_state brw_cs_state;
109extern const struct brw_tracked_state gen7_cs_push_constants;
110extern const struct brw_tracked_state gen6_binding_table_pointers;
111extern const struct brw_tracked_state gen6_blend_state;
112extern const struct brw_tracked_state gen6_clip_state;
113extern const struct brw_tracked_state gen6_sf_and_clip_viewports;
114extern const struct brw_tracked_state gen6_color_calc_state;
115extern const struct brw_tracked_state gen6_depth_stencil_state;
116extern const struct brw_tracked_state gen6_gs_state;
117extern const struct brw_tracked_state gen6_gs_push_constants;
118extern const struct brw_tracked_state gen6_gs_binding_table;
119extern const struct brw_tracked_state gen6_multisample_state;
120extern const struct brw_tracked_state gen6_renderbuffer_surfaces;
121extern const struct brw_tracked_state gen6_sampler_state;
122extern const struct brw_tracked_state gen6_scissor_state;
123extern const struct brw_tracked_state gen6_sol_surface;
124extern const struct brw_tracked_state gen6_sf_state;
125extern const struct brw_tracked_state gen6_sf_vp;
126extern const struct brw_tracked_state gen6_urb;
127extern const struct brw_tracked_state gen6_viewport_state;
128extern const struct brw_tracked_state gen6_vs_push_constants;
129extern const struct brw_tracked_state gen6_vs_state;
130extern const struct brw_tracked_state gen6_wm_push_constants;
131extern const struct brw_tracked_state gen6_wm_state;
132extern const struct brw_tracked_state gen7_depthbuffer;
133extern const struct brw_tracked_state gen7_ds_state;
134extern const struct brw_tracked_state gen7_gs_state;
135extern const struct brw_tracked_state gen7_tcs_push_constants;
136extern const struct brw_tracked_state gen7_hs_state;
137extern const struct brw_tracked_state gen7_l3_state;
138extern const struct brw_tracked_state gen7_ps_state;
139extern const struct brw_tracked_state gen7_push_constant_space;
140extern const struct brw_tracked_state gen7_sbe_state;
141extern const struct brw_tracked_state gen7_sf_clip_viewport;
142extern const struct brw_tracked_state gen7_sf_state;
143extern const struct brw_tracked_state gen7_sol_state;
144extern const struct brw_tracked_state gen7_te_state;
145extern const struct brw_tracked_state gen7_tes_push_constants;
146extern const struct brw_tracked_state gen7_urb;
147extern const struct brw_tracked_state gen7_vs_state;
148extern const struct brw_tracked_state gen7_wm_state;
149extern const struct brw_tracked_state gen7_hw_binding_tables;
150extern const struct brw_tracked_state haswell_cut_index;
151extern const struct brw_tracked_state gen8_blend_state;
152extern const struct brw_tracked_state gen8_ds_state;
153extern const struct brw_tracked_state gen8_gs_state;
154extern const struct brw_tracked_state gen8_hs_state;
155extern const struct brw_tracked_state gen8_index_buffer;
156extern const struct brw_tracked_state gen8_multisample_state;
157extern const struct brw_tracked_state gen8_pma_fix;
158extern const struct brw_tracked_state gen8_ps_blend;
159extern const struct brw_tracked_state gen8_ps_extra;
160extern const struct brw_tracked_state gen8_ps_state;
161extern const struct brw_tracked_state gen8_wm_depth_stencil;
162extern const struct brw_tracked_state gen8_wm_state;
163extern const struct brw_tracked_state gen8_raster_state;
164extern const struct brw_tracked_state gen8_sbe_state;
165extern const struct brw_tracked_state gen8_sf_state;
166extern const struct brw_tracked_state gen8_sf_clip_viewport;
167extern const struct brw_tracked_state gen8_vertices;
168extern const struct brw_tracked_state gen8_vf_topology;
169extern const struct brw_tracked_state gen8_vs_state;
170extern const struct brw_tracked_state brw_cs_work_groups_surface;
171
172static inline bool
173brw_state_dirty(const struct brw_context *brw,
174                GLuint mesa_flags, uint64_t brw_flags)
175{
176   return ((brw->NewGLState & mesa_flags) |
177           (brw->ctx.NewDriverState & brw_flags)) != 0;
178}
179
180/* brw_binding_tables.c */
181void brw_upload_binding_table(struct brw_context *brw,
182                              uint32_t packet_name,
183                              const struct brw_stage_prog_data *prog_data,
184                              struct brw_stage_state *stage_state);
185
186/* brw_misc_state.c */
187void brw_upload_invariant_state(struct brw_context *brw);
188uint32_t
189brw_depthbuffer_format(struct brw_context *brw);
190
191void brw_upload_state_base_address(struct brw_context *brw);
192
193/* gen8_depth_state.c */
194void gen8_write_pma_stall_bits(struct brw_context *brw,
195                               uint32_t pma_stall_bits);
196
197/***********************************************************************
198 * brw_state.c
199 */
200void brw_upload_render_state(struct brw_context *brw);
201void brw_render_state_finished(struct brw_context *brw);
202void brw_upload_compute_state(struct brw_context *brw);
203void brw_compute_state_finished(struct brw_context *brw);
204void brw_init_state(struct brw_context *brw);
205void brw_destroy_state(struct brw_context *brw);
206void brw_emit_select_pipeline(struct brw_context *brw,
207                              enum brw_pipeline pipeline);
208
209static inline void
210brw_select_pipeline(struct brw_context *brw, enum brw_pipeline pipeline)
211{
212   if (unlikely(brw->last_pipeline != pipeline)) {
213      assert(pipeline < BRW_NUM_PIPELINES);
214      brw_emit_select_pipeline(brw, pipeline);
215      brw->last_pipeline = pipeline;
216   }
217}
218
219/***********************************************************************
220 * brw_program_cache.c
221 */
222
223void brw_upload_cache(struct brw_cache *cache,
224                      enum brw_cache_id cache_id,
225                      const void *key,
226                      GLuint key_sz,
227                      const void *data,
228                      GLuint data_sz,
229                      const void *aux,
230                      GLuint aux_sz,
231                      uint32_t *out_offset, void *out_aux);
232
233bool brw_search_cache(struct brw_cache *cache,
234                      enum brw_cache_id cache_id,
235                      const void *key,
236                      GLuint key_size,
237                      uint32_t *inout_offset, void *inout_aux);
238
239const void *brw_find_previous_compile(struct brw_cache *cache,
240                                      enum brw_cache_id cache_id,
241                                      unsigned program_string_id);
242
243void brw_program_cache_check_size(struct brw_context *brw);
244
245void brw_init_caches( struct brw_context *brw );
246void brw_destroy_caches( struct brw_context *brw );
247
248void brw_print_program_cache(struct brw_context *brw);
249
250/***********************************************************************
251 * brw_state_batch.c
252 */
253#define BRW_BATCH_STRUCT(brw, s) \
254   intel_batchbuffer_data(brw, (s), sizeof(*(s)), RENDER_RING)
255
256void *__brw_state_batch(struct brw_context *brw,
257                        enum aub_state_struct_type type,
258                        int size,
259                        int alignment,
260                        int index,
261                        uint32_t *out_offset);
262#define brw_state_batch(brw, type, size, alignment, out_offset) \
263   __brw_state_batch(brw, type, size, alignment, 0, out_offset)
264
265/* brw_wm_surface_state.c */
266void gen4_init_vtable_surface_functions(struct brw_context *brw);
267uint32_t brw_get_surface_tiling_bits(uint32_t tiling);
268uint32_t brw_get_surface_num_multisamples(unsigned num_samples);
269
270uint32_t brw_format_for_mesa_format(mesa_format mesa_format);
271
272GLuint translate_tex_target(GLenum target);
273
274GLuint translate_tex_format(struct brw_context *brw,
275                            mesa_format mesa_format,
276                            GLenum srgb_decode);
277
278int brw_get_texture_swizzle(const struct gl_context *ctx,
279                            const struct gl_texture_object *t);
280
281void brw_emit_buffer_surface_state(struct brw_context *brw,
282                                   uint32_t *out_offset,
283                                   drm_intel_bo *bo,
284                                   unsigned buffer_offset,
285                                   unsigned surface_format,
286                                   unsigned buffer_size,
287                                   unsigned pitch,
288                                   bool rw);
289
290void brw_update_texture_surface(struct gl_context *ctx,
291                                unsigned unit, uint32_t *surf_offset,
292                                bool for_gather, uint32_t plane);
293
294uint32_t brw_update_renderbuffer_surface(struct brw_context *brw,
295                                         struct gl_renderbuffer *rb,
296                                         uint32_t flags, unsigned unit,
297                                         uint32_t surf_index);
298
299void brw_update_renderbuffer_surfaces(struct brw_context *brw,
300                                      const struct gl_framebuffer *fb,
301                                      uint32_t render_target_start,
302                                      uint32_t *surf_offset);
303
304/* gen7_wm_surface_state.c */
305void gen7_check_surface_setup(uint32_t *surf, bool is_render_target);
306void gen7_init_vtable_surface_functions(struct brw_context *brw);
307
308/* gen8_ps_state.c */
309void gen8_upload_ps_state(struct brw_context *brw,
310                          const struct brw_stage_state *stage_state,
311                          const struct brw_wm_prog_data *prog_data,
312                          uint32_t fast_clear_op);
313
314void gen8_upload_ps_extra(struct brw_context *brw,
315                          const struct brw_wm_prog_data *prog_data);
316
317/* gen7_sol_state.c */
318void gen7_upload_3dstate_so_decl_list(struct brw_context *brw,
319                                      const struct brw_vue_map *vue_map);
320void gen8_upload_3dstate_so_buffers(struct brw_context *brw);
321
322/* gen8_surface_state.c */
323
324void gen8_init_vtable_surface_functions(struct brw_context *brw);
325
326/* brw_sampler_state.c */
327void brw_emit_sampler_state(struct brw_context *brw,
328                            uint32_t *sampler_state,
329                            uint32_t batch_offset_for_sampler_state,
330                            unsigned min_filter,
331                            unsigned mag_filter,
332                            unsigned mip_filter,
333                            unsigned max_anisotropy,
334                            unsigned address_rounding,
335                            unsigned wrap_s,
336                            unsigned wrap_t,
337                            unsigned wrap_r,
338                            unsigned base_level,
339                            unsigned min_lod,
340                            unsigned max_lod,
341                            int lod_bias,
342                            unsigned shadow_function,
343                            bool non_normalized_coordinates,
344                            uint32_t border_color_offset);
345
346/* gen6_wm_state.c */
347void
348gen6_upload_wm_state(struct brw_context *brw,
349                     const struct brw_wm_prog_data *prog_data,
350                     const struct brw_stage_state *stage_state,
351                     bool multisampled_fbo,
352                     bool dual_source_blend_enable, bool kill_enable,
353                     bool color_buffer_write_enable, bool msaa_enabled,
354                     bool line_stipple_enable, bool polygon_stipple_enable,
355                     bool statistic_enable);
356
357/* gen6_sf_state.c */
358void
359calculate_attr_overrides(const struct brw_context *brw,
360                         uint16_t *attr_overrides,
361                         uint32_t *point_sprite_enables,
362                         uint32_t *urb_entry_read_length,
363                         uint32_t *urb_entry_read_offset);
364
365/* gen6_surface_state.c */
366void gen6_init_vtable_surface_functions(struct brw_context *brw);
367
368/* brw_vs_surface_state.c */
369void
370brw_upload_pull_constants(struct brw_context *brw,
371                          GLbitfield64 brw_new_constbuf,
372                          const struct gl_program *prog,
373                          struct brw_stage_state *stage_state,
374                          const struct brw_stage_prog_data *prog_data);
375
376/* gen7_vs_state.c */
377void
378gen7_upload_constant_state(struct brw_context *brw,
379                           const struct brw_stage_state *stage_state,
380                           bool active, unsigned opcode);
381
382void gen7_rs_control(struct brw_context *brw, int enable);
383
384void gen7_edit_hw_binding_table_entry(struct brw_context *brw,
385                                      gl_shader_stage stage,
386                                      uint32_t index,
387                                      uint32_t surf_offset);
388void gen7_update_binding_table_from_array(struct brw_context *brw,
389                                          gl_shader_stage stage,
390                                          const uint32_t* binding_table,
391                                          int num_surfaces);
392void gen7_enable_hw_binding_tables(struct brw_context *brw);
393void gen7_disable_hw_binding_tables(struct brw_context *brw);
394void gen7_reset_hw_bt_pool_offsets(struct brw_context *brw);
395
396/* brw_clip.c */
397void brw_upload_clip_prog(struct brw_context *brw);
398
399/* brw_sf.c */
400void brw_upload_sf_prog(struct brw_context *brw);
401
402bool brw_is_drawing_points(const struct brw_context *brw);
403bool brw_is_drawing_lines(const struct brw_context *brw);
404
405/* gen7_l3_state.c */
406void
407gen7_restore_default_l3_config(struct brw_context *brw);
408
409static inline bool
410use_state_point_size(const struct brw_context *brw)
411{
412   const struct gl_context *ctx = &brw->ctx;
413
414   /* Section 14.4 (Points) of the OpenGL 4.5 specification says:
415    *
416    *    "If program point size mode is enabled, the derived point size is
417    *     taken from the (potentially clipped) shader built-in gl_PointSize
418    *     written by:
419    *
420    *        * the geometry shader, if active;
421    *        * the tessellation evaluation shader, if active and no
422    *          geometry shader is active;
423    *        * the vertex shader, otherwise
424    *
425    *    and clamped to the implementation-dependent point size range.  If
426    *    the value written to gl_PointSize is less than or equal to zero,
427    *    or if no value was written to gl_PointSize, results are undefined.
428    *    If program point size mode is disabled, the derived point size is
429    *    specified with the command
430    *
431    *       void PointSize(float size);
432    *
433    *    size specifies the requested size of a point.  The default value
434    *    is 1.0."
435    *
436    * The rules for GLES come from the ES 3.2, OES_geometry_point_size, and
437    * OES_tessellation_point_size specifications.  To summarize: if the last
438    * stage before rasterization is a GS or TES, then use gl_PointSize from
439    * the shader if written.  Otherwise, use 1.0.  If the last stage is a
440    * vertex shader, use gl_PointSize, or it is undefined.
441    *
442    * We can combine these rules into a single condition for both APIs.
443    * Using the state point size when the last shader stage doesn't write
444    * gl_PointSize satisfies GL's requirements, as it's undefined.  Because
445    * ES doesn't have a PointSize() command, the state point size will
446    * remain 1.0, satisfying the ES default value in the GS/TES case, and
447    * the VS case (1.0 works for "undefined").  Mesa sets the program point
448    * mode flag to always-enabled in ES, so we can safely check that, and
449    * it'll be ignored for ES.
450    *
451    * _NEW_PROGRAM | _NEW_POINT
452    * BRW_NEW_VUE_MAP_GEOM_OUT
453    */
454   return (!ctx->VertexProgram.PointSizeEnabled && !ctx->Point._Attenuated) ||
455          (brw->vue_map_geom_out.slots_valid & VARYING_BIT_PSIZ) == 0;
456}
457
458void brw_calculate_guardband_size(const struct gen_device_info *devinfo,
459                                  uint32_t fb_width, uint32_t fb_height,
460                                  float m00, float m11, float m30, float m31,
461                                  float *xmin, float *xmax,
462                                  float *ymin, float *ymax);
463
464#ifdef __cplusplus
465}
466#endif
467
468#endif
469