diff options
Diffstat (limited to 'src/gallium/drivers/llvmpipe/lp_state_fs.c')
-rw-r--r-- | src/gallium/drivers/llvmpipe/lp_state_fs.c | 394 |
1 files changed, 284 insertions, 110 deletions
diff --git a/src/gallium/drivers/llvmpipe/lp_state_fs.c b/src/gallium/drivers/llvmpipe/lp_state_fs.c index 521700acd5..9b0e7cdd37 100644 --- a/src/gallium/drivers/llvmpipe/lp_state_fs.c +++ b/src/gallium/drivers/llvmpipe/lp_state_fs.c @@ -38,9 +38,11 @@ #include "tgsi/tgsi_parse.h" #include "lp_bld_type.h" #include "lp_bld_conv.h" +#include "lp_bld_logic.h" #include "lp_bld_depth.h" #include "lp_bld_tgsi.h" #include "lp_bld_alpha.h" +#include "lp_bld_blend.h" #include "lp_bld_swizzle.h" #include "lp_bld_flow.h" #include "lp_bld_debug.h" @@ -55,13 +57,13 @@ static const unsigned char quad_offset_y[4] = {0, 0, 1, 1}; static void -setup_pos_vector(LLVMBuilderRef builder, - LLVMValueRef x, - LLVMValueRef y, - LLVMValueRef a0_ptr, - LLVMValueRef dadx_ptr, - LLVMValueRef dady_ptr, - LLVMValueRef *pos) +generate_pos(LLVMBuilderRef builder, + LLVMValueRef x, + LLVMValueRef y, + LLVMValueRef a0_ptr, + LLVMValueRef dadx_ptr, + LLVMValueRef dady_ptr, + LLVMValueRef *pos) { LLVMTypeRef int_elem_type = LLVMInt32Type(); LLVMTypeRef int_vec_type = LLVMVectorType(int_elem_type, QUAD_SIZE); @@ -110,13 +112,13 @@ setup_pos_vector(LLVMBuilderRef builder, static void -depth_test_generate(struct llvmpipe_context *lp, - LLVMBuilderRef builder, - const struct pipe_depth_state *state, - union lp_type src_type, - struct lp_build_mask_context *mask, - LLVMValueRef src, - LLVMValueRef dst_ptr) +generate_depth(struct llvmpipe_context *lp, + LLVMBuilderRef builder, + const struct pipe_depth_state *state, + union lp_type src_type, + struct lp_build_mask_context *mask, + LLVMValueRef src, + LLVMValueRef dst_ptr) { const struct util_format_description *format_desc; union lp_type dst_type; @@ -151,18 +153,177 @@ depth_test_generate(struct llvmpipe_context *lp, } -static struct lp_fragment_shader_variant * -shader_generate(struct llvmpipe_context *lp, - struct lp_fragment_shader *shader, - const struct lp_fragment_shader_variant_key *key) +/** + * Generate the fragment shader, depth/stencil and alpha tests. + */ +static void +generate_fs(struct llvmpipe_context *lp, + struct lp_fragment_shader *shader, + const struct lp_fragment_shader_variant_key *key, + LLVMBuilderRef builder, + union lp_type type, + unsigned i, + LLVMValueRef x, + LLVMValueRef y, + LLVMValueRef a0_ptr, + LLVMValueRef dadx_ptr, + LLVMValueRef dady_ptr, + LLVMValueRef consts_ptr, + LLVMValueRef *pmask, + LLVMValueRef *color, + LLVMValueRef depth_ptr, + LLVMValueRef samplers_ptr) { - struct llvmpipe_screen *screen = llvmpipe_screen(lp->pipe.screen); - struct lp_fragment_shader_variant *variant; const struct tgsi_token *tokens = shader->base.tokens; - union lp_type type; LLVMTypeRef elem_type; LLVMTypeRef vec_type; LLVMTypeRef int_vec_type; + LLVMValueRef pos[NUM_CHANNELS]; + LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][NUM_CHANNELS]; + struct lp_build_mask_context mask; + boolean early_depth_test; + unsigned attrib; + unsigned chan; + + elem_type = lp_build_elem_type(type); + vec_type = lp_build_vec_type(type); + int_vec_type = lp_build_int_vec_type(type); + + generate_pos(builder, x, y, a0_ptr, dadx_ptr, dady_ptr, pos); + + lp_build_mask_begin(&mask, builder, type, *pmask); + + early_depth_test = + lp->depth_stencil->depth.enabled && + lp->framebuffer.zsbuf && + !lp->depth_stencil->alpha.enabled && + !lp->fs->info.uses_kill && + !lp->fs->info.writes_z; + + if(early_depth_test) + generate_depth(lp, builder, &key->depth, + type, &mask, + pos[2], depth_ptr); + + memset(outputs, 0, sizeof outputs); + + lp_build_tgsi_soa(builder, tokens, type, &mask, + pos, a0_ptr, dadx_ptr, dady_ptr, + consts_ptr, outputs, samplers_ptr); + + for (attrib = 0; attrib < shader->info.num_outputs; ++attrib) { + for(chan = 0; chan < NUM_CHANNELS; ++chan) { + if(outputs[attrib][chan]) { + lp_build_name(outputs[attrib][chan], "output%u.%u.%c", i, attrib, "xyzw"[chan]); + + switch (shader->info.output_semantic_name[attrib]) { + case TGSI_SEMANTIC_COLOR: + { + unsigned cbuf = shader->info.output_semantic_index[attrib]; + + lp_build_name(outputs[attrib][chan], "color%u.%u.%c", i, attrib, "rgba"[chan]); + + /* Alpha test */ + /* XXX: should the alpha reference value be passed separately? */ + if(cbuf == 0 && chan == 3) + lp_build_alpha_test(builder, &key->alpha, type, + &mask, + outputs[attrib][chan]); + + if(cbuf == 0) + color[chan] = outputs[attrib][chan]; + + break; + } + + case TGSI_SEMANTIC_POSITION: + if(chan == 2) + pos[2] = outputs[attrib][chan]; + break; + } + } + } + } + + if(!early_depth_test) + generate_depth(lp, builder, &key->depth, + type, &mask, + pos[2], depth_ptr); + + lp_build_mask_end(&mask); + + *pmask = mask.value; + +} + + +/** + * Generate blending code according to blend->base state. + * The blend function will look like: + * blend(mask, src_color, constant color, dst_color) + * dst_color will be modified and contain the result of the blend func. + */ +static void +generate_blend(const struct pipe_blend_state *blend, + LLVMBuilderRef builder, + union lp_type type, + LLVMValueRef mask, + LLVMValueRef *src, + LLVMValueRef const_ptr, + LLVMValueRef dst_ptr) +{ + struct lp_build_context bld; + LLVMTypeRef vec_type; + LLVMTypeRef int_vec_type; + LLVMValueRef con[4]; + LLVMValueRef dst[4]; + LLVMValueRef res[4]; + unsigned chan; + + vec_type = lp_build_vec_type(type); + int_vec_type = lp_build_int_vec_type(type); + + lp_build_context_init(&bld, builder, type); + + for(chan = 0; chan < 4; ++chan) { + LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), chan, 0); + + if(const_ptr) + con[chan] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, const_ptr, &index, 1, ""), ""); + else + con[chan] = LLVMGetUndef(vec_type); /* FIXME */ + + dst[chan] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, dst_ptr, &index, 1, ""), ""); + + lp_build_name(con[chan], "con.%c", "rgba"[chan]); + lp_build_name(dst[chan], "dst.%c", "rgba"[chan]); + } + + lp_build_blend_soa(builder, blend, type, src, dst, con, res); + + for(chan = 0; chan < 4; ++chan) { + LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), chan, 0); + lp_build_name(res[chan], "res.%c", "rgba"[chan]); + res[chan] = lp_build_select(&bld, mask, res[chan], dst[chan]); + LLVMBuildStore(builder, res[chan], LLVMBuildGEP(builder, dst_ptr, &index, 1, "")); + } +} + + +static struct lp_fragment_shader_variant * +generate_fragment(struct llvmpipe_context *lp, + struct lp_fragment_shader *shader, + const struct lp_fragment_shader_variant_key *key) +{ + struct llvmpipe_screen *screen = llvmpipe_screen(lp->pipe.screen); + struct lp_fragment_shader_variant *variant; + union lp_type fs_type; + union lp_type blend_type; + LLVMTypeRef fs_elem_type; + LLVMTypeRef fs_vec_type; + LLVMTypeRef fs_int_vec_type; + LLVMTypeRef blend_vec_type; + LLVMTypeRef blend_int_vec_type; LLVMTypeRef arg_types[10]; LLVMTypeRef func_type; LLVMValueRef x; @@ -177,24 +338,38 @@ shader_generate(struct llvmpipe_context *lp, LLVMValueRef samplers_ptr; LLVMBasicBlockRef block; LLVMBuilderRef builder; - LLVMValueRef pos[NUM_CHANNELS]; - LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][NUM_CHANNELS]; - struct lp_build_mask_context mask; - boolean early_depth_test; + LLVMValueRef fs_mask[LP_MAX_VECTOR_LENGTH]; + LLVMValueRef fs_out_color[NUM_CHANNELS][LP_MAX_VECTOR_LENGTH]; + LLVMValueRef blend_mask; + LLVMValueRef blend_in_color[NUM_CHANNELS]; LLVMValueRef fetch_texel; + unsigned num_fs; unsigned i; - unsigned attrib; unsigned chan; #ifdef DEBUG tgsi_dump(shader->base.tokens, 0); - debug_printf("depth.enabled = %u\n", key->depth.enabled); - debug_printf("depth.func = %s\n", debug_dump_func(key->depth.func, TRUE)); - debug_printf("depth.writemask = %u\n", key->depth.writemask); - debug_printf("depth.occlusion_count = %u\n", key->depth.occlusion_count); - debug_printf("alpha.enabled = %u\n", key->alpha.enabled); - debug_printf("alpha.func = %s\n", debug_dump_func(key->alpha.func, TRUE)); - debug_printf("alpha.ref_value = %f\n", key->alpha.ref_value); + if(key->depth.enabled) { + debug_printf("depth.func = %s\n", debug_dump_func(key->depth.func, TRUE)); + debug_printf("depth.writemask = %u\n", key->depth.writemask); + debug_printf("depth.occlusion_count = %u\n", key->depth.occlusion_count); + } + if(key->alpha.enabled) { + debug_printf("alpha.func = %s\n", debug_dump_func(key->alpha.func, TRUE)); + debug_printf("alpha.ref_value = %f\n", key->alpha.ref_value); + } + if(key->blend.logicop_enable) { + debug_printf("blend.logicop_func = %u\n", key->blend.logicop_func); + } + else if(key->blend.blend_enable) { + debug_printf("blend.rgb_func = %s\n", debug_dump_blend_func (key->blend.rgb_func, TRUE)); + debug_printf("rgb_src_factor = %s\n", debug_dump_blend_factor(key->blend.rgb_src_factor, TRUE)); + debug_printf("rgb_dst_factor = %s\n", debug_dump_blend_factor(key->blend.rgb_dst_factor, TRUE)); + debug_printf("alpha_func = %s\n", debug_dump_blend_func (key->blend.alpha_func, TRUE)); + debug_printf("alpha_src_factor = %s\n", debug_dump_blend_factor(key->blend.alpha_src_factor, TRUE)); + debug_printf("alpha_dst_factor = %s\n", debug_dump_blend_factor(key->blend.alpha_dst_factor, TRUE)); + } + debug_printf("blend.colormask = 0x%x\n", key->blend.colormask); #endif variant = CALLOC_STRUCT(lp_fragment_shader_variant); @@ -204,26 +379,37 @@ shader_generate(struct llvmpipe_context *lp, variant->shader = shader; memcpy(&variant->key, key, sizeof *key); - type.value = 0; - type.floating = TRUE; /* floating point values */ - type.sign = TRUE; /* values are signed */ - type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ - type.width = 32; /* 32-bit float */ - type.length = 4; /* 4 element per vector */ + fs_type.value = 0; + fs_type.floating = TRUE; /* floating point values */ + fs_type.sign = TRUE; /* values are signed */ + fs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ + fs_type.width = 32; /* 32-bit float */ + fs_type.length = 4; /* 4 element per vector */ + num_fs = 4; - elem_type = lp_build_elem_type(type); - vec_type = lp_build_vec_type(type); - int_vec_type = lp_build_int_vec_type(type); + blend_type.value = 0; + blend_type.floating = FALSE; /* values are integers */ + blend_type.sign = FALSE; /* values are unsigned */ + blend_type.norm = TRUE; /* values are in [0,1] or [-1,1] */ + blend_type.width = 8; /* 8-bit ubyte values */ + blend_type.length = 16; /* 16 elements per vector */ + + fs_elem_type = lp_build_elem_type(fs_type); + fs_vec_type = lp_build_vec_type(fs_type); + fs_int_vec_type = lp_build_int_vec_type(fs_type); + + blend_vec_type = lp_build_vec_type(blend_type); + blend_int_vec_type = lp_build_int_vec_type(blend_type); arg_types[0] = LLVMInt32Type(); /* x */ arg_types[1] = LLVMInt32Type(); /* y */ - arg_types[2] = LLVMPointerType(elem_type, 0); /* a0 */ - arg_types[3] = LLVMPointerType(elem_type, 0); /* dadx */ - arg_types[4] = LLVMPointerType(elem_type, 0); /* dady */ - arg_types[5] = LLVMPointerType(elem_type, 0); /* consts */ - arg_types[6] = LLVMPointerType(int_vec_type, 0); /* mask */ - arg_types[7] = LLVMPointerType(vec_type, 0); /* color */ - arg_types[8] = LLVMPointerType(int_vec_type, 0); /* depth */ + arg_types[2] = LLVMPointerType(fs_elem_type, 0); /* a0 */ + arg_types[3] = LLVMPointerType(fs_elem_type, 0); /* dadx */ + arg_types[4] = LLVMPointerType(fs_elem_type, 0); /* dady */ + arg_types[5] = LLVMPointerType(fs_elem_type, 0); /* consts */ + arg_types[6] = LLVMPointerType(fs_int_vec_type, 0); /* mask */ + arg_types[7] = LLVMPointerType(blend_vec_type, 0); /* color */ + arg_types[8] = LLVMPointerType(fs_int_vec_type, 0); /* depth */ arg_types[9] = LLVMPointerType(LLVMInt8Type(), 0); /* samplers */ func_type = LLVMFunctionType(LLVMVoidType(), arg_types, Elements(arg_types), 0); @@ -260,70 +446,57 @@ shader_generate(struct llvmpipe_context *lp, builder = LLVMCreateBuilder(); LLVMPositionBuilderAtEnd(builder, block); - setup_pos_vector(builder, x, y, a0_ptr, dadx_ptr, dady_ptr, pos); - - lp_build_mask_begin(&mask, builder, type, - LLVMBuildLoad(builder, mask_ptr, "")); - - early_depth_test = - lp->depth_stencil->depth.enabled && - lp->framebuffer.zsbuf && - !lp->depth_stencil->alpha.enabled && - !lp->fs->info.uses_kill && - !lp->fs->info.writes_z; - - if(early_depth_test) - depth_test_generate(lp, builder, &key->depth, - type, &mask, - pos[2], depth_ptr); - - memset(outputs, 0, sizeof outputs); - - lp_build_tgsi_soa(builder, tokens, type, &mask, - pos, a0_ptr, dadx_ptr, dady_ptr, - consts_ptr, outputs, samplers_ptr); - - for (attrib = 0; attrib < shader->info.num_outputs; ++attrib) { - for(chan = 0; chan < NUM_CHANNELS; ++chan) { - if(outputs[attrib][chan]) { - lp_build_name(outputs[attrib][chan], "output%u.%c", attrib, "xyzw"[chan]); - - switch (shader->info.output_semantic_name[attrib]) { - case TGSI_SEMANTIC_COLOR: - { - unsigned cbuf = shader->info.output_semantic_index[attrib]; - LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), cbuf*NUM_CHANNELS + chan, 0); - LLVMValueRef output_ptr = LLVMBuildGEP(builder, color_ptr, &index, 1, ""); - lp_build_name(outputs[attrib][chan], "color%u.%c", attrib, "rgba"[chan]); - LLVMBuildStore(builder, outputs[attrib][chan], output_ptr); - - /* Alpha test */ - /* XXX: should the alpha reference value be passed separately? */ - if(cbuf == 0 && chan == 3) - lp_build_alpha_test(builder, &key->alpha, type, - &mask, - outputs[attrib][chan]); - - break; - } + for(i = 0; i < num_fs; ++i) { + LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0); + LLVMValueRef out_color[NUM_CHANNELS]; + LLVMValueRef x_i; + LLVMValueRef depth_ptr_i; + + /* TODO: Reuse position interpolation */ + x_i = LLVMBuildAdd(builder, x, LLVMConstInt(LLVMInt32Type(), 2*i, 0), ""); + + fs_mask[i] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, mask_ptr, &index, 1, ""), ""); + depth_ptr_i = LLVMBuildGEP(builder, depth_ptr, &index, 1, ""); + + generate_fs(lp, + shader, + key, + builder, + fs_type, + i, + x_i, + y, + a0_ptr, + dadx_ptr, + dady_ptr, + consts_ptr, + &fs_mask[i], + out_color, + depth_ptr_i, + samplers_ptr); + + for(chan = 0; chan < NUM_CHANNELS; ++chan) + fs_out_color[chan][i] = out_color[chan]; + } - case TGSI_SEMANTIC_POSITION: - if(chan == 2) - pos[2] = outputs[attrib][chan]; - break; - } - } - } + for(chan = 0; chan < NUM_CHANNELS; ++chan) { + lp_build_conv(builder, fs_type, blend_type, + fs_out_color[chan], num_fs, + &blend_in_color[chan], 1); + lp_build_name(blend_in_color[chan], "color.%c", "rgba"[chan]); } - if(!early_depth_test) - depth_test_generate(lp, builder, &key->depth, - type, &mask, - pos[2], depth_ptr); + lp_build_conv_mask(builder, fs_type, blend_type, + fs_mask, num_fs, + &blend_mask, 1); - lp_build_mask_end(&mask); - if(mask.value) - LLVMBuildStore(builder, mask.value, mask_ptr); + generate_blend(&key->blend, + builder, + blend_type, + blend_mask, + blend_in_color, + NULL /* FIXME: blend_const_color */, + color_ptr); LLVMBuildRetVoid(builder);; @@ -512,6 +685,7 @@ void llvmpipe_update_fs(struct llvmpipe_context *lp) memset(&key, 0, sizeof key); memcpy(&key.depth, &lp->depth_stencil->depth, sizeof &key.depth); memcpy(&key.alpha, &lp->depth_stencil->alpha, sizeof &key.alpha); + memcpy(&key.blend, &lp->blend->base, sizeof &key.blend); variant = shader->variants; while(variant) { @@ -522,7 +696,7 @@ void llvmpipe_update_fs(struct llvmpipe_context *lp) } if(!variant) - variant = shader_generate(lp, shader, &key); + variant = generate_fragment(lp, shader, &key); shader->current = variant; } |