r600g: add point/sprite rendering support
[mesa.git] / src / gallium / drivers / r600 / r600_shader.c
index f38aa7b46369a1bf788b9a2ce0e422236ee9f979..ca65bff24c1bdedf6aeb939544424f630e32e3db 100644 (file)
@@ -130,12 +130,11 @@ static int r600_pipe_shader_vs(struct pipe_context *ctx, struct r600_context_sta
        struct r600_shader *rshader = &rpshader->shader;
        struct radeon_state *state;
        unsigned i, tmp;
-       int r;
 
-       r = radeon_state_init(&rpshader->rstate, rscreen->rw, R600_VS_SHADER_TYPE, R600_VS_SHADER);
-       if (r)
-               return r;
-       state = &rpshader->rstate;
+       rpshader->rstate = radeon_state_decref(rpshader->rstate);
+       state = radeon_state(rscreen->rw, R600_VS_SHADER_TYPE, R600_VS_SHADER);
+       if (state == NULL)
+               return -ENOMEM;
        for (i = 0; i < 10; i++) {
                state->states[R600_VS_SHADER__SPI_VS_OUT_ID_0 + i] = 0;
        }
@@ -146,25 +145,28 @@ static int r600_pipe_shader_vs(struct pipe_context *ctx, struct r600_context_sta
        }
        state->states[R600_VS_SHADER__SPI_VS_OUT_CONFIG] = S_0286C4_VS_EXPORT_COUNT(rshader->noutput - 2);
        state->states[R600_VS_SHADER__SQ_PGM_RESOURCES_VS] = S_028868_NUM_GPRS(rshader->bc.ngpr);
-       rpshader->rstate.bo[0] = radeon_bo_incref(rscreen->rw, rpshader->bo);
-       rpshader->rstate.bo[1] = radeon_bo_incref(rscreen->rw, rpshader->bo);
-       rpshader->rstate.nbo = 2;
-       rpshader->rstate.placement[0] = RADEON_GEM_DOMAIN_GTT;
+       rpshader->rstate = state;
+       rpshader->rstate->bo[0] = radeon_bo_incref(rscreen->rw, rpshader->bo);
+       rpshader->rstate->bo[1] = radeon_bo_incref(rscreen->rw, rpshader->bo);
+       rpshader->rstate->nbo = 2;
+       rpshader->rstate->placement[0] = RADEON_GEM_DOMAIN_GTT;
        return radeon_state_pm4(state);
 }
 
 static int r600_pipe_shader_ps(struct pipe_context *ctx, struct r600_context_state *rpshader)
 {
+       const struct pipe_rasterizer_state *rasterizer;
        struct r600_screen *rscreen = r600_screen(ctx->screen);
        struct r600_shader *rshader = &rpshader->shader;
+       struct r600_context *rctx = r600_context(ctx);
        struct radeon_state *state;
-       unsigned i, tmp;
-       int r;
+       unsigned i, tmp, exports_ps, num_cout;
 
-       r = radeon_state_init(&rpshader->rstate, rscreen->rw, R600_PS_SHADER_TYPE, R600_PS_SHADER);
-       if (r)
-               return r;
-       state = &rpshader->rstate;
+       rasterizer = &rctx->rasterizer->state.rasterizer;
+       rpshader->rstate = radeon_state_decref(rpshader->rstate);
+       state = radeon_state(rscreen->rw, R600_PS_SHADER_TYPE, R600_PS_SHADER);
+       if (state == NULL)
+               return -ENOMEM;
        for (i = 0; i < rshader->ninput; i++) {
                tmp = S_028644_SEMANTIC(i);
                tmp |= S_028644_SEL_CENTROID(1);
@@ -172,16 +174,31 @@ static int r600_pipe_shader_ps(struct pipe_context *ctx, struct r600_context_sta
                        rshader->input[i].name == TGSI_SEMANTIC_BCOLOR) {
                        tmp |= S_028644_FLAT_SHADE(rshader->flat_shade);
                }
+               if (rasterizer->sprite_coord_enable & (1 << i)) {
+                       tmp |= S_028644_PT_SPRITE_TEX(1);
+               }
                state->states[R600_PS_SHADER__SPI_PS_INPUT_CNTL_0 + i] = tmp;
        }
+
+       exports_ps = 0;
+       num_cout = 0;
+       for (i = 0; i < rshader->noutput; i++) {
+               if (rshader->output[i].name == TGSI_SEMANTIC_POSITION)
+                       exports_ps |= 1;
+               else if (rshader->output[i].name == TGSI_SEMANTIC_COLOR) {
+                       exports_ps |= (1 << (num_cout+1));
+                       num_cout++;
+               }
+       }
        state->states[R600_PS_SHADER__SPI_PS_IN_CONTROL_0] = S_0286CC_NUM_INTERP(rshader->ninput) |
                                                        S_0286CC_PERSP_GRADIENT_ENA(1);
        state->states[R600_PS_SHADER__SPI_PS_IN_CONTROL_1] = 0x00000000;
        state->states[R600_PS_SHADER__SQ_PGM_RESOURCES_PS] = S_028868_NUM_GPRS(rshader->bc.ngpr);
-       state->states[R600_PS_SHADER__SQ_PGM_EXPORTS_PS] = 0x00000002;
-       rpshader->rstate.bo[0] = radeon_bo_incref(rscreen->rw, rpshader->bo);
-       rpshader->rstate.nbo = 1;
-       rpshader->rstate.placement[0] = RADEON_GEM_DOMAIN_GTT;
+       state->states[R600_PS_SHADER__SQ_PGM_EXPORTS_PS] = exports_ps;
+       rpshader->rstate = state;
+       rpshader->rstate->bo[0] = radeon_bo_incref(rscreen->rw, rpshader->bo);
+       rpshader->rstate->nbo = 1;
+       rpshader->rstate->placement[0] = RADEON_GEM_DOMAIN_GTT;
        return radeon_state_pm4(state);
 }
 
@@ -328,7 +345,8 @@ int r600_shader_from_tgsi(const struct tgsi_token *tokens, struct r600_shader *s
 {
        struct tgsi_full_immediate *immediate;
        struct r600_shader_ctx ctx;
-       struct r600_bc_output output;
+       struct r600_bc_output output[32];
+       unsigned output_done, noutput;
        unsigned opcode;
        int i, r = 0, pos0;
 
@@ -406,31 +424,41 @@ int r600_shader_from_tgsi(const struct tgsi_token *tokens, struct r600_shader *s
                }
        }
        /* export output */
-       for (i = 0, pos0 = 0; i < shader->noutput; i++) {
-               memset(&output, 0, sizeof(struct r600_bc_output));
-               output.gpr = shader->output[i].gpr;
-               output.elem_size = 3;
-               output.swizzle_x = 0;
-               output.swizzle_y = 1;
-               output.swizzle_z = 2;
-               output.swizzle_w = 3;
-               output.barrier = 1;
-               output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
-               output.array_base = i - pos0;
-               output.inst = V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE;
-               switch (ctx.type == TGSI_PROCESSOR_VERTEX) {
+       noutput = shader->noutput;
+       for (i = 0, pos0 = 0; i < noutput; i++) {
+               memset(&output[i], 0, sizeof(struct r600_bc_output));
+               output[i].gpr = shader->output[i].gpr;
+               output[i].elem_size = 3;
+               output[i].swizzle_x = 0;
+               output[i].swizzle_y = 1;
+               output[i].swizzle_z = 2;
+               output[i].swizzle_w = 3;
+               output[i].barrier = 1;
+               output[i].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
+               output[i].array_base = i - pos0;
+               output[i].inst = V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT;
+               switch (ctx.type) {
                case TGSI_PROCESSOR_VERTEX:
                        if (shader->output[i].name == TGSI_SEMANTIC_POSITION) {
-                               output.array_base = 60;
-                               output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
+                               output[i].array_base = 60;
+                               output[i].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
                                /* position doesn't count in array_base */
-                               pos0 = 1;
+                               pos0++;
+                       }
+                       if (shader->output[i].name == TGSI_SEMANTIC_PSIZE) {
+                               output[i].array_base = 61;
+                               output[i].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
+                               /* position doesn't count in array_base */
+                               pos0++;
                        }
                        break;
                case TGSI_PROCESSOR_FRAGMENT:
                        if (shader->output[i].name == TGSI_SEMANTIC_COLOR) {
-                               output.array_base = 0;
-                               output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
+                               output[i].array_base = shader->output[i].sid;
+                               output[i].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
+                       } else if (shader->output[i].name == TGSI_SEMANTIC_POSITION) {
+                               output[i].array_base = 61;
+                               output[i].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
                        } else {
                                R600_ERR("unsupported fragment output name %d\n", shader->output[i].name);
                                r = -EINVAL;
@@ -442,10 +470,43 @@ int r600_shader_from_tgsi(const struct tgsi_token *tokens, struct r600_shader *s
                        r = -EINVAL;
                        goto out_err;
                }
-               if (i == (shader->noutput - 1)) {
-                       output.end_of_program = 1;
+       }
+       /* add fake param output for vertex shader if no param is exported */
+       if (ctx.type == TGSI_PROCESSOR_VERTEX) {
+               for (i = 0, pos0 = 0; i < noutput; i++) {
+                       if (output[i].type == V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM) {
+                               pos0 = 1;
+                               break;
+                       }
+               }
+               if (!pos0) {
+                       memset(&output[i], 0, sizeof(struct r600_bc_output));
+                       output[i].gpr = 0;
+                       output[i].elem_size = 3;
+                       output[i].swizzle_x = 0;
+                       output[i].swizzle_y = 1;
+                       output[i].swizzle_z = 2;
+                       output[i].swizzle_w = 3;
+                       output[i].barrier = 1;
+                       output[i].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
+                       output[i].array_base = 0;
+                       output[i].inst = V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT;
+                       noutput++;
+               }
+       }
+       /* set export done on last export of each type */
+       for (i = noutput - 1, output_done = 0; i >= 0; i--) {
+               if (i == (noutput - 1)) {
+                       output[i].end_of_program = 1;
+               }
+               if (!(output_done & (1 << output[i].type))) {
+                       output_done |= (1 << output[i].type);
+                       output[i].inst = V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE;
                }
-               r = r600_bc_add_output(ctx.bc, &output);
+       }
+       /* add output to bytecode */
+       for (i = 0; i < noutput; i++) {
+               r = r600_bc_add_output(ctx.bc, &output[i]);
                if (r)
                        goto out_err;
        }