From 43af02ac731dac7d80f7e47feb0c80e4da156769 Mon Sep 17 00:00:00 2001 From: Yuanhan Liu Date: Mon, 27 Feb 2012 15:46:32 +0800 Subject: [PATCH] i965: handle gl_PointCoord for Gen4 and Gen5 platforms This patch add the support of gl_PointCoord gl builtin variable for platform gen4 and gen5(ILK). Unlike gen6+, we don't have a hardware support of gl_PointCoord, means hardware will not calculate the interpolation coefficient for you. Instead, you should handle it yourself in sf shader stage. But badly, gl_PointCoord is a FS instead of VS builtin variable, thus it's not included in c.vue_map generated in VS stage. Thus the current code doesn't aware of this attribute. And to handle it correctly, we need add it to c.vue_map manually to let SF shader generate the needed interpolation coefficient for FS shader. SF stage has it's own copy of vue_map, thus I think it's safe to do it manually. Since handling gl_PointCoord for gen4 and gen5 platforms is somehow a little special, I added a lot of comments and hope I didn't overdo it ;) v2: add a /* _NEW_BUFFERS */ comment to note the state flag dependency and also add the _NEW_BUFFERS dirty mask (Eric). Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=45975 Piglit: glsl-fs-pointcoord and fbo-gl_pointcoord NOTE: This is a candidate for stable release branches. Signed-off-by: Yuanhan Liu Reviewed-by: Eric Anholt --- src/mesa/drivers/dri/i965/brw_context.h | 6 +++++ src/mesa/drivers/dri/i965/brw_fs.cpp | 9 ++++++++ src/mesa/drivers/dri/i965/brw_sf.c | 30 ++++++++++++++++++++----- src/mesa/drivers/dri/i965/brw_sf.h | 1 + src/mesa/drivers/dri/i965/brw_sf_emit.c | 4 ++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/mesa/drivers/dri/i965/brw_context.h b/src/mesa/drivers/dri/i965/brw_context.h index a16145b9baa..0c50b6bc513 100644 --- a/src/mesa/drivers/dri/i965/brw_context.h +++ b/src/mesa/drivers/dri/i965/brw_context.h @@ -296,6 +296,12 @@ typedef enum BRW_VERT_RESULT_NDC = VERT_RESULT_MAX, BRW_VERT_RESULT_HPOS_DUPLICATE, BRW_VERT_RESULT_PAD, + /* + * It's actually not a vert_result but just a _mark_ to let sf aware that + * he need do something special to handle gl_PointCoord builtin variable + * correctly. see compile_sf_prog() for more info. + */ + BRW_VERT_RESULT_PNTC, BRW_VERT_RESULT_MAX } brw_vert_result; diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp index bf59da3a4d6..5f3d79d1d09 100644 --- a/src/mesa/drivers/dri/i965/brw_fs.cpp +++ b/src/mesa/drivers/dri/i965/brw_fs.cpp @@ -710,6 +710,15 @@ fs_visitor::calculate_urb_setup() urb_setup[fp_index] = urb_next++; } } + + /* + * It's a FS only attribute, and we did interpolation for this attribute + * in SF thread. So, count it here, too. + * + * See compile_sf_prog() for more info. + */ + if (brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(FRAG_ATTRIB_PNTC)) + urb_setup[FRAG_ATTRIB_PNTC] = urb_next++; } /* Each attribute is 4 setup channels, each of which is half a reg. */ diff --git a/src/mesa/drivers/dri/i965/brw_sf.c b/src/mesa/drivers/dri/i965/brw_sf.c index 6e63583e546..37d1ee502d8 100644 --- a/src/mesa/drivers/dri/i965/brw_sf.c +++ b/src/mesa/drivers/dri/i965/brw_sf.c @@ -64,6 +64,16 @@ static void compile_sf_prog( struct brw_context *brw, c.key = *key; c.vue_map = brw->vs.prog_data->vue_map; + if (c.key.do_point_coord) { + /* + * gl_PointCoord is a FS instead of VS builtin variable, thus it's + * not included in c.vue_map generated in VS stage. Here we add + * it manually to let SF shader generate the needed interpolation + * coefficient for FS shader. + */ + c.vue_map.vert_result_to_slot[BRW_VERT_RESULT_PNTC] = c.vue_map.num_slots; + c.vue_map.slot_to_vert_result[c.vue_map.num_slots++] = BRW_VERT_RESULT_PNTC; + } c.urb_entry_read_offset = brw_sf_compute_urb_entry_read_offset(intel); c.nr_attr_regs = (c.vue_map.num_slots + 1)/2 - c.urb_entry_read_offset; c.nr_setup_regs = c.nr_attr_regs; @@ -125,6 +135,8 @@ brw_upload_sf_prog(struct brw_context *brw) { struct gl_context *ctx = &brw->intel.ctx; struct brw_sf_prog_key key; + /* _NEW_BUFFERS */ + bool render_to_fbo = ctx->DrawBuffer->Name != 0; memset(&key, 0, sizeof(key)); @@ -167,7 +179,15 @@ brw_upload_sf_prog(struct brw_context *brw) key.point_sprite_coord_replace |= (1 << i); } } - key.sprite_origin_lower_left = (ctx->Point.SpriteOrigin == GL_LOWER_LEFT); + if (brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(FRAG_ATTRIB_PNTC)) + key.do_point_coord = 1; + /* + * Window coordinates in a FBO are inverted, which means point + * sprite origin must be inverted, too. + */ + if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) + key.sprite_origin_lower_left = true; + /* _NEW_LIGHT */ key.do_flat_shading = (ctx->Light.ShadeModel == GL_FLAT); key.do_twoside_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide); @@ -176,10 +196,9 @@ brw_upload_sf_prog(struct brw_context *brw) if (key.do_twoside_color) { /* If we're rendering to a FBO, we have to invert the polygon * face orientation, just as we invert the viewport in - * sf_unit_create_from_key(). ctx->DrawBuffer->Name will be - * nonzero if we're rendering to such an FBO. + * sf_unit_create_from_key(). */ - key.frontface_ccw = (ctx->Polygon.FrontFace == GL_CCW) ^ (ctx->DrawBuffer->Name != 0); + key.frontface_ccw = (ctx->Polygon.FrontFace == GL_CCW) != render_to_fbo; } if (!brw_search_cache(&brw->cache, BRW_SF_PROG, @@ -192,7 +211,8 @@ brw_upload_sf_prog(struct brw_context *brw) const struct brw_tracked_state brw_sf_prog = { .dirty = { - .mesa = (_NEW_HINT | _NEW_LIGHT | _NEW_POLYGON | _NEW_POINT | _NEW_TRANSFORM), + .mesa = (_NEW_HINT | _NEW_LIGHT | _NEW_POLYGON | _NEW_POINT | + _NEW_TRANSFORM | _NEW_BUFFERS), .brw = (BRW_NEW_REDUCED_PRIMITIVE), .cache = CACHE_NEW_VS_PROG }, diff --git a/src/mesa/drivers/dri/i965/brw_sf.h b/src/mesa/drivers/dri/i965/brw_sf.h index 4ef024043ec..f908fc0667b 100644 --- a/src/mesa/drivers/dri/i965/brw_sf.h +++ b/src/mesa/drivers/dri/i965/brw_sf.h @@ -52,6 +52,7 @@ struct brw_sf_prog_key { GLuint do_flat_shading:1; GLuint frontface_ccw:1; GLuint do_point_sprite:1; + GLuint do_point_coord:1; GLuint sprite_origin_lower_left:1; GLuint userclip_active:1; }; diff --git a/src/mesa/drivers/dri/i965/brw_sf_emit.c b/src/mesa/drivers/dri/i965/brw_sf_emit.c index 1ee0098d982..ff6383bb188 100644 --- a/src/mesa/drivers/dri/i965/brw_sf_emit.c +++ b/src/mesa/drivers/dri/i965/brw_sf_emit.c @@ -386,6 +386,8 @@ calculate_point_sprite_mask(struct brw_sf_compile *c, GLuint reg) if (c->key.point_sprite_coord_replace & (1 << (vert_result1 - VERT_RESULT_TEX0))) pc |= 0x0f; } + if (vert_result1 == BRW_VERT_RESULT_PNTC) + pc |= 0x0f; vert_result2 = vert_reg_to_vert_result(c, reg, 1); if (vert_result2 >= VERT_RESULT_TEX0 && vert_result2 <= VERT_RESULT_TEX7) { @@ -393,6 +395,8 @@ calculate_point_sprite_mask(struct brw_sf_compile *c, GLuint reg) VERT_RESULT_TEX0))) pc |= 0xf0; } + if (vert_result2 == BRW_VERT_RESULT_PNTC) + pc |= 0xf0; return pc; } -- 2.30.2