101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2010 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#ifndef BRW_SHADER_H
2501e04c3fSmrg#define BRW_SHADER_H
2601e04c3fSmrg
2701e04c3fSmrg#include <stdint.h>
287ec681f3Smrg#include "brw_cfg.h"
2901e04c3fSmrg#include "brw_compiler.h"
3001e04c3fSmrg#include "compiler/nir/nir.h"
3101e04c3fSmrg
3201e04c3fSmrg#ifdef __cplusplus
337ec681f3Smrg#include "brw_ir_analysis.h"
3401e04c3fSmrg#include "brw_ir_allocator.h"
3501e04c3fSmrg
3601e04c3fSmrgenum instruction_scheduler_mode {
3701e04c3fSmrg   SCHEDULE_PRE,
3801e04c3fSmrg   SCHEDULE_PRE_NON_LIFO,
3901e04c3fSmrg   SCHEDULE_PRE_LIFO,
4001e04c3fSmrg   SCHEDULE_POST,
4101e04c3fSmrg};
4201e04c3fSmrg
437ec681f3Smrg#define UBO_START ((1 << 16) - 4)
447ec681f3Smrg
4501e04c3fSmrgstruct backend_shader {
4601e04c3fSmrgprotected:
4701e04c3fSmrg
4801e04c3fSmrg   backend_shader(const struct brw_compiler *compiler,
4901e04c3fSmrg                  void *log_data,
5001e04c3fSmrg                  void *mem_ctx,
5101e04c3fSmrg                  const nir_shader *shader,
527ec681f3Smrg                  struct brw_stage_prog_data *stage_prog_data,
537ec681f3Smrg                  bool debug_enabled);
5401e04c3fSmrg
5501e04c3fSmrgpublic:
5601e04c3fSmrg   virtual ~backend_shader();
5701e04c3fSmrg
5801e04c3fSmrg   const struct brw_compiler *compiler;
5901e04c3fSmrg   void *log_data; /* Passed to compiler->*_log functions */
6001e04c3fSmrg
617ec681f3Smrg   const struct intel_device_info * const devinfo;
6201e04c3fSmrg   const nir_shader *nir;
6301e04c3fSmrg   struct brw_stage_prog_data * const stage_prog_data;
6401e04c3fSmrg
6501e04c3fSmrg   /** ralloc context for temporary data used during compile */
6601e04c3fSmrg   void *mem_ctx;
6701e04c3fSmrg
6801e04c3fSmrg   /**
6901e04c3fSmrg    * List of either fs_inst or vec4_instruction (inheriting from
7001e04c3fSmrg    * backend_instruction)
7101e04c3fSmrg    */
7201e04c3fSmrg   exec_list instructions;
7301e04c3fSmrg
7401e04c3fSmrg   cfg_t *cfg;
757ec681f3Smrg   brw_analysis<brw::idom_tree, backend_shader> idom_analysis;
7601e04c3fSmrg
7701e04c3fSmrg   gl_shader_stage stage;
7801e04c3fSmrg   bool debug_enabled;
7901e04c3fSmrg   const char *stage_name;
8001e04c3fSmrg   const char *stage_abbrev;
8101e04c3fSmrg
8201e04c3fSmrg   brw::simple_allocator alloc;
8301e04c3fSmrg
847ec681f3Smrg   virtual void dump_instruction(const backend_instruction *inst) const = 0;
857ec681f3Smrg   virtual void dump_instruction(const backend_instruction *inst, FILE *file) const = 0;
867ec681f3Smrg   virtual void dump_instructions() const;
877ec681f3Smrg   virtual void dump_instructions(const char *name) const;
8801e04c3fSmrg
8901e04c3fSmrg   void calculate_cfg();
9001e04c3fSmrg
917ec681f3Smrg   virtual void invalidate_analysis(brw::analysis_dependency_class c);
9201e04c3fSmrg};
9301e04c3fSmrg
9401e04c3fSmrg#else
9501e04c3fSmrgstruct backend_shader;
9601e04c3fSmrg#endif /* __cplusplus */
9701e04c3fSmrg
9801e04c3fSmrgenum brw_reg_type brw_type_for_base_type(const struct glsl_type *type);
9901e04c3fSmrgenum brw_conditional_mod brw_conditional_for_comparison(unsigned int op);
10001e04c3fSmrguint32_t brw_math_function(enum opcode op);
1017ec681f3Smrgconst char *brw_instruction_name(const struct intel_device_info *devinfo,
10201e04c3fSmrg                                 enum opcode op);
10301e04c3fSmrgbool brw_saturate_immediate(enum brw_reg_type type, struct brw_reg *reg);
10401e04c3fSmrgbool brw_negate_immediate(enum brw_reg_type type, struct brw_reg *reg);
10501e04c3fSmrgbool brw_abs_immediate(enum brw_reg_type type, struct brw_reg *reg);
10601e04c3fSmrg
10701e04c3fSmrgbool opt_predicated_break(struct backend_shader *s);
10801e04c3fSmrg
10901e04c3fSmrg#ifdef __cplusplus
11001e04c3fSmrgextern "C" {
11101e04c3fSmrg#endif
11201e04c3fSmrg
11301e04c3fSmrg/* brw_fs_reg_allocate.cpp */
11401e04c3fSmrgvoid brw_fs_alloc_reg_sets(struct brw_compiler *compiler);
11501e04c3fSmrg
11601e04c3fSmrg/* brw_vec4_reg_allocate.cpp */
11701e04c3fSmrgvoid brw_vec4_alloc_reg_set(struct brw_compiler *compiler);
11801e04c3fSmrg
11901e04c3fSmrg/* brw_disasm.c */
12001e04c3fSmrgextern const char *const conditional_modifier[16];
12101e04c3fSmrgextern const char *const pred_ctrl_align16[16];
12201e04c3fSmrg
12301e04c3fSmrg/* Per-thread scratch space is a power-of-two multiple of 1KB. */
12401e04c3fSmrgstatic inline int
12501e04c3fSmrgbrw_get_scratch_size(int size)
12601e04c3fSmrg{
12701e04c3fSmrg   return MAX2(1024, util_next_power_of_two(size));
12801e04c3fSmrg}
12901e04c3fSmrg
1307ec681f3Smrg
1317ec681f3Smrgstatic inline nir_variable_mode
1327ec681f3Smrgbrw_nir_no_indirect_mask(const struct brw_compiler *compiler,
1337ec681f3Smrg                         gl_shader_stage stage)
1347ec681f3Smrg{
1357ec681f3Smrg   const struct intel_device_info *devinfo = compiler->devinfo;
1367ec681f3Smrg   const bool is_scalar = compiler->scalar_stage[stage];
1377ec681f3Smrg   nir_variable_mode indirect_mask = (nir_variable_mode) 0;
1387ec681f3Smrg
1397ec681f3Smrg   switch (stage) {
1407ec681f3Smrg   case MESA_SHADER_VERTEX:
1417ec681f3Smrg   case MESA_SHADER_FRAGMENT:
1427ec681f3Smrg      indirect_mask |= nir_var_shader_in;
1437ec681f3Smrg      break;
1447ec681f3Smrg
1457ec681f3Smrg   case MESA_SHADER_GEOMETRY:
1467ec681f3Smrg      if (!is_scalar)
1477ec681f3Smrg         indirect_mask |= nir_var_shader_in;
1487ec681f3Smrg      break;
1497ec681f3Smrg
1507ec681f3Smrg   default:
1517ec681f3Smrg      /* Everything else can handle indirect inputs */
1527ec681f3Smrg      break;
1537ec681f3Smrg   }
1547ec681f3Smrg
1557ec681f3Smrg   if (is_scalar && stage != MESA_SHADER_TESS_CTRL)
1567ec681f3Smrg      indirect_mask |= nir_var_shader_out;
1577ec681f3Smrg
1587ec681f3Smrg   /* On HSW+, we allow indirects in scalar shaders.  They get implemented
1597ec681f3Smrg    * using nir_lower_vars_to_explicit_types and nir_lower_explicit_io in
1607ec681f3Smrg    * brw_postprocess_nir.
1617ec681f3Smrg    *
1627ec681f3Smrg    * We haven't plumbed through the indirect scratch messages on gfx6 or
1637ec681f3Smrg    * earlier so doing indirects via scratch doesn't work there. On gfx7 and
1647ec681f3Smrg    * earlier the scratch space size is limited to 12kB.  If we allowed
1657ec681f3Smrg    * indirects as scratch all the time, we may easily exceed this limit
1667ec681f3Smrg    * without having any fallback.
1677ec681f3Smrg    */
1687ec681f3Smrg   if (is_scalar && devinfo->verx10 <= 70)
1697ec681f3Smrg      indirect_mask |= nir_var_function_temp;
1707ec681f3Smrg
1717ec681f3Smrg   return indirect_mask;
1727ec681f3Smrg}
1737ec681f3Smrg
1747ec681f3Smrgbool brw_texture_offset(const nir_tex_instr *tex, unsigned src,
1757ec681f3Smrg                        uint32_t *offset_bits);
1767ec681f3Smrg
17701e04c3fSmrg/**
17801e04c3fSmrg * Scratch data used when compiling a GLSL geometry shader.
17901e04c3fSmrg */
18001e04c3fSmrgstruct brw_gs_compile
18101e04c3fSmrg{
18201e04c3fSmrg   struct brw_gs_prog_key key;
18301e04c3fSmrg   struct brw_vue_map input_vue_map;
18401e04c3fSmrg
18501e04c3fSmrg   unsigned control_data_bits_per_vertex;
18601e04c3fSmrg   unsigned control_data_header_size_bits;
18701e04c3fSmrg};
18801e04c3fSmrg
18901e04c3fSmrg#ifdef __cplusplus
19001e04c3fSmrg}
19101e04c3fSmrg#endif
19201e04c3fSmrg
19301e04c3fSmrg#endif /* BRW_SHADER_H */
194