r600g: Close a memory leak of llvm byte streams
[mesa.git] / src / gallium / drivers / r600 / r600_shader.c
index 32d5a78758f21fd08ddcc7791b90a4bc4f4602d7..736165b8083c80ee5c7950a4b40249619300e18e 100644 (file)
@@ -253,11 +253,23 @@ int r600_compute_shader_create(struct pipe_context * ctx,
        if (dump) {
                r600_bytecode_dump(shader_ctx.bc);
        }
+       free(bytes);
        return 1;
 }
 
 #endif /* HAVE_OPENCL */
 
+static uint32_t i32_from_byte_stream(unsigned char * bytes,
+               unsigned * bytes_read)
+{
+       unsigned i;
+       uint32_t out = 0;
+       for (i = 0; i < 4; i++) {
+               out |= bytes[(*bytes_read)++] << (8 * i);
+       }
+       return out;
+}
+
 static unsigned r600_src_from_byte_stream(unsigned char * bytes,
                unsigned bytes_read, struct r600_bytecode_alu * alu, unsigned src_idx)
 {
@@ -282,6 +294,7 @@ static unsigned r600_alu_from_byte_stream(struct r600_shader_ctx *ctx,
 {
        unsigned src_idx;
        unsigned inst0, inst1;
+       unsigned push_modifier;
        struct r600_bytecode_alu alu;
        memset(&alu, 0, sizeof(alu));
        for(src_idx = 0; src_idx < 3; src_idx++) {
@@ -299,12 +312,32 @@ static unsigned r600_alu_from_byte_stream(struct r600_shader_ctx *ctx,
        alu.inst = inst0 | (inst1 << 8);
        alu.last = bytes[bytes_read++];
        alu.is_op3 = bytes[bytes_read++];
-       alu.predicate = bytes[bytes_read++];
+       push_modifier = bytes[bytes_read++];
+       alu.pred_sel = bytes[bytes_read++];
        alu.bank_swizzle = bytes[bytes_read++];
        alu.bank_swizzle_force = bytes[bytes_read++];
        alu.omod = bytes[bytes_read++];
        alu.index_mode = bytes[bytes_read++];
-       r600_bytecode_add_alu(ctx->bc, &alu);
+
+
+       if (alu.inst == CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE) ||
+           alu.inst == CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE) ||
+           alu.inst == CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_INT) ||
+           alu.inst == CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_INT)) {
+               alu.update_pred = 1;
+               alu.dst.write = 0;
+               alu.src[1].sel = V_SQ_ALU_SRC_0;
+               alu.src[1].chan = 0;
+               alu.last = 1;
+    }
+
+    if (push_modifier) {
+        alu.pred_sel = 0;
+               alu.execute_mask = 1;
+               r600_bytecode_add_alu_type(ctx->bc, &alu, CTX_INST(V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU_PUSH_BEFORE));
+       } else
+               r600_bytecode_add_alu(ctx->bc, &alu);
+
 
        /* XXX: Handle other KILL instructions */
        if (alu.inst == CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_KILLGT)) {
@@ -318,15 +351,6 @@ static unsigned r600_alu_from_byte_stream(struct r600_shader_ctx *ctx,
 static void llvm_if(struct r600_shader_ctx *ctx, struct r600_bytecode_alu * alu,
        unsigned pred_inst)
 {
-       alu->inst = pred_inst; 
-       alu->predicate = 1;
-       alu->dst.write = 0;
-       alu->src[1].sel = V_SQ_ALU_SRC_0;
-       alu->src[1].chan = 0;
-       alu->last = 1;
-       r600_bytecode_add_alu_type(ctx->bc, alu,
-               CTX_INST(V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU_PUSH_BEFORE));
-
        r600_bytecode_add_cfinst(ctx->bc, CTX_INST(V_SQ_CF_WORD1_SQ_CF_INST_JUMP));
        fc_pushlevel(ctx, FC_IF);
        callstack_check_depth(ctx, FC_PUSH_VPM, 0);
@@ -356,31 +380,35 @@ static unsigned r600_fc_from_byte_stream(struct r600_shader_ctx *ctx,
        bytes_read = r600_src_from_byte_stream(bytes, bytes_read, &alu, 0);
        inst = bytes[bytes_read++];
        switch (inst) {
-       case 0:
+       case 0: /* FC_IF */
                llvm_if(ctx, &alu,
                        CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE));
                break;
-       case 1:
+       case 1: /* FC_IF_INT */
+               llvm_if(ctx, &alu,
+                       CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_INT));
+               break;
+       case 2: /* FC_ELSE */
                tgsi_else(ctx);
                break;
-       case 2:
+       case 3: /* FC_ENDIF */
                tgsi_endif(ctx);
                break;
-       case 3:
+       case 4: /* FC_BGNLOOP */
                tgsi_bgnloop(ctx);
                break;
-       case 4:
+       case 5: /* FC_ENDLOOP */
                tgsi_endloop(ctx);
                break;
-       case 5:
+       case 6: /* FC_BREAK */
                r600_break_from_byte_stream(ctx, &alu,
-                       CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE));
+                       CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_INT));
                break;
-       case 6:
+       case 7: /* FC_BREAK_NZ_INT */
                r600_break_from_byte_stream(ctx, &alu,
                        CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE_INT));
                break;
-       case 7:
+       case 8: /* FC_CONTINUE */
                {
                        unsigned opcode = TGSI_OPCODE_CONT;
                        if (ctx->bc->chip_class == CAYMAN) {
@@ -396,10 +424,14 @@ static unsigned r600_fc_from_byte_stream(struct r600_shader_ctx *ctx,
                        tgsi_loop_brk_cont(ctx);
                }
                break;
-       case 8:
+       case 9: /* FC_BREAK_Z_INT */
                r600_break_from_byte_stream(ctx, &alu,
                        CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETE_INT));
                break;
+       case 10: /* FC_BREAK_NZ */
+               r600_break_from_byte_stream(ctx, &alu,
+                       CTX_INST(V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_PRED_SETNE));
+               break;
        }
 
        return bytes_read;
@@ -443,27 +475,36 @@ static int r600_vtx_from_byte_stream(struct r600_shader_ctx *ctx,
        unsigned char * bytes, unsigned bytes_read)
 {
        struct r600_bytecode_vtx vtx;
+
+       uint32_t word0 = i32_from_byte_stream(bytes, &bytes_read);
+        uint32_t word1 = i32_from_byte_stream(bytes, &bytes_read);
+       uint32_t word2 = i32_from_byte_stream(bytes, &bytes_read);
+
        memset(&vtx, 0, sizeof(vtx));
-       vtx.inst = bytes[bytes_read++];
-       vtx.fetch_type = bytes[bytes_read++];
-       vtx.buffer_id = bytes[bytes_read++];
-       vtx.src_gpr = bytes[bytes_read++];
-       vtx.src_sel_x = bytes[bytes_read++];
-       vtx.mega_fetch_count = bytes[bytes_read++];
-       vtx.dst_gpr = bytes[bytes_read++];
-       vtx.dst_sel_x = bytes[bytes_read++];
-       vtx.dst_sel_y = bytes[bytes_read++];
-       vtx.dst_sel_z = bytes[bytes_read++];
-       vtx.dst_sel_w = bytes[bytes_read++];
-       vtx.use_const_fields = bytes[bytes_read++];
-       vtx.data_format = bytes[bytes_read++];
-       vtx.num_format_all = bytes[bytes_read++];
-       vtx.format_comp_all = bytes[bytes_read++];
-       vtx.srf_mode_all = bytes[bytes_read++];
-       /* offset is 2 bytes wide */
-       vtx.offset = bytes[bytes_read++];
-       vtx.offset |= (bytes[bytes_read++] << 8);
-       vtx.endian = bytes[bytes_read++];
+
+       /* WORD0 */
+       vtx.inst = G_SQ_VTX_WORD0_VTX_INST(word0);
+       vtx.fetch_type = G_SQ_VTX_WORD0_FETCH_TYPE(word0);
+       vtx.buffer_id = G_SQ_VTX_WORD0_BUFFER_ID(word0);
+       vtx.src_gpr = G_SQ_VTX_WORD0_SRC_GPR(word0);
+       vtx.src_sel_x = G_SQ_VTX_WORD0_SRC_SEL_X(word0);
+       vtx.mega_fetch_count = G_SQ_VTX_WORD0_MEGA_FETCH_COUNT(word0);
+
+       /* WORD1 */
+       vtx.dst_gpr = G_SQ_VTX_WORD1_GPR_DST_GPR(word1);
+       vtx.dst_sel_x = G_SQ_VTX_WORD1_DST_SEL_X(word1);
+       vtx.dst_sel_y = G_SQ_VTX_WORD1_DST_SEL_Y(word1);
+       vtx.dst_sel_z = G_SQ_VTX_WORD1_DST_SEL_Z(word1);
+       vtx.dst_sel_w = G_SQ_VTX_WORD1_DST_SEL_W(word1);
+       vtx.use_const_fields = G_SQ_VTX_WORD1_USE_CONST_FIELDS(word1);
+       vtx.data_format = G_SQ_VTX_WORD1_DATA_FORMAT(word1);
+       vtx.num_format_all = G_SQ_VTX_WORD1_NUM_FORMAT_ALL(word1);
+       vtx.format_comp_all = G_SQ_VTX_WORD1_FORMAT_COMP_ALL(word1);
+       vtx.srf_mode_all = G_SQ_VTX_WORD1_SRF_MODE_ALL(word1);
+
+       /* WORD 2*/
+       vtx.offset = G_SQ_VTX_WORD2_OFFSET(word2);
+       vtx.endian = G_SQ_VTX_WORD2_ENDIAN_SWAP(word2);
 
        if (r600_bytecode_add_vtx(ctx->bc, &vtx)) {
                fprintf(stderr, "Error adding vtx\n");
@@ -1143,7 +1184,7 @@ static int r600_shader_from_tgsi(struct r600_context * rctx, struct r600_pipe_sh
 #endif
        ctx.bc = &shader->bc;
        ctx.shader = shader;
-       ctx.native_integers = (rctx->screen->glsl_feature_level >= 130);
+       ctx.native_integers = true;
 
        r600_bytecode_init(ctx.bc, rctx->chip_class, rctx->family);
        ctx.tokens = tokens;
@@ -1284,8 +1325,7 @@ static int r600_shader_from_tgsi(struct r600_context * rctx, struct r600_pipe_sh
                                        shader->fs_write_all = TRUE;
                                break;
                        case TGSI_PROPERTY_VS_PROHIBIT_UCPS:
-                               if (property->u[0].Data == 1)
-                                       shader->vs_prohibit_ucps = TRUE;
+                               /* we don't need this one */
                                break;
                        }
                        break;
@@ -1544,6 +1584,7 @@ static int r600_shader_from_tgsi(struct r600_context * rctx, struct r600_pipe_sh
                                        j--;
                                        continue;
                                }
+                               output[j].swizzle_w = rctx->alpha_to_one && rctx->multisample_enable && !rctx->cb0_is_integer ? 5 : 3;
                                output[j].array_base = next_pixel_base++;
                                output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
                                shader->nr_ps_color_exports++;
@@ -1556,7 +1597,7 @@ static int r600_shader_from_tgsi(struct r600_context * rctx, struct r600_pipe_sh
                                                output[j].swizzle_x = 0;
                                                output[j].swizzle_y = 1;
                                                output[j].swizzle_z = 2;
-                                               output[j].swizzle_w = 3;
+                                               output[j].swizzle_w = rctx->alpha_to_one && rctx->multisample_enable && !rctx->cb0_is_integer ? 5 : 3;
                                                output[j].burst_count = 1;
                                                output[j].barrier = 1;
                                                output[j].array_base = next_pixel_base++;
@@ -3400,7 +3441,7 @@ static int tgsi_f2i(struct r600_shader_ctx *ctx)
                alu.src[0].sel = ctx->temp_reg;
                alu.src[0].chan = i;
 
-               if (i == last_inst)
+               if (i == last_inst || alu.inst == EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_UINT)
                        alu.last = 1;
                r = r600_bytecode_add_alu(ctx->bc, &alu);
                if (r)
@@ -3716,9 +3757,10 @@ static int tgsi_tex(struct r600_shader_ctx *ctx)
        int opcode;
        /* Texture fetch instructions can only use gprs as source.
         * Also they cannot negate the source or take the absolute value */
-       const boolean src_requires_loading = tgsi_tex_src_requires_loading(ctx, 0);
+       const boolean src_requires_loading = inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ &&
+                                             tgsi_tex_src_requires_loading(ctx, 0);
        boolean src_loaded = FALSE;
-       unsigned sampler_src_reg = 1;
+       unsigned sampler_src_reg = inst->Instruction.Opcode == TGSI_OPCODE_TXQ_LZ ? 0 : 1;
        uint8_t offset_x = 0, offset_y = 0, offset_z = 0;
 
        src_gpr = tgsi_tex_get_src_gpr(ctx, 0);
@@ -3851,7 +3893,8 @@ static int tgsi_tex(struct r600_shader_ctx *ctx)
 
        if ((inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
             inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE) &&
-           inst->Instruction.Opcode != TGSI_OPCODE_TXQ) {
+           inst->Instruction.Opcode != TGSI_OPCODE_TXQ &&
+           inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ) {
 
                static const unsigned src0_swizzle[] = {2, 2, 0, 1};
                static const unsigned src1_swizzle[] = {1, 0, 2, 2};
@@ -4020,7 +4063,13 @@ static int tgsi_tex(struct r600_shader_ctx *ctx)
        tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;
        tex.dst_sel_z = (inst->Dst[0].Register.WriteMask & 4) ? 2 : 7;
        tex.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;
-       if (src_loaded) {
+
+       if (inst->Instruction.Opcode == TGSI_OPCODE_TXQ_LZ) {
+               tex.src_sel_x = 4;
+               tex.src_sel_y = 4;
+               tex.src_sel_z = 4;
+               tex.src_sel_w = 4;
+       } else if (src_loaded) {
                tex.src_sel_x = 0;
                tex.src_sel_y = 1;
                tex.src_sel_z = 2;
@@ -4817,7 +4866,8 @@ static int emit_logic_pred(struct r600_shader_ctx *ctx, int opcode)
 
        memset(&alu, 0, sizeof(struct r600_bytecode_alu));
        alu.inst = opcode;
-       alu.predicate = 1;
+       alu.execute_mask = 1;
+       alu.update_pred = 1;
 
        alu.dst.sel = ctx->temp_reg;
        alu.dst.write = 1;
@@ -4931,7 +4981,7 @@ static void fc_set_mid(struct r600_shader_ctx *ctx, int fc_sp)
 {
        struct r600_cf_stack_entry *sp = &ctx->bc->fc_stack[fc_sp];
 
-       sp->mid = (struct r600_bytecode_cf **)realloc((void *)sp->mid,
+       sp->mid = realloc((void *)sp->mid,
                                                sizeof(struct r600_bytecode_cf *) * (sp->num_mid + 1));
        sp->mid[sp->num_mid] = ctx->bc->cf_last;
        sp->num_mid++;
@@ -4947,10 +4997,8 @@ static void fc_pushlevel(struct r600_shader_ctx *ctx, int type)
 static void fc_poplevel(struct r600_shader_ctx *ctx)
 {
        struct r600_cf_stack_entry *sp = &ctx->bc->fc_stack[ctx->bc->fc_sp];
-       if (sp->mid) {
-               free(sp->mid);
-               sp->mid = NULL;
-       }
+       free(sp->mid);
+       sp->mid = NULL;
        sp->num_mid = 0;
        sp->start = NULL;
        sp->type = 0;
@@ -5279,8 +5327,8 @@ static struct r600_shader_tgsi_instruction r600_shader_tgsi_instruction[] = {
        {TGSI_OPCODE_BGNSUB,    0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {TGSI_OPCODE_ENDLOOP,   0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_endloop},
        {TGSI_OPCODE_ENDSUB,    0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
+       {TGSI_OPCODE_TXQ_LZ,    0, SQ_TEX_INST_GET_TEXTURE_RESINFO, tgsi_tex},
        /* gap */
-       {103,                   0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {104,                   0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {105,                   0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {106,                   0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
@@ -5306,7 +5354,7 @@ static struct r600_shader_tgsi_instruction r600_shader_tgsi_instruction[] = {
        {TGSI_OPCODE_ISGE,      0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETGE_INT, tgsi_op2},
        {TGSI_OPCODE_ISHR,      0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ASHR_INT, tgsi_op2_trans},
        {TGSI_OPCODE_ISLT,      0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_SETGT_INT, tgsi_op2_swap},
-       {TGSI_OPCODE_F2U,       0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_UINT, tgsi_op2},
+       {TGSI_OPCODE_F2U,       0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_FLT_TO_UINT, tgsi_op2_trans},
        {TGSI_OPCODE_U2F,       0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_UINT_TO_FLT, tgsi_op2_trans},
        {TGSI_OPCODE_UADD,      0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_ADD_INT, tgsi_op2},
        {TGSI_OPCODE_UDIV,      0, V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_udiv},
@@ -5453,8 +5501,8 @@ static struct r600_shader_tgsi_instruction eg_shader_tgsi_instruction[] = {
        {TGSI_OPCODE_BGNSUB,    0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {TGSI_OPCODE_ENDLOOP,   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_endloop},
        {TGSI_OPCODE_ENDSUB,    0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
+       {TGSI_OPCODE_TXQ_LZ,    0, SQ_TEX_INST_GET_TEXTURE_RESINFO, tgsi_tex},
        /* gap */
-       {103,                   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {104,                   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {105,                   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {106,                   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
@@ -5627,8 +5675,8 @@ static struct r600_shader_tgsi_instruction cm_shader_tgsi_instruction[] = {
        {TGSI_OPCODE_BGNSUB,    0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {TGSI_OPCODE_ENDLOOP,   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_endloop},
        {TGSI_OPCODE_ENDSUB,    0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
+       {TGSI_OPCODE_TXQ_LZ,    0, SQ_TEX_INST_GET_TEXTURE_RESINFO, tgsi_tex},
        /* gap */
-       {103,                   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {104,                   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {105,                   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},
        {106,                   0, EG_V_SQ_ALU_WORD1_OP2_SQ_OP2_INST_NOP, tgsi_unsupported},