Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2010 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21.  * IN THE SOFTWARE.
  22.  */
  23.  
  24. extern "C" {
  25. #include "main/macros.h"
  26. #include "brw_context.h"
  27. #include "brw_vs.h"
  28. }
  29. #include "brw_fs.h"
  30. #include "glsl/ir_optimization.h"
  31. #include "glsl/glsl_parser_extras.h"
  32.  
  33. struct gl_shader *
  34. brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
  35. {
  36.    struct brw_shader *shader;
  37.  
  38.    shader = rzalloc(NULL, struct brw_shader);
  39.    if (shader) {
  40.       shader->base.Type = type;
  41.       shader->base.Name = name;
  42.       _mesa_init_shader(ctx, &shader->base);
  43.    }
  44.  
  45.    return &shader->base;
  46. }
  47.  
  48. struct gl_shader_program *
  49. brw_new_shader_program(struct gl_context *ctx, GLuint name)
  50. {
  51.    struct gl_shader_program *prog = rzalloc(NULL, struct gl_shader_program);
  52.    if (prog) {
  53.       prog->Name = name;
  54.       _mesa_init_shader_program(ctx, prog);
  55.    }
  56.    return prog;
  57. }
  58.  
  59. /**
  60.  * Performs a compile of the shader stages even when we don't know
  61.  * what non-orthogonal state will be set, in the hope that it reflects
  62.  * the eventual NOS used, and thus allows us to produce link failures.
  63.  */
  64. static bool
  65. brw_shader_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
  66. {
  67.    struct brw_context *brw = brw_context(ctx);
  68.  
  69.    if (brw->precompile && !brw_fs_precompile(ctx, prog))
  70.       return false;
  71.  
  72.    if (brw->precompile && !brw_vs_precompile(ctx, prog))
  73.       return false;
  74.  
  75.    return true;
  76. }
  77.  
  78. static void
  79. brw_lower_packing_builtins(struct brw_context *brw,
  80.                            gl_shader_type shader_type,
  81.                            exec_list *ir)
  82. {
  83.    int ops = LOWER_PACK_SNORM_2x16
  84.            | LOWER_UNPACK_SNORM_2x16
  85.            | LOWER_PACK_UNORM_2x16
  86.            | LOWER_UNPACK_UNORM_2x16
  87.            | LOWER_PACK_SNORM_4x8
  88.            | LOWER_UNPACK_SNORM_4x8
  89.            | LOWER_PACK_UNORM_4x8
  90.            | LOWER_UNPACK_UNORM_4x8;
  91.  
  92.    if (brw->gen >= 7) {
  93.       /* Gen7 introduced the f32to16 and f16to32 instructions, which can be
  94.        * used to execute packHalf2x16 and unpackHalf2x16. For AOS code, no
  95.        * lowering is needed. For SOA code, the Half2x16 ops must be
  96.        * scalarized.
  97.        */
  98.       if (shader_type == MESA_SHADER_FRAGMENT) {
  99.          ops |= LOWER_PACK_HALF_2x16_TO_SPLIT
  100.              |  LOWER_UNPACK_HALF_2x16_TO_SPLIT;
  101.       }
  102.    } else {
  103.       ops |= LOWER_PACK_HALF_2x16
  104.           |  LOWER_UNPACK_HALF_2x16;
  105.    }
  106.  
  107.    lower_packing_builtins(ir, ops);
  108. }
  109.  
  110. GLboolean
  111. brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
  112. {
  113.    struct brw_context *brw = brw_context(ctx);
  114.    unsigned int stage;
  115.  
  116.    for (stage = 0; stage < ARRAY_SIZE(shProg->_LinkedShaders); stage++) {
  117.       struct brw_shader *shader =
  118.          (struct brw_shader *)shProg->_LinkedShaders[stage];
  119.  
  120.       if (!shader)
  121.          continue;
  122.  
  123.       struct gl_program *prog =
  124.          ctx->Driver.NewProgram(ctx, _mesa_program_index_to_target(stage),
  125.                                 shader->base.Name);
  126.       if (!prog)
  127.         return false;
  128.       prog->Parameters = _mesa_new_parameter_list();
  129.  
  130.       if (stage == 0) {
  131.          struct gl_vertex_program *vp = (struct gl_vertex_program *) prog;
  132.          vp->UsesClipDistance = shProg->Vert.UsesClipDistance;
  133.       }
  134.  
  135.       void *mem_ctx = ralloc_context(NULL);
  136.       bool progress;
  137.  
  138.       if (shader->ir)
  139.          ralloc_free(shader->ir);
  140.       shader->ir = new(shader) exec_list;
  141.       clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
  142.  
  143.       /* lower_packing_builtins() inserts arithmetic instructions, so it
  144.        * must precede lower_instructions().
  145.        */
  146.       brw_lower_packing_builtins(brw, (gl_shader_type) stage, shader->ir);
  147.       do_mat_op_to_vec(shader->ir);
  148.       const int bitfield_insert = brw->gen >= 7
  149.                                   ? BITFIELD_INSERT_TO_BFM_BFI
  150.                                   : 0;
  151.       const int lrp_to_arith = brw->gen < 6 ? LRP_TO_ARITH : 0;
  152.       lower_instructions(shader->ir,
  153.                          MOD_TO_FRACT |
  154.                          DIV_TO_MUL_RCP |
  155.                          SUB_TO_ADD_NEG |
  156.                          EXP_TO_EXP2 |
  157.                          LOG_TO_LOG2 |
  158.                          bitfield_insert |
  159.                          lrp_to_arith);
  160.  
  161.       /* Pre-gen6 HW can only nest if-statements 16 deep.  Beyond this,
  162.        * if-statements need to be flattened.
  163.        */
  164.       if (brw->gen < 6)
  165.          lower_if_to_cond_assign(shader->ir, 16);
  166.  
  167.       do_lower_texture_projection(shader->ir);
  168.       brw_lower_texture_gradients(brw, shader->ir);
  169.       do_vec_index_to_cond_assign(shader->ir);
  170.       lower_vector_insert(shader->ir, true);
  171.       brw_do_cubemap_normalize(shader->ir);
  172.       lower_noise(shader->ir);
  173.       lower_quadop_vector(shader->ir, false);
  174.  
  175.       bool input = true;
  176.       bool output = stage == MESA_SHADER_FRAGMENT;
  177.       bool temp = stage == MESA_SHADER_FRAGMENT;
  178.       bool uniform = false;
  179.  
  180.       bool lowered_variable_indexing =
  181.          lower_variable_index_to_cond_assign(shader->ir,
  182.                                              input, output, temp, uniform);
  183.  
  184.       if (unlikely(brw->perf_debug && lowered_variable_indexing)) {
  185.          perf_debug("Unsupported form of variable indexing in FS; falling "
  186.                     "back to very inefficient code generation\n");
  187.       }
  188.  
  189.       /* FINISHME: Do this before the variable index lowering. */
  190.       lower_ubo_reference(&shader->base, shader->ir);
  191.  
  192.       do {
  193.          progress = false;
  194.  
  195.          if (stage == MESA_SHADER_FRAGMENT) {
  196.             brw_do_channel_expressions(shader->ir);
  197.             brw_do_vector_splitting(shader->ir);
  198.          }
  199.  
  200.          progress = do_lower_jumps(shader->ir, true, true,
  201.                                    true, /* main return */
  202.                                    false, /* continue */
  203.                                    false /* loops */
  204.                                    ) || progress;
  205.  
  206.          progress = do_common_optimization(shader->ir, true, true, 32,
  207.                                            &ctx->ShaderCompilerOptions[stage])
  208.            || progress;
  209.       } while (progress);
  210.  
  211.       /* Make a pass over the IR to add state references for any built-in
  212.        * uniforms that are used.  This has to be done now (during linking).
  213.        * Code generation doesn't happen until the first time this shader is
  214.        * used for rendering.  Waiting until then to generate the parameters is
  215.        * too late.  At that point, the values for the built-in uniforms won't
  216.        * get sent to the shader.
  217.        */
  218.       foreach_list(node, shader->ir) {
  219.          ir_variable *var = ((ir_instruction *) node)->as_variable();
  220.  
  221.          if ((var == NULL) || (var->mode != ir_var_uniform)
  222.              || (strncmp(var->name, "gl_", 3) != 0))
  223.             continue;
  224.  
  225.          const ir_state_slot *const slots = var->state_slots;
  226.          assert(var->state_slots != NULL);
  227.  
  228.          for (unsigned int i = 0; i < var->num_state_slots; i++) {
  229.             _mesa_add_state_reference(prog->Parameters,
  230.                                       (gl_state_index *) slots[i].tokens);
  231.          }
  232.       }
  233.  
  234.       validate_ir_tree(shader->ir);
  235.  
  236.       reparent_ir(shader->ir, shader->ir);
  237.       ralloc_free(mem_ctx);
  238.  
  239.       do_set_program_inouts(shader->ir, prog,
  240.                             shader->base.Type == GL_FRAGMENT_SHADER);
  241.  
  242.       prog->SamplersUsed = shader->base.active_samplers;
  243.       _mesa_update_shader_textures_used(shProg, prog);
  244.  
  245.       _mesa_reference_program(ctx, &shader->base.Program, prog);
  246.  
  247.       brw_add_texrect_params(prog);
  248.  
  249.       /* This has to be done last.  Any operation that can cause
  250.        * prog->ParameterValues to get reallocated (e.g., anything that adds a
  251.        * program constant) has to happen before creating this linkage.
  252.        */
  253.       _mesa_associate_uniform_storage(ctx, shProg, prog->Parameters);
  254.  
  255.       _mesa_reference_program(ctx, &prog, NULL);
  256.  
  257.       if (ctx->Shader.Flags & GLSL_DUMP) {
  258.          printf("\n");
  259.          printf("GLSL IR for linked %s program %d:\n",
  260.                 _mesa_glsl_shader_target_name(shader->base.Type), shProg->Name);
  261.          _mesa_print_ir(shader->base.ir, NULL);
  262.          printf("\n");
  263.       }
  264.    }
  265.  
  266.    if (ctx->Shader.Flags & GLSL_DUMP) {
  267.       for (unsigned i = 0; i < shProg->NumShaders; i++) {
  268.          const struct gl_shader *sh = shProg->Shaders[i];
  269.          if (!sh)
  270.             continue;
  271.  
  272.          printf("GLSL %s shader %d source for linked program %d:\n",
  273.                 _mesa_glsl_shader_target_name(sh->Type),
  274.                 i,
  275.                 shProg->Name);
  276.          printf("%s", sh->Source);
  277.          printf("\n");
  278.       }
  279.    }
  280.  
  281.    if (!brw_shader_precompile(ctx, shProg))
  282.       return false;
  283.  
  284.    return true;
  285. }
  286.  
  287.  
  288. int
  289. brw_type_for_base_type(const struct glsl_type *type)
  290. {
  291.    switch (type->base_type) {
  292.    case GLSL_TYPE_FLOAT:
  293.       return BRW_REGISTER_TYPE_F;
  294.    case GLSL_TYPE_INT:
  295.    case GLSL_TYPE_BOOL:
  296.       return BRW_REGISTER_TYPE_D;
  297.    case GLSL_TYPE_UINT:
  298.       return BRW_REGISTER_TYPE_UD;
  299.    case GLSL_TYPE_ARRAY:
  300.       return brw_type_for_base_type(type->fields.array);
  301.    case GLSL_TYPE_STRUCT:
  302.    case GLSL_TYPE_SAMPLER:
  303.       /* These should be overridden with the type of the member when
  304.        * dereferenced into.  BRW_REGISTER_TYPE_UD seems like a likely
  305.        * way to trip up if we don't.
  306.        */
  307.       return BRW_REGISTER_TYPE_UD;
  308.    case GLSL_TYPE_VOID:
  309.    case GLSL_TYPE_ERROR:
  310.    case GLSL_TYPE_INTERFACE:
  311.       assert(!"not reached");
  312.       break;
  313.    }
  314.  
  315.    return BRW_REGISTER_TYPE_F;
  316. }
  317.  
  318. uint32_t
  319. brw_conditional_for_comparison(unsigned int op)
  320. {
  321.    switch (op) {
  322.    case ir_binop_less:
  323.       return BRW_CONDITIONAL_L;
  324.    case ir_binop_greater:
  325.       return BRW_CONDITIONAL_G;
  326.    case ir_binop_lequal:
  327.       return BRW_CONDITIONAL_LE;
  328.    case ir_binop_gequal:
  329.       return BRW_CONDITIONAL_GE;
  330.    case ir_binop_equal:
  331.    case ir_binop_all_equal: /* same as equal for scalars */
  332.       return BRW_CONDITIONAL_Z;
  333.    case ir_binop_nequal:
  334.    case ir_binop_any_nequal: /* same as nequal for scalars */
  335.       return BRW_CONDITIONAL_NZ;
  336.    default:
  337.       assert(!"not reached: bad operation for comparison");
  338.       return BRW_CONDITIONAL_NZ;
  339.    }
  340. }
  341.  
  342. uint32_t
  343. brw_math_function(enum opcode op)
  344. {
  345.    switch (op) {
  346.    case SHADER_OPCODE_RCP:
  347.       return BRW_MATH_FUNCTION_INV;
  348.    case SHADER_OPCODE_RSQ:
  349.       return BRW_MATH_FUNCTION_RSQ;
  350.    case SHADER_OPCODE_SQRT:
  351.       return BRW_MATH_FUNCTION_SQRT;
  352.    case SHADER_OPCODE_EXP2:
  353.       return BRW_MATH_FUNCTION_EXP;
  354.    case SHADER_OPCODE_LOG2:
  355.       return BRW_MATH_FUNCTION_LOG;
  356.    case SHADER_OPCODE_POW:
  357.       return BRW_MATH_FUNCTION_POW;
  358.    case SHADER_OPCODE_SIN:
  359.       return BRW_MATH_FUNCTION_SIN;
  360.    case SHADER_OPCODE_COS:
  361.       return BRW_MATH_FUNCTION_COS;
  362.    case SHADER_OPCODE_INT_QUOTIENT:
  363.       return BRW_MATH_FUNCTION_INT_DIV_QUOTIENT;
  364.    case SHADER_OPCODE_INT_REMAINDER:
  365.       return BRW_MATH_FUNCTION_INT_DIV_REMAINDER;
  366.    default:
  367.       assert(!"not reached: unknown math function");
  368.       return 0;
  369.    }
  370. }
  371.  
  372. uint32_t
  373. brw_texture_offset(ir_constant *offset)
  374. {
  375.    assert(offset != NULL);
  376.  
  377.    signed char offsets[3];
  378.    for (unsigned i = 0; i < offset->type->vector_elements; i++)
  379.       offsets[i] = (signed char) offset->value.i[i];
  380.  
  381.    /* Combine all three offsets into a single unsigned dword:
  382.     *
  383.     *    bits 11:8 - U Offset (X component)
  384.     *    bits  7:4 - V Offset (Y component)
  385.     *    bits  3:0 - R Offset (Z component)
  386.     */
  387.    unsigned offset_bits = 0;
  388.    for (unsigned i = 0; i < offset->type->vector_elements; i++) {
  389.       const unsigned shift = 4 * (2 - i);
  390.       offset_bits |= (offsets[i] << shift) & (0xF << shift);
  391.    }
  392.    return offset_bits;
  393. }
  394.  
  395. const char *
  396. brw_instruction_name(enum opcode op)
  397. {
  398.    char *fallback;
  399.  
  400.    if (op < ARRAY_SIZE(opcode_descs) && opcode_descs[op].name)
  401.       return opcode_descs[op].name;
  402.  
  403.    switch (op) {
  404.    case FS_OPCODE_FB_WRITE:
  405.       return "fb_write";
  406.  
  407.    case SHADER_OPCODE_RCP:
  408.       return "rcp";
  409.    case SHADER_OPCODE_RSQ:
  410.       return "rsq";
  411.    case SHADER_OPCODE_SQRT:
  412.       return "sqrt";
  413.    case SHADER_OPCODE_EXP2:
  414.       return "exp2";
  415.    case SHADER_OPCODE_LOG2:
  416.       return "log2";
  417.    case SHADER_OPCODE_POW:
  418.       return "pow";
  419.    case SHADER_OPCODE_INT_QUOTIENT:
  420.       return "int_quot";
  421.    case SHADER_OPCODE_INT_REMAINDER:
  422.       return "int_rem";
  423.    case SHADER_OPCODE_SIN:
  424.       return "sin";
  425.    case SHADER_OPCODE_COS:
  426.       return "cos";
  427.  
  428.    case SHADER_OPCODE_TEX:
  429.       return "tex";
  430.    case SHADER_OPCODE_TXD:
  431.       return "txd";
  432.    case SHADER_OPCODE_TXF:
  433.       return "txf";
  434.    case SHADER_OPCODE_TXL:
  435.       return "txl";
  436.    case SHADER_OPCODE_TXS:
  437.       return "txs";
  438.    case FS_OPCODE_TXB:
  439.       return "txb";
  440.    case SHADER_OPCODE_TXF_MS:
  441.       return "txf_ms";
  442.  
  443.    case FS_OPCODE_DDX:
  444.       return "ddx";
  445.    case FS_OPCODE_DDY:
  446.       return "ddy";
  447.  
  448.    case FS_OPCODE_PIXEL_X:
  449.       return "pixel_x";
  450.    case FS_OPCODE_PIXEL_Y:
  451.       return "pixel_y";
  452.  
  453.    case FS_OPCODE_CINTERP:
  454.       return "cinterp";
  455.    case FS_OPCODE_LINTERP:
  456.       return "linterp";
  457.  
  458.    case FS_OPCODE_SPILL:
  459.       return "spill";
  460.    case FS_OPCODE_UNSPILL:
  461.       return "unspill";
  462.  
  463.    case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
  464.       return "uniform_pull_const";
  465.    case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
  466.       return "uniform_pull_const_gen7";
  467.    case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
  468.       return "varying_pull_const";
  469.    case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
  470.       return "varying_pull_const_gen7";
  471.  
  472.    case FS_OPCODE_MOV_DISPATCH_TO_FLAGS:
  473.       return "mov_dispatch_to_flags";
  474.    case FS_OPCODE_DISCARD_JUMP:
  475.       return "discard_jump";
  476.  
  477.    case FS_OPCODE_SET_SIMD4X2_OFFSET:
  478.       return "set_simd4x2_offset";
  479.  
  480.    case FS_OPCODE_PACK_HALF_2x16_SPLIT:
  481.       return "pack_half_2x16_split";
  482.    case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X:
  483.       return "unpack_half_2x16_split_x";
  484.    case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y:
  485.       return "unpack_half_2x16_split_y";
  486.  
  487.    case FS_OPCODE_PLACEHOLDER_HALT:
  488.       return "placeholder_halt";
  489.  
  490.    case VS_OPCODE_URB_WRITE:
  491.       return "urb_write";
  492.    case VS_OPCODE_SCRATCH_READ:
  493.       return "scratch_read";
  494.    case VS_OPCODE_SCRATCH_WRITE:
  495.       return "scratch_write";
  496.    case VS_OPCODE_PULL_CONSTANT_LOAD:
  497.       return "pull_constant_load";
  498.    case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
  499.       return "pull_constant_load_gen7";
  500.  
  501.    default:
  502.       /* Yes, this leaks.  It's in debug code, it should never occur, and if
  503.        * it does, you should just add the case to the list above.
  504.        */
  505. //      asprintf(&fallback, "op%d", op);
  506.       return fallback;
  507.    }
  508. }
  509.  
  510. bool
  511. backend_instruction::is_tex()
  512. {
  513.    return (opcode == SHADER_OPCODE_TEX ||
  514.            opcode == FS_OPCODE_TXB ||
  515.            opcode == SHADER_OPCODE_TXD ||
  516.            opcode == SHADER_OPCODE_TXF ||
  517.            opcode == SHADER_OPCODE_TXF_MS ||
  518.            opcode == SHADER_OPCODE_TXL ||
  519.            opcode == SHADER_OPCODE_TXS ||
  520.            opcode == SHADER_OPCODE_LOD);
  521. }
  522.  
  523. bool
  524. backend_instruction::is_math()
  525. {
  526.    return (opcode == SHADER_OPCODE_RCP ||
  527.            opcode == SHADER_OPCODE_RSQ ||
  528.            opcode == SHADER_OPCODE_SQRT ||
  529.            opcode == SHADER_OPCODE_EXP2 ||
  530.            opcode == SHADER_OPCODE_LOG2 ||
  531.            opcode == SHADER_OPCODE_SIN ||
  532.            opcode == SHADER_OPCODE_COS ||
  533.            opcode == SHADER_OPCODE_INT_QUOTIENT ||
  534.            opcode == SHADER_OPCODE_INT_REMAINDER ||
  535.            opcode == SHADER_OPCODE_POW);
  536. }
  537.  
  538. bool
  539. backend_instruction::is_control_flow()
  540. {
  541.    switch (opcode) {
  542.    case BRW_OPCODE_DO:
  543.    case BRW_OPCODE_WHILE:
  544.    case BRW_OPCODE_IF:
  545.    case BRW_OPCODE_ELSE:
  546.    case BRW_OPCODE_ENDIF:
  547.    case BRW_OPCODE_BREAK:
  548.    case BRW_OPCODE_CONTINUE:
  549.       return true;
  550.    default:
  551.       return false;
  552.    }
  553. }
  554.  
  555. void
  556. backend_visitor::dump_instructions()
  557. {
  558.    int ip = 0;
  559.    foreach_list(node, &this->instructions) {
  560.       backend_instruction *inst = (backend_instruction *)node;
  561.       printf("%d: ", ip++);
  562.       dump_instruction(inst);
  563.    }
  564. }
  565.