diff options
Diffstat (limited to 'mesalib/src/mesa/state_tracker')
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_atom_blend.c | 604 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_atom_framebuffer.c | 364 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_atom_pixeltransfer.c | 708 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_atom_sampler.c | 456 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_cb_drawpixels.c | 2976 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_cb_drawtex.c | 614 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_cb_readpixels.c | 1122 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_context.h | 540 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_draw.c | 1546 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_draw_feedback.c | 554 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_format.h | 172 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_mesa_to_tgsi.c | 2498 | ||||
-rw-r--r-- | mesalib/src/mesa/state_tracker/st_program.c | 2330 |
13 files changed, 7242 insertions, 7242 deletions
diff --git a/mesalib/src/mesa/state_tracker/st_atom_blend.c b/mesalib/src/mesa/state_tracker/st_atom_blend.c index fb1c7a4ef..e3b3b9936 100644 --- a/mesalib/src/mesa/state_tracker/st_atom_blend.c +++ b/mesalib/src/mesa/state_tracker/st_atom_blend.c @@ -1,302 +1,302 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - - /* - * Authors: - * Keith Whitwell <keith@tungstengraphics.com> - * Brian Paul - */ - - -#include "st_context.h" -#include "st_atom.h" - -#include "pipe/p_context.h" -#include "pipe/p_defines.h" -#include "cso_cache/cso_context.h" - -#include "main/macros.h" - -/** - * Convert GLenum blend tokens to pipe tokens. - * Both blend factors and blend funcs are accepted. - */ -static GLuint -translate_blend(GLenum blend) -{ - switch (blend) { - /* blend functions */ - case GL_FUNC_ADD: - return PIPE_BLEND_ADD; - case GL_FUNC_SUBTRACT: - return PIPE_BLEND_SUBTRACT; - case GL_FUNC_REVERSE_SUBTRACT: - return PIPE_BLEND_REVERSE_SUBTRACT; - case GL_MIN: - return PIPE_BLEND_MIN; - case GL_MAX: - return PIPE_BLEND_MAX; - - /* blend factors */ - case GL_ONE: - return PIPE_BLENDFACTOR_ONE; - case GL_SRC_COLOR: - return PIPE_BLENDFACTOR_SRC_COLOR; - case GL_SRC_ALPHA: - return PIPE_BLENDFACTOR_SRC_ALPHA; - case GL_DST_ALPHA: - return PIPE_BLENDFACTOR_DST_ALPHA; - case GL_DST_COLOR: - return PIPE_BLENDFACTOR_DST_COLOR; - case GL_SRC_ALPHA_SATURATE: - return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE; - case GL_CONSTANT_COLOR: - return PIPE_BLENDFACTOR_CONST_COLOR; - case GL_CONSTANT_ALPHA: - return PIPE_BLENDFACTOR_CONST_ALPHA; - /* - return PIPE_BLENDFACTOR_SRC1_COLOR; - return PIPE_BLENDFACTOR_SRC1_ALPHA; - */ - case GL_ZERO: - return PIPE_BLENDFACTOR_ZERO; - case GL_ONE_MINUS_SRC_COLOR: - return PIPE_BLENDFACTOR_INV_SRC_COLOR; - case GL_ONE_MINUS_SRC_ALPHA: - return PIPE_BLENDFACTOR_INV_SRC_ALPHA; - case GL_ONE_MINUS_DST_COLOR: - return PIPE_BLENDFACTOR_INV_DST_COLOR; - case GL_ONE_MINUS_DST_ALPHA: - return PIPE_BLENDFACTOR_INV_DST_ALPHA; - case GL_ONE_MINUS_CONSTANT_COLOR: - return PIPE_BLENDFACTOR_INV_CONST_COLOR; - case GL_ONE_MINUS_CONSTANT_ALPHA: - return PIPE_BLENDFACTOR_INV_CONST_ALPHA; - /* - return PIPE_BLENDFACTOR_INV_SRC1_COLOR; - return PIPE_BLENDFACTOR_INV_SRC1_ALPHA; - */ - default: - assert("invalid GL token in translate_blend()" == NULL); - return 0; - } -} - - -/** - * Convert GLenum logicop tokens to pipe tokens. - */ -static GLuint -translate_logicop(GLenum logicop) -{ - switch (logicop) { - case GL_CLEAR: - return PIPE_LOGICOP_CLEAR; - case GL_NOR: - return PIPE_LOGICOP_NOR; - case GL_AND_INVERTED: - return PIPE_LOGICOP_AND_INVERTED; - case GL_COPY_INVERTED: - return PIPE_LOGICOP_COPY_INVERTED; - case GL_AND_REVERSE: - return PIPE_LOGICOP_AND_REVERSE; - case GL_INVERT: - return PIPE_LOGICOP_INVERT; - case GL_XOR: - return PIPE_LOGICOP_XOR; - case GL_NAND: - return PIPE_LOGICOP_NAND; - case GL_AND: - return PIPE_LOGICOP_AND; - case GL_EQUIV: - return PIPE_LOGICOP_EQUIV; - case GL_NOOP: - return PIPE_LOGICOP_NOOP; - case GL_OR_INVERTED: - return PIPE_LOGICOP_OR_INVERTED; - case GL_COPY: - return PIPE_LOGICOP_COPY; - case GL_OR_REVERSE: - return PIPE_LOGICOP_OR_REVERSE; - case GL_OR: - return PIPE_LOGICOP_OR; - case GL_SET: - return PIPE_LOGICOP_SET; - default: - assert("invalid GL token in translate_logicop()" == NULL); - return 0; - } -} - -/** - * Figure out if colormasks are different per rt. - */ -static GLboolean -colormask_per_rt(struct gl_context *ctx) -{ - /* a bit suboptimal have to compare lots of values */ - unsigned i; - for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) { - if (memcmp(ctx->Color.ColorMask[0], ctx->Color.ColorMask[i], 4)) { - return GL_TRUE; - } - } - return GL_FALSE; -} - -/** - * Figure out if blend enables/state are different per rt. - */ -static GLboolean -blend_per_rt(struct gl_context *ctx) -{ - if (ctx->Color.BlendEnabled && - (ctx->Color.BlendEnabled != ((1 << ctx->Const.MaxDrawBuffers) - 1))) { - /* This can only happen if GL_EXT_draw_buffers2 is enabled */ - return GL_TRUE; - } - if (ctx->Color._BlendFuncPerBuffer || ctx->Color._BlendEquationPerBuffer) { - /* this can only happen if GL_ARB_draw_buffers_blend is enabled */ - return GL_TRUE; - } - return GL_FALSE; -} - -static void -update_blend( struct st_context *st ) -{ - struct pipe_blend_state *blend = &st->state.blend; - unsigned num_state = 1; - unsigned i, j; - - memset(blend, 0, sizeof(*blend)); - - if (blend_per_rt(st->ctx) || colormask_per_rt(st->ctx)) { - num_state = st->ctx->Const.MaxDrawBuffers; - blend->independent_blend_enable = 1; - } - /* Note it is impossible to correctly deal with EXT_blend_logic_op and - EXT_draw_buffers2/EXT_blend_equation_separate at the same time. - These combinations would require support for per-rt logicop enables - and separate alpha/rgb logicop/blend support respectively. Neither - possible in gallium nor most hardware. Assume these combinations - don't happen. */ - if (st->ctx->Color.ColorLogicOpEnabled || - (st->ctx->Color.BlendEnabled && - st->ctx->Color.Blend[0].EquationRGB == GL_LOGIC_OP)) { - /* logicop enabled */ - blend->logicop_enable = 1; - blend->logicop_func = translate_logicop(st->ctx->Color.LogicOp); - } - else if (st->ctx->Color.BlendEnabled) { - /* blending enabled */ - for (i = 0, j = 0; i < num_state; i++) { - - blend->rt[i].blend_enable = (st->ctx->Color.BlendEnabled >> i) & 0x1; - - if (st->ctx->Extensions.ARB_draw_buffers_blend) - j = i; - - blend->rt[i].rgb_func = - translate_blend(st->ctx->Color.Blend[j].EquationRGB); - - if (st->ctx->Color.Blend[i].EquationRGB == GL_MIN || - st->ctx->Color.Blend[i].EquationRGB == GL_MAX) { - /* Min/max are special */ - blend->rt[i].rgb_src_factor = PIPE_BLENDFACTOR_ONE; - blend->rt[i].rgb_dst_factor = PIPE_BLENDFACTOR_ONE; - } - else { - blend->rt[i].rgb_src_factor = - translate_blend(st->ctx->Color.Blend[j].SrcRGB); - blend->rt[i].rgb_dst_factor = - translate_blend(st->ctx->Color.Blend[j].DstRGB); - } - - blend->rt[i].alpha_func = - translate_blend(st->ctx->Color.Blend[j].EquationA); - - if (st->ctx->Color.Blend[i].EquationA == GL_MIN || - st->ctx->Color.Blend[i].EquationA == GL_MAX) { - /* Min/max are special */ - blend->rt[i].alpha_src_factor = PIPE_BLENDFACTOR_ONE; - blend->rt[i].alpha_dst_factor = PIPE_BLENDFACTOR_ONE; - } - else { - blend->rt[i].alpha_src_factor = - translate_blend(st->ctx->Color.Blend[j].SrcA); - blend->rt[i].alpha_dst_factor = - translate_blend(st->ctx->Color.Blend[j].DstA); - } - } - } - else { - /* no blending / logicop */ - } - - /* Colormask - maybe reverse these bits? */ - for (i = 0; i < num_state; i++) { - if (st->ctx->Color.ColorMask[i][0]) - blend->rt[i].colormask |= PIPE_MASK_R; - if (st->ctx->Color.ColorMask[i][1]) - blend->rt[i].colormask |= PIPE_MASK_G; - if (st->ctx->Color.ColorMask[i][2]) - blend->rt[i].colormask |= PIPE_MASK_B; - if (st->ctx->Color.ColorMask[i][3]) - blend->rt[i].colormask |= PIPE_MASK_A; - } - - if (st->ctx->Color.DitherFlag) - blend->dither = 1; - - if (st->ctx->Multisample.Enabled) { - /* unlike in gallium/d3d10 these operations are only performed - if msaa is enabled */ - if (st->ctx->Multisample.SampleAlphaToCoverage) - blend->alpha_to_coverage = 1; - if (st->ctx->Multisample.SampleAlphaToOne) - blend->alpha_to_one = 1; - } - - cso_set_blend(st->cso_context, blend); - - { - struct pipe_blend_color bc; - COPY_4FV(bc.color, st->ctx->Color.BlendColor); - cso_set_blend_color(st->cso_context, &bc); - } -} - - -const struct st_tracked_state st_update_blend = { - "st_update_blend", /* name */ - { /* dirty */ - (_NEW_COLOR | _NEW_MULTISAMPLE), /* XXX _NEW_BLEND someday? */ /* mesa */ - 0, /* st */ - }, - update_blend, /* update */ -}; +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+ /*
+ * Authors:
+ * Keith Whitwell <keith@tungstengraphics.com>
+ * Brian Paul
+ */
+
+
+#include "st_context.h"
+#include "st_atom.h"
+
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "cso_cache/cso_context.h"
+
+#include "main/macros.h"
+
+/**
+ * Convert GLenum blend tokens to pipe tokens.
+ * Both blend factors and blend funcs are accepted.
+ */
+static GLuint
+translate_blend(GLenum blend)
+{
+ switch (blend) {
+ /* blend functions */
+ case GL_FUNC_ADD:
+ return PIPE_BLEND_ADD;
+ case GL_FUNC_SUBTRACT:
+ return PIPE_BLEND_SUBTRACT;
+ case GL_FUNC_REVERSE_SUBTRACT:
+ return PIPE_BLEND_REVERSE_SUBTRACT;
+ case GL_MIN:
+ return PIPE_BLEND_MIN;
+ case GL_MAX:
+ return PIPE_BLEND_MAX;
+
+ /* blend factors */
+ case GL_ONE:
+ return PIPE_BLENDFACTOR_ONE;
+ case GL_SRC_COLOR:
+ return PIPE_BLENDFACTOR_SRC_COLOR;
+ case GL_SRC_ALPHA:
+ return PIPE_BLENDFACTOR_SRC_ALPHA;
+ case GL_DST_ALPHA:
+ return PIPE_BLENDFACTOR_DST_ALPHA;
+ case GL_DST_COLOR:
+ return PIPE_BLENDFACTOR_DST_COLOR;
+ case GL_SRC_ALPHA_SATURATE:
+ return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE;
+ case GL_CONSTANT_COLOR:
+ return PIPE_BLENDFACTOR_CONST_COLOR;
+ case GL_CONSTANT_ALPHA:
+ return PIPE_BLENDFACTOR_CONST_ALPHA;
+ /*
+ return PIPE_BLENDFACTOR_SRC1_COLOR;
+ return PIPE_BLENDFACTOR_SRC1_ALPHA;
+ */
+ case GL_ZERO:
+ return PIPE_BLENDFACTOR_ZERO;
+ case GL_ONE_MINUS_SRC_COLOR:
+ return PIPE_BLENDFACTOR_INV_SRC_COLOR;
+ case GL_ONE_MINUS_SRC_ALPHA:
+ return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
+ case GL_ONE_MINUS_DST_COLOR:
+ return PIPE_BLENDFACTOR_INV_DST_COLOR;
+ case GL_ONE_MINUS_DST_ALPHA:
+ return PIPE_BLENDFACTOR_INV_DST_ALPHA;
+ case GL_ONE_MINUS_CONSTANT_COLOR:
+ return PIPE_BLENDFACTOR_INV_CONST_COLOR;
+ case GL_ONE_MINUS_CONSTANT_ALPHA:
+ return PIPE_BLENDFACTOR_INV_CONST_ALPHA;
+ /*
+ return PIPE_BLENDFACTOR_INV_SRC1_COLOR;
+ return PIPE_BLENDFACTOR_INV_SRC1_ALPHA;
+ */
+ default:
+ assert("invalid GL token in translate_blend()" == NULL);
+ return 0;
+ }
+}
+
+
+/**
+ * Convert GLenum logicop tokens to pipe tokens.
+ */
+static GLuint
+translate_logicop(GLenum logicop)
+{
+ switch (logicop) {
+ case GL_CLEAR:
+ return PIPE_LOGICOP_CLEAR;
+ case GL_NOR:
+ return PIPE_LOGICOP_NOR;
+ case GL_AND_INVERTED:
+ return PIPE_LOGICOP_AND_INVERTED;
+ case GL_COPY_INVERTED:
+ return PIPE_LOGICOP_COPY_INVERTED;
+ case GL_AND_REVERSE:
+ return PIPE_LOGICOP_AND_REVERSE;
+ case GL_INVERT:
+ return PIPE_LOGICOP_INVERT;
+ case GL_XOR:
+ return PIPE_LOGICOP_XOR;
+ case GL_NAND:
+ return PIPE_LOGICOP_NAND;
+ case GL_AND:
+ return PIPE_LOGICOP_AND;
+ case GL_EQUIV:
+ return PIPE_LOGICOP_EQUIV;
+ case GL_NOOP:
+ return PIPE_LOGICOP_NOOP;
+ case GL_OR_INVERTED:
+ return PIPE_LOGICOP_OR_INVERTED;
+ case GL_COPY:
+ return PIPE_LOGICOP_COPY;
+ case GL_OR_REVERSE:
+ return PIPE_LOGICOP_OR_REVERSE;
+ case GL_OR:
+ return PIPE_LOGICOP_OR;
+ case GL_SET:
+ return PIPE_LOGICOP_SET;
+ default:
+ assert("invalid GL token in translate_logicop()" == NULL);
+ return 0;
+ }
+}
+
+/**
+ * Figure out if colormasks are different per rt.
+ */
+static GLboolean
+colormask_per_rt(struct gl_context *ctx)
+{
+ /* a bit suboptimal have to compare lots of values */
+ unsigned i;
+ for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
+ if (memcmp(ctx->Color.ColorMask[0], ctx->Color.ColorMask[i], 4)) {
+ return GL_TRUE;
+ }
+ }
+ return GL_FALSE;
+}
+
+/**
+ * Figure out if blend enables/state are different per rt.
+ */
+static GLboolean
+blend_per_rt(struct gl_context *ctx)
+{
+ if (ctx->Color.BlendEnabled &&
+ (ctx->Color.BlendEnabled != ((1 << ctx->Const.MaxDrawBuffers) - 1))) {
+ /* This can only happen if GL_EXT_draw_buffers2 is enabled */
+ return GL_TRUE;
+ }
+ if (ctx->Color._BlendFuncPerBuffer || ctx->Color._BlendEquationPerBuffer) {
+ /* this can only happen if GL_ARB_draw_buffers_blend is enabled */
+ return GL_TRUE;
+ }
+ return GL_FALSE;
+}
+
+static void
+update_blend( struct st_context *st )
+{
+ struct pipe_blend_state *blend = &st->state.blend;
+ unsigned num_state = 1;
+ unsigned i, j;
+
+ memset(blend, 0, sizeof(*blend));
+
+ if (blend_per_rt(st->ctx) || colormask_per_rt(st->ctx)) {
+ num_state = st->ctx->Const.MaxDrawBuffers;
+ blend->independent_blend_enable = 1;
+ }
+ /* Note it is impossible to correctly deal with EXT_blend_logic_op and
+ EXT_draw_buffers2/EXT_blend_equation_separate at the same time.
+ These combinations would require support for per-rt logicop enables
+ and separate alpha/rgb logicop/blend support respectively. Neither
+ possible in gallium nor most hardware. Assume these combinations
+ don't happen. */
+ if (st->ctx->Color.ColorLogicOpEnabled ||
+ (st->ctx->Color.BlendEnabled &&
+ st->ctx->Color.Blend[0].EquationRGB == GL_LOGIC_OP)) {
+ /* logicop enabled */
+ blend->logicop_enable = 1;
+ blend->logicop_func = translate_logicop(st->ctx->Color.LogicOp);
+ }
+ else if (st->ctx->Color.BlendEnabled) {
+ /* blending enabled */
+ for (i = 0, j = 0; i < num_state; i++) {
+
+ blend->rt[i].blend_enable = (st->ctx->Color.BlendEnabled >> i) & 0x1;
+
+ if (st->ctx->Extensions.ARB_draw_buffers_blend)
+ j = i;
+
+ blend->rt[i].rgb_func =
+ translate_blend(st->ctx->Color.Blend[j].EquationRGB);
+
+ if (st->ctx->Color.Blend[i].EquationRGB == GL_MIN ||
+ st->ctx->Color.Blend[i].EquationRGB == GL_MAX) {
+ /* Min/max are special */
+ blend->rt[i].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
+ blend->rt[i].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
+ }
+ else {
+ blend->rt[i].rgb_src_factor =
+ translate_blend(st->ctx->Color.Blend[j].SrcRGB);
+ blend->rt[i].rgb_dst_factor =
+ translate_blend(st->ctx->Color.Blend[j].DstRGB);
+ }
+
+ blend->rt[i].alpha_func =
+ translate_blend(st->ctx->Color.Blend[j].EquationA);
+
+ if (st->ctx->Color.Blend[i].EquationA == GL_MIN ||
+ st->ctx->Color.Blend[i].EquationA == GL_MAX) {
+ /* Min/max are special */
+ blend->rt[i].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
+ blend->rt[i].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
+ }
+ else {
+ blend->rt[i].alpha_src_factor =
+ translate_blend(st->ctx->Color.Blend[j].SrcA);
+ blend->rt[i].alpha_dst_factor =
+ translate_blend(st->ctx->Color.Blend[j].DstA);
+ }
+ }
+ }
+ else {
+ /* no blending / logicop */
+ }
+
+ /* Colormask - maybe reverse these bits? */
+ for (i = 0; i < num_state; i++) {
+ if (st->ctx->Color.ColorMask[i][0])
+ blend->rt[i].colormask |= PIPE_MASK_R;
+ if (st->ctx->Color.ColorMask[i][1])
+ blend->rt[i].colormask |= PIPE_MASK_G;
+ if (st->ctx->Color.ColorMask[i][2])
+ blend->rt[i].colormask |= PIPE_MASK_B;
+ if (st->ctx->Color.ColorMask[i][3])
+ blend->rt[i].colormask |= PIPE_MASK_A;
+ }
+
+ if (st->ctx->Color.DitherFlag)
+ blend->dither = 1;
+
+ if (st->ctx->Multisample.Enabled) {
+ /* unlike in gallium/d3d10 these operations are only performed
+ if msaa is enabled */
+ if (st->ctx->Multisample.SampleAlphaToCoverage)
+ blend->alpha_to_coverage = 1;
+ if (st->ctx->Multisample.SampleAlphaToOne)
+ blend->alpha_to_one = 1;
+ }
+
+ cso_set_blend(st->cso_context, blend);
+
+ {
+ struct pipe_blend_color bc;
+ COPY_4FV(bc.color, st->ctx->Color.BlendColor);
+ cso_set_blend_color(st->cso_context, &bc);
+ }
+}
+
+
+const struct st_tracked_state st_update_blend = {
+ "st_update_blend", /* name */
+ { /* dirty */
+ (_NEW_COLOR | _NEW_MULTISAMPLE), /* XXX _NEW_BLEND someday? */ /* mesa */
+ 0, /* st */
+ },
+ update_blend, /* update */
+};
diff --git a/mesalib/src/mesa/state_tracker/st_atom_framebuffer.c b/mesalib/src/mesa/state_tracker/st_atom_framebuffer.c index 76386fe01..678a270c7 100644 --- a/mesalib/src/mesa/state_tracker/st_atom_framebuffer.c +++ b/mesalib/src/mesa/state_tracker/st_atom_framebuffer.c @@ -1,182 +1,182 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - - /* - * Authors: - * Keith Whitwell <keith@tungstengraphics.com> - * Brian Paul - */ - -#include "st_context.h" -#include "st_atom.h" -#include "st_cb_fbo.h" -#include "st_texture.h" -#include "pipe/p_context.h" -#include "cso_cache/cso_context.h" -#include "util/u_math.h" -#include "util/u_inlines.h" -#include "util/u_format.h" - - -/** - * When doing GL render to texture, we have to be sure that finalize_texture() - * didn't yank out the pipe_resource that we earlier created a surface for. - * Check for that here and create a new surface if needed. - */ -static void -update_renderbuffer_surface(struct st_context *st, - struct st_renderbuffer *strb) -{ - struct pipe_context *pipe = st->pipe; - struct pipe_resource *resource = strb->rtt->pt; - int rtt_width = strb->Base.Width; - int rtt_height = strb->Base.Height; - enum pipe_format format = st->ctx->Color.sRGBEnabled ? resource->format : util_format_linear(resource->format); - - if (!strb->surface || - strb->surface->format != format || - strb->surface->texture != resource || - strb->surface->width != rtt_width || - strb->surface->height != rtt_height) { - GLuint level; - /* find matching mipmap level size */ - for (level = 0; level <= resource->last_level; level++) { - if (u_minify(resource->width0, level) == rtt_width && - u_minify(resource->height0, level) == rtt_height) { - struct pipe_surface surf_tmpl; - memset(&surf_tmpl, 0, sizeof(surf_tmpl)); - surf_tmpl.format = format; - surf_tmpl.usage = PIPE_BIND_RENDER_TARGET; - surf_tmpl.u.tex.level = level; - surf_tmpl.u.tex.first_layer = strb->rtt_face + strb->rtt_slice; - surf_tmpl.u.tex.last_layer = strb->rtt_face + strb->rtt_slice; - - pipe_surface_reference(&strb->surface, NULL); - - strb->surface = pipe->create_surface(pipe, - resource, - &surf_tmpl); -#if 0 - printf("-- alloc new surface %d x %d into tex %p\n", - strb->surface->width, strb->surface->height, - texture); -#endif - break; - } - } - } -} - - -/** - * Update framebuffer state (color, depth, stencil, etc. buffers) - */ -static void -update_framebuffer_state( struct st_context *st ) -{ - struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer; - struct gl_framebuffer *fb = st->ctx->DrawBuffer; - struct st_renderbuffer *strb; - GLuint i; - - framebuffer->width = fb->Width; - framebuffer->height = fb->Height; - - /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/ - - /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state - * to determine which surfaces to draw to - */ - framebuffer->nr_cbufs = 0; - for (i = 0; i < fb->_NumColorDrawBuffers; i++) { - strb = st_renderbuffer(fb->_ColorDrawBuffers[i]); - - if (strb) { - /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/ - if (strb->rtt) { - /* rendering to a GL texture, may have to update surface */ - update_renderbuffer_surface(st, strb); - } - - if (strb->surface) { - pipe_surface_reference(&framebuffer->cbufs[framebuffer->nr_cbufs], - strb->surface); - framebuffer->nr_cbufs++; - } - strb->defined = GL_TRUE; /* we'll be drawing something */ - } - } - for (i = framebuffer->nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) { - pipe_surface_reference(&framebuffer->cbufs[i], NULL); - } - - /* - * Depth/Stencil renderbuffer/surface. - */ - strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer); - if (strb) { - strb = st_renderbuffer(strb->Base.Wrapped); - if (strb->rtt) { - /* rendering to a GL texture, may have to update surface */ - update_renderbuffer_surface(st, strb); - } - pipe_surface_reference(&framebuffer->zsbuf, strb->surface); - } - else { - strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer); - if (strb) { - strb = st_renderbuffer(strb->Base.Wrapped); - assert(strb->surface); - pipe_surface_reference(&framebuffer->zsbuf, strb->surface); - } - else - pipe_surface_reference(&framebuffer->zsbuf, NULL); - } - -#ifdef DEBUG - /* Make sure the resource binding flags were set properly */ - for (i = 0; i < framebuffer->nr_cbufs; i++) { - assert(framebuffer->cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET); - } - if (framebuffer->zsbuf) { - assert(framebuffer->zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL); - } -#endif - - cso_set_framebuffer(st->cso_context, framebuffer); -} - - -const struct st_tracked_state st_update_framebuffer = { - "st_update_framebuffer", /* name */ - { /* dirty */ - _NEW_BUFFERS, /* mesa */ - ST_NEW_FRAMEBUFFER, /* st */ - }, - update_framebuffer_state /* update */ -}; - +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+ /*
+ * Authors:
+ * Keith Whitwell <keith@tungstengraphics.com>
+ * Brian Paul
+ */
+
+#include "st_context.h"
+#include "st_atom.h"
+#include "st_cb_fbo.h"
+#include "st_texture.h"
+#include "pipe/p_context.h"
+#include "cso_cache/cso_context.h"
+#include "util/u_math.h"
+#include "util/u_inlines.h"
+#include "util/u_format.h"
+
+
+/**
+ * When doing GL render to texture, we have to be sure that finalize_texture()
+ * didn't yank out the pipe_resource that we earlier created a surface for.
+ * Check for that here and create a new surface if needed.
+ */
+static void
+update_renderbuffer_surface(struct st_context *st,
+ struct st_renderbuffer *strb)
+{
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_resource *resource = strb->rtt->pt;
+ int rtt_width = strb->Base.Width;
+ int rtt_height = strb->Base.Height;
+ enum pipe_format format = st->ctx->Color.sRGBEnabled ? resource->format : util_format_linear(resource->format);
+
+ if (!strb->surface ||
+ strb->surface->format != format ||
+ strb->surface->texture != resource ||
+ strb->surface->width != rtt_width ||
+ strb->surface->height != rtt_height) {
+ GLuint level;
+ /* find matching mipmap level size */
+ for (level = 0; level <= resource->last_level; level++) {
+ if (u_minify(resource->width0, level) == rtt_width &&
+ u_minify(resource->height0, level) == rtt_height) {
+ struct pipe_surface surf_tmpl;
+ memset(&surf_tmpl, 0, sizeof(surf_tmpl));
+ surf_tmpl.format = format;
+ surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
+ surf_tmpl.u.tex.level = level;
+ surf_tmpl.u.tex.first_layer = strb->rtt_face + strb->rtt_slice;
+ surf_tmpl.u.tex.last_layer = strb->rtt_face + strb->rtt_slice;
+
+ pipe_surface_reference(&strb->surface, NULL);
+
+ strb->surface = pipe->create_surface(pipe,
+ resource,
+ &surf_tmpl);
+#if 0
+ printf("-- alloc new surface %d x %d into tex %p\n",
+ strb->surface->width, strb->surface->height,
+ texture);
+#endif
+ break;
+ }
+ }
+ }
+}
+
+
+/**
+ * Update framebuffer state (color, depth, stencil, etc. buffers)
+ */
+static void
+update_framebuffer_state( struct st_context *st )
+{
+ struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
+ struct gl_framebuffer *fb = st->ctx->DrawBuffer;
+ struct st_renderbuffer *strb;
+ GLuint i;
+
+ framebuffer->width = fb->Width;
+ framebuffer->height = fb->Height;
+
+ /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
+
+ /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
+ * to determine which surfaces to draw to
+ */
+ framebuffer->nr_cbufs = 0;
+ for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
+ strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
+
+ if (strb) {
+ /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
+ if (strb->rtt) {
+ /* rendering to a GL texture, may have to update surface */
+ update_renderbuffer_surface(st, strb);
+ }
+
+ if (strb->surface) {
+ pipe_surface_reference(&framebuffer->cbufs[framebuffer->nr_cbufs],
+ strb->surface);
+ framebuffer->nr_cbufs++;
+ }
+ strb->defined = GL_TRUE; /* we'll be drawing something */
+ }
+ }
+ for (i = framebuffer->nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
+ pipe_surface_reference(&framebuffer->cbufs[i], NULL);
+ }
+
+ /*
+ * Depth/Stencil renderbuffer/surface.
+ */
+ strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
+ if (strb) {
+ strb = st_renderbuffer(strb->Base.Wrapped);
+ if (strb->rtt) {
+ /* rendering to a GL texture, may have to update surface */
+ update_renderbuffer_surface(st, strb);
+ }
+ pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
+ }
+ else {
+ strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
+ if (strb) {
+ strb = st_renderbuffer(strb->Base.Wrapped);
+ assert(strb->surface);
+ pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
+ }
+ else
+ pipe_surface_reference(&framebuffer->zsbuf, NULL);
+ }
+
+#ifdef DEBUG
+ /* Make sure the resource binding flags were set properly */
+ for (i = 0; i < framebuffer->nr_cbufs; i++) {
+ assert(framebuffer->cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET);
+ }
+ if (framebuffer->zsbuf) {
+ assert(framebuffer->zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL);
+ }
+#endif
+
+ cso_set_framebuffer(st->cso_context, framebuffer);
+}
+
+
+const struct st_tracked_state st_update_framebuffer = {
+ "st_update_framebuffer", /* name */
+ { /* dirty */
+ _NEW_BUFFERS, /* mesa */
+ ST_NEW_FRAMEBUFFER, /* st */
+ },
+ update_framebuffer_state /* update */
+};
+
diff --git a/mesalib/src/mesa/state_tracker/st_atom_pixeltransfer.c b/mesalib/src/mesa/state_tracker/st_atom_pixeltransfer.c index 9557adc2d..e0a1cba2b 100644 --- a/mesalib/src/mesa/state_tracker/st_atom_pixeltransfer.c +++ b/mesalib/src/mesa/state_tracker/st_atom_pixeltransfer.c @@ -1,354 +1,354 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - -/* - * Generate fragment programs to implement pixel transfer ops, such as - * scale/bias, colortable, convolution... - * - * Authors: - * Brian Paul - */ - -#include "main/imports.h" -#include "main/image.h" -#include "main/macros.h" -#include "program/program.h" -#include "program/prog_cache.h" -#include "program/prog_instruction.h" -#include "program/prog_parameter.h" -#include "program/prog_print.h" - -#include "st_context.h" -#include "st_format.h" -#include "st_texture.h" - -#include "pipe/p_screen.h" -#include "pipe/p_context.h" -#include "util/u_inlines.h" -#include "util/u_pack_color.h" - - -struct state_key -{ - GLuint scaleAndBias:1; - GLuint pixelMaps:1; - -#if 0 - GLfloat Maps[3][256][4]; - int NumMaps; - GLint NumStages; - pipeline_stage Stages[STAGE_MAX]; - GLboolean StagesUsed[STAGE_MAX]; - GLfloat Scale1[4], Bias1[4]; - GLfloat Scale2[4], Bias2[4]; -#endif -}; - -static void -make_state_key(struct gl_context *ctx, struct state_key *key) -{ - memset(key, 0, sizeof(*key)); - - if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 || - ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 || - ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 || - ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) { - key->scaleAndBias = 1; - } - - key->pixelMaps = ctx->Pixel.MapColorFlag; -} - - -static struct pipe_resource * -create_color_map_texture(struct gl_context *ctx) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - struct pipe_resource *pt; - enum pipe_format format; - const uint texSize = 256; /* simple, and usually perfect */ - - /* find an RGBA texture format */ - format = st_choose_format(pipe->screen, GL_RGBA, - PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW); - - /* create texture for color map/table */ - pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0, - texSize, texSize, 1, 1, PIPE_BIND_SAMPLER_VIEW); - return pt; -} - - -/** - * Update the pixelmap texture with the contents of the R/G/B/A pixel maps. - */ -static void -load_color_map_texture(struct gl_context *ctx, struct pipe_resource *pt) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - struct pipe_transfer *transfer; - const GLuint rSize = ctx->PixelMaps.RtoR.Size; - const GLuint gSize = ctx->PixelMaps.GtoG.Size; - const GLuint bSize = ctx->PixelMaps.BtoB.Size; - const GLuint aSize = ctx->PixelMaps.AtoA.Size; - const uint texSize = pt->width0; - uint *dest; - uint i, j; - - transfer = pipe_get_transfer(st_context(ctx)->pipe, - pt, 0, 0, PIPE_TRANSFER_WRITE, - 0, 0, texSize, texSize); - dest = (uint *) pipe_transfer_map(pipe, transfer); - - /* Pack four 1D maps into a 2D texture: - * R map is placed horizontally, indexed by S, in channel 0 - * G map is placed vertically, indexed by T, in channel 1 - * B map is placed horizontally, indexed by S, in channel 2 - * A map is placed vertically, indexed by T, in channel 3 - */ - for (i = 0; i < texSize; i++) { - for (j = 0; j < texSize; j++) { - union util_color uc; - int k = (i * texSize + j); - ubyte r = ctx->PixelMaps.RtoR.Map8[j * rSize / texSize]; - ubyte g = ctx->PixelMaps.GtoG.Map8[i * gSize / texSize]; - ubyte b = ctx->PixelMaps.BtoB.Map8[j * bSize / texSize]; - ubyte a = ctx->PixelMaps.AtoA.Map8[i * aSize / texSize]; - util_pack_color_ub(r, g, b, a, pt->format, &uc); - *(dest + k) = uc.ui; - } - } - - pipe_transfer_unmap(pipe, transfer); - pipe->transfer_destroy(pipe, transfer); -} - - - -#define MAX_INST 100 - -/** - * Returns a fragment program which implements the current pixel transfer ops. - */ -static struct gl_fragment_program * -get_pixel_transfer_program(struct gl_context *ctx, const struct state_key *key) -{ - struct st_context *st = st_context(ctx); - struct prog_instruction inst[MAX_INST]; - struct gl_program_parameter_list *params; - struct gl_fragment_program *fp; - GLuint ic = 0; - const GLuint colorTemp = 0; - - fp = (struct gl_fragment_program *) - ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0); - if (!fp) - return NULL; - - params = _mesa_new_parameter_list(); - - /* - * Get initial pixel color from the texture. - * TEX colorTemp, fragment.texcoord[0], texture[0], 2D; - */ - _mesa_init_instructions(inst + ic, 1); - inst[ic].Opcode = OPCODE_TEX; - inst[ic].DstReg.File = PROGRAM_TEMPORARY; - inst[ic].DstReg.Index = colorTemp; - inst[ic].SrcReg[0].File = PROGRAM_INPUT; - inst[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0; - inst[ic].TexSrcUnit = 0; - inst[ic].TexSrcTarget = TEXTURE_2D_INDEX; - ic++; - fp->Base.InputsRead = (1 << FRAG_ATTRIB_TEX0); - fp->Base.OutputsWritten = BITFIELD64_BIT(FRAG_RESULT_COLOR); - fp->Base.SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */ - - if (key->scaleAndBias) { - static const gl_state_index scale_state[STATE_LENGTH] = - { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 }; - static const gl_state_index bias_state[STATE_LENGTH] = - { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 }; - GLfloat scale[4], bias[4]; - GLint scale_p, bias_p; - - scale[0] = ctx->Pixel.RedScale; - scale[1] = ctx->Pixel.GreenScale; - scale[2] = ctx->Pixel.BlueScale; - scale[3] = ctx->Pixel.AlphaScale; - bias[0] = ctx->Pixel.RedBias; - bias[1] = ctx->Pixel.GreenBias; - bias[2] = ctx->Pixel.BlueBias; - bias[3] = ctx->Pixel.AlphaBias; - - scale_p = _mesa_add_state_reference(params, scale_state); - bias_p = _mesa_add_state_reference(params, bias_state); - - /* MAD colorTemp, colorTemp, scale, bias; */ - _mesa_init_instructions(inst + ic, 1); - inst[ic].Opcode = OPCODE_MAD; - inst[ic].DstReg.File = PROGRAM_TEMPORARY; - inst[ic].DstReg.Index = colorTemp; - inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; - inst[ic].SrcReg[0].Index = colorTemp; - inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR; - inst[ic].SrcReg[1].Index = scale_p; - inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR; - inst[ic].SrcReg[2].Index = bias_p; - ic++; - } - - if (key->pixelMaps) { - const GLuint temp = 1; - - /* create the colormap/texture now if not already done */ - if (!st->pixel_xfer.pixelmap_texture) { - st->pixel_xfer.pixelmap_texture = create_color_map_texture(ctx); - st->pixel_xfer.pixelmap_sampler_view = - st_create_texture_sampler_view(st->pipe, - st->pixel_xfer.pixelmap_texture); - } - - /* with a little effort, we can do four pixel map look-ups with - * two TEX instructions: - */ - - /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */ - _mesa_init_instructions(inst + ic, 1); - inst[ic].Opcode = OPCODE_TEX; - inst[ic].DstReg.File = PROGRAM_TEMPORARY; - inst[ic].DstReg.Index = temp; - inst[ic].DstReg.WriteMask = WRITEMASK_XY; /* write R,G */ - inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; - inst[ic].SrcReg[0].Index = colorTemp; - inst[ic].TexSrcUnit = 1; - inst[ic].TexSrcTarget = TEXTURE_2D_INDEX; - ic++; - - /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */ - _mesa_init_instructions(inst + ic, 1); - inst[ic].Opcode = OPCODE_TEX; - inst[ic].DstReg.File = PROGRAM_TEMPORARY; - inst[ic].DstReg.Index = temp; - inst[ic].DstReg.WriteMask = WRITEMASK_ZW; /* write B,A */ - inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; - inst[ic].SrcReg[0].Index = colorTemp; - inst[ic].SrcReg[0].Swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, - SWIZZLE_Z, SWIZZLE_W); - inst[ic].TexSrcUnit = 1; - inst[ic].TexSrcTarget = TEXTURE_2D_INDEX; - ic++; - - /* MOV colorTemp, temp; */ - _mesa_init_instructions(inst + ic, 1); - inst[ic].Opcode = OPCODE_MOV; - inst[ic].DstReg.File = PROGRAM_TEMPORARY; - inst[ic].DstReg.Index = colorTemp; - inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY; - inst[ic].SrcReg[0].Index = temp; - ic++; - - fp->Base.SamplersUsed |= (1 << 1); /* sampler 1 is used */ - } - - /* Modify last instruction's dst reg to write to result.color */ - { - struct prog_instruction *last = &inst[ic - 1]; - last->DstReg.File = PROGRAM_OUTPUT; - last->DstReg.Index = FRAG_RESULT_COLOR; - } - - /* END; */ - _mesa_init_instructions(inst + ic, 1); - inst[ic].Opcode = OPCODE_END; - ic++; - - assert(ic <= MAX_INST); - - - fp->Base.Instructions = _mesa_alloc_instructions(ic); - if (!fp->Base.Instructions) { - _mesa_error(ctx, GL_OUT_OF_MEMORY, - "generating pixel transfer program"); - return NULL; - } - - _mesa_copy_instructions(fp->Base.Instructions, inst, ic); - fp->Base.NumInstructions = ic; - fp->Base.Parameters = params; - -#if 0 - printf("========= pixel transfer prog\n"); - _mesa_print_program(&fp->Base); - _mesa_print_parameter_list(fp->Base.Parameters); -#endif - - return fp; -} - - - -/** - * Update st->pixel_xfer.program in response to new pixel-transfer state. - */ -static void -update_pixel_transfer(struct st_context *st) -{ - struct gl_context *ctx = st->ctx; - struct state_key key; - struct gl_fragment_program *fp; - - make_state_key(st->ctx, &key); - - fp = (struct gl_fragment_program *) - _mesa_search_program_cache(st->pixel_xfer.cache, &key, sizeof(key)); - if (!fp) { - fp = get_pixel_transfer_program(st->ctx, &key); - _mesa_program_cache_insert(st->ctx, st->pixel_xfer.cache, - &key, sizeof(key), &fp->Base); - } - - if (ctx->Pixel.MapColorFlag) { - load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture); - } - st->pixel_xfer.pixelmap_enabled = ctx->Pixel.MapColorFlag; - - st->pixel_xfer.program = (struct st_fragment_program *) fp; -} - - - -const struct st_tracked_state st_update_pixel_transfer = { - "st_update_pixel_transfer", /* name */ - { /* dirty */ - _NEW_PIXEL, /* mesa */ - 0, /* st */ - }, - update_pixel_transfer /* update */ -}; +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+/*
+ * Generate fragment programs to implement pixel transfer ops, such as
+ * scale/bias, colortable, convolution...
+ *
+ * Authors:
+ * Brian Paul
+ */
+
+#include "main/imports.h"
+#include "main/image.h"
+#include "main/macros.h"
+#include "program/program.h"
+#include "program/prog_cache.h"
+#include "program/prog_instruction.h"
+#include "program/prog_parameter.h"
+#include "program/prog_print.h"
+
+#include "st_context.h"
+#include "st_format.h"
+#include "st_texture.h"
+
+#include "pipe/p_screen.h"
+#include "pipe/p_context.h"
+#include "util/u_inlines.h"
+#include "util/u_pack_color.h"
+
+
+struct state_key
+{
+ GLuint scaleAndBias:1;
+ GLuint pixelMaps:1;
+
+#if 0
+ GLfloat Maps[3][256][4];
+ int NumMaps;
+ GLint NumStages;
+ pipeline_stage Stages[STAGE_MAX];
+ GLboolean StagesUsed[STAGE_MAX];
+ GLfloat Scale1[4], Bias1[4];
+ GLfloat Scale2[4], Bias2[4];
+#endif
+};
+
+static void
+make_state_key(struct gl_context *ctx, struct state_key *key)
+{
+ memset(key, 0, sizeof(*key));
+
+ if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
+ ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
+ ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
+ ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
+ key->scaleAndBias = 1;
+ }
+
+ key->pixelMaps = ctx->Pixel.MapColorFlag;
+}
+
+
+static struct pipe_resource *
+create_color_map_texture(struct gl_context *ctx)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_resource *pt;
+ enum pipe_format format;
+ const uint texSize = 256; /* simple, and usually perfect */
+
+ /* find an RGBA texture format */
+ format = st_choose_format(pipe->screen, GL_RGBA,
+ PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW);
+
+ /* create texture for color map/table */
+ pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
+ texSize, texSize, 1, 1, PIPE_BIND_SAMPLER_VIEW);
+ return pt;
+}
+
+
+/**
+ * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
+ */
+static void
+load_color_map_texture(struct gl_context *ctx, struct pipe_resource *pt)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_transfer *transfer;
+ const GLuint rSize = ctx->PixelMaps.RtoR.Size;
+ const GLuint gSize = ctx->PixelMaps.GtoG.Size;
+ const GLuint bSize = ctx->PixelMaps.BtoB.Size;
+ const GLuint aSize = ctx->PixelMaps.AtoA.Size;
+ const uint texSize = pt->width0;
+ uint *dest;
+ uint i, j;
+
+ transfer = pipe_get_transfer(st_context(ctx)->pipe,
+ pt, 0, 0, PIPE_TRANSFER_WRITE,
+ 0, 0, texSize, texSize);
+ dest = (uint *) pipe_transfer_map(pipe, transfer);
+
+ /* Pack four 1D maps into a 2D texture:
+ * R map is placed horizontally, indexed by S, in channel 0
+ * G map is placed vertically, indexed by T, in channel 1
+ * B map is placed horizontally, indexed by S, in channel 2
+ * A map is placed vertically, indexed by T, in channel 3
+ */
+ for (i = 0; i < texSize; i++) {
+ for (j = 0; j < texSize; j++) {
+ union util_color uc;
+ int k = (i * texSize + j);
+ ubyte r = ctx->PixelMaps.RtoR.Map8[j * rSize / texSize];
+ ubyte g = ctx->PixelMaps.GtoG.Map8[i * gSize / texSize];
+ ubyte b = ctx->PixelMaps.BtoB.Map8[j * bSize / texSize];
+ ubyte a = ctx->PixelMaps.AtoA.Map8[i * aSize / texSize];
+ util_pack_color_ub(r, g, b, a, pt->format, &uc);
+ *(dest + k) = uc.ui;
+ }
+ }
+
+ pipe_transfer_unmap(pipe, transfer);
+ pipe->transfer_destroy(pipe, transfer);
+}
+
+
+
+#define MAX_INST 100
+
+/**
+ * Returns a fragment program which implements the current pixel transfer ops.
+ */
+static struct gl_fragment_program *
+get_pixel_transfer_program(struct gl_context *ctx, const struct state_key *key)
+{
+ struct st_context *st = st_context(ctx);
+ struct prog_instruction inst[MAX_INST];
+ struct gl_program_parameter_list *params;
+ struct gl_fragment_program *fp;
+ GLuint ic = 0;
+ const GLuint colorTemp = 0;
+
+ fp = (struct gl_fragment_program *)
+ ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
+ if (!fp)
+ return NULL;
+
+ params = _mesa_new_parameter_list();
+
+ /*
+ * Get initial pixel color from the texture.
+ * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
+ */
+ _mesa_init_instructions(inst + ic, 1);
+ inst[ic].Opcode = OPCODE_TEX;
+ inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+ inst[ic].DstReg.Index = colorTemp;
+ inst[ic].SrcReg[0].File = PROGRAM_INPUT;
+ inst[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
+ inst[ic].TexSrcUnit = 0;
+ inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+ ic++;
+ fp->Base.InputsRead = (1 << FRAG_ATTRIB_TEX0);
+ fp->Base.OutputsWritten = BITFIELD64_BIT(FRAG_RESULT_COLOR);
+ fp->Base.SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
+
+ if (key->scaleAndBias) {
+ static const gl_state_index scale_state[STATE_LENGTH] =
+ { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 };
+ static const gl_state_index bias_state[STATE_LENGTH] =
+ { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 };
+ GLfloat scale[4], bias[4];
+ GLint scale_p, bias_p;
+
+ scale[0] = ctx->Pixel.RedScale;
+ scale[1] = ctx->Pixel.GreenScale;
+ scale[2] = ctx->Pixel.BlueScale;
+ scale[3] = ctx->Pixel.AlphaScale;
+ bias[0] = ctx->Pixel.RedBias;
+ bias[1] = ctx->Pixel.GreenBias;
+ bias[2] = ctx->Pixel.BlueBias;
+ bias[3] = ctx->Pixel.AlphaBias;
+
+ scale_p = _mesa_add_state_reference(params, scale_state);
+ bias_p = _mesa_add_state_reference(params, bias_state);
+
+ /* MAD colorTemp, colorTemp, scale, bias; */
+ _mesa_init_instructions(inst + ic, 1);
+ inst[ic].Opcode = OPCODE_MAD;
+ inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+ inst[ic].DstReg.Index = colorTemp;
+ inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+ inst[ic].SrcReg[0].Index = colorTemp;
+ inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
+ inst[ic].SrcReg[1].Index = scale_p;
+ inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
+ inst[ic].SrcReg[2].Index = bias_p;
+ ic++;
+ }
+
+ if (key->pixelMaps) {
+ const GLuint temp = 1;
+
+ /* create the colormap/texture now if not already done */
+ if (!st->pixel_xfer.pixelmap_texture) {
+ st->pixel_xfer.pixelmap_texture = create_color_map_texture(ctx);
+ st->pixel_xfer.pixelmap_sampler_view =
+ st_create_texture_sampler_view(st->pipe,
+ st->pixel_xfer.pixelmap_texture);
+ }
+
+ /* with a little effort, we can do four pixel map look-ups with
+ * two TEX instructions:
+ */
+
+ /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
+ _mesa_init_instructions(inst + ic, 1);
+ inst[ic].Opcode = OPCODE_TEX;
+ inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+ inst[ic].DstReg.Index = temp;
+ inst[ic].DstReg.WriteMask = WRITEMASK_XY; /* write R,G */
+ inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+ inst[ic].SrcReg[0].Index = colorTemp;
+ inst[ic].TexSrcUnit = 1;
+ inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+ ic++;
+
+ /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
+ _mesa_init_instructions(inst + ic, 1);
+ inst[ic].Opcode = OPCODE_TEX;
+ inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+ inst[ic].DstReg.Index = temp;
+ inst[ic].DstReg.WriteMask = WRITEMASK_ZW; /* write B,A */
+ inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+ inst[ic].SrcReg[0].Index = colorTemp;
+ inst[ic].SrcReg[0].Swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W,
+ SWIZZLE_Z, SWIZZLE_W);
+ inst[ic].TexSrcUnit = 1;
+ inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+ ic++;
+
+ /* MOV colorTemp, temp; */
+ _mesa_init_instructions(inst + ic, 1);
+ inst[ic].Opcode = OPCODE_MOV;
+ inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+ inst[ic].DstReg.Index = colorTemp;
+ inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+ inst[ic].SrcReg[0].Index = temp;
+ ic++;
+
+ fp->Base.SamplersUsed |= (1 << 1); /* sampler 1 is used */
+ }
+
+ /* Modify last instruction's dst reg to write to result.color */
+ {
+ struct prog_instruction *last = &inst[ic - 1];
+ last->DstReg.File = PROGRAM_OUTPUT;
+ last->DstReg.Index = FRAG_RESULT_COLOR;
+ }
+
+ /* END; */
+ _mesa_init_instructions(inst + ic, 1);
+ inst[ic].Opcode = OPCODE_END;
+ ic++;
+
+ assert(ic <= MAX_INST);
+
+
+ fp->Base.Instructions = _mesa_alloc_instructions(ic);
+ if (!fp->Base.Instructions) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY,
+ "generating pixel transfer program");
+ return NULL;
+ }
+
+ _mesa_copy_instructions(fp->Base.Instructions, inst, ic);
+ fp->Base.NumInstructions = ic;
+ fp->Base.Parameters = params;
+
+#if 0
+ printf("========= pixel transfer prog\n");
+ _mesa_print_program(&fp->Base);
+ _mesa_print_parameter_list(fp->Base.Parameters);
+#endif
+
+ return fp;
+}
+
+
+
+/**
+ * Update st->pixel_xfer.program in response to new pixel-transfer state.
+ */
+static void
+update_pixel_transfer(struct st_context *st)
+{
+ struct gl_context *ctx = st->ctx;
+ struct state_key key;
+ struct gl_fragment_program *fp;
+
+ make_state_key(st->ctx, &key);
+
+ fp = (struct gl_fragment_program *)
+ _mesa_search_program_cache(st->pixel_xfer.cache, &key, sizeof(key));
+ if (!fp) {
+ fp = get_pixel_transfer_program(st->ctx, &key);
+ _mesa_program_cache_insert(st->ctx, st->pixel_xfer.cache,
+ &key, sizeof(key), &fp->Base);
+ }
+
+ if (ctx->Pixel.MapColorFlag) {
+ load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture);
+ }
+ st->pixel_xfer.pixelmap_enabled = ctx->Pixel.MapColorFlag;
+
+ st->pixel_xfer.program = (struct st_fragment_program *) fp;
+}
+
+
+
+const struct st_tracked_state st_update_pixel_transfer = {
+ "st_update_pixel_transfer", /* name */
+ { /* dirty */
+ _NEW_PIXEL, /* mesa */
+ 0, /* st */
+ },
+ update_pixel_transfer /* update */
+};
diff --git a/mesalib/src/mesa/state_tracker/st_atom_sampler.c b/mesalib/src/mesa/state_tracker/st_atom_sampler.c index 474cbd589..17596d4c8 100644 --- a/mesalib/src/mesa/state_tracker/st_atom_sampler.c +++ b/mesalib/src/mesa/state_tracker/st_atom_sampler.c @@ -1,228 +1,228 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - - /* - * Authors: - * Keith Whitwell <keith@tungstengraphics.com> - * Brian Paul - */ - - -#include "main/macros.h" - -#include "st_context.h" -#include "st_cb_texture.h" -#include "st_format.h" -#include "st_atom.h" -#include "pipe/p_context.h" -#include "pipe/p_defines.h" - -#include "cso_cache/cso_context.h" - - -/** - * Convert GLenum texcoord wrap tokens to pipe tokens. - */ -static GLuint -gl_wrap_xlate(GLenum wrap) -{ - switch (wrap) { - case GL_REPEAT: - return PIPE_TEX_WRAP_REPEAT; - case GL_CLAMP: - return PIPE_TEX_WRAP_CLAMP; - case GL_CLAMP_TO_EDGE: - return PIPE_TEX_WRAP_CLAMP_TO_EDGE; - case GL_CLAMP_TO_BORDER: - return PIPE_TEX_WRAP_CLAMP_TO_BORDER; - case GL_MIRRORED_REPEAT: - return PIPE_TEX_WRAP_MIRROR_REPEAT; - case GL_MIRROR_CLAMP_EXT: - return PIPE_TEX_WRAP_MIRROR_CLAMP; - case GL_MIRROR_CLAMP_TO_EDGE_EXT: - return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE; - case GL_MIRROR_CLAMP_TO_BORDER_EXT: - return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER; - default: - assert(0); - return 0; - } -} - - -static GLuint -gl_filter_to_mip_filter(GLenum filter) -{ - switch (filter) { - case GL_NEAREST: - case GL_LINEAR: - return PIPE_TEX_MIPFILTER_NONE; - - case GL_NEAREST_MIPMAP_NEAREST: - case GL_LINEAR_MIPMAP_NEAREST: - return PIPE_TEX_MIPFILTER_NEAREST; - - case GL_NEAREST_MIPMAP_LINEAR: - case GL_LINEAR_MIPMAP_LINEAR: - return PIPE_TEX_MIPFILTER_LINEAR; - - default: - assert(0); - return PIPE_TEX_MIPFILTER_NONE; - } -} - - -static GLuint -gl_filter_to_img_filter(GLenum filter) -{ - switch (filter) { - case GL_NEAREST: - case GL_NEAREST_MIPMAP_NEAREST: - case GL_NEAREST_MIPMAP_LINEAR: - return PIPE_TEX_FILTER_NEAREST; - - case GL_LINEAR: - case GL_LINEAR_MIPMAP_NEAREST: - case GL_LINEAR_MIPMAP_LINEAR: - return PIPE_TEX_FILTER_LINEAR; - - default: - assert(0); - return PIPE_TEX_FILTER_NEAREST; - } -} - - -static void -update_samplers(struct st_context *st) -{ - struct gl_vertex_program *vprog = st->ctx->VertexProgram._Current; - struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current; - const GLbitfield samplersUsed = (vprog->Base.SamplersUsed | - fprog->Base.SamplersUsed); - GLuint su; - - st->state.num_samplers = 0; - - /* loop over sampler units (aka tex image units) */ - for (su = 0; su < st->ctx->Const.MaxTextureImageUnits; su++) { - struct pipe_sampler_state *sampler = st->state.samplers + su; - - memset(sampler, 0, sizeof(*sampler)); - - if (samplersUsed & (1 << su)) { - struct gl_texture_object *texobj; - struct gl_texture_image *teximg; - GLuint texUnit; - - if (fprog->Base.SamplersUsed & (1 << su)) - texUnit = fprog->Base.SamplerUnits[su]; - else - texUnit = vprog->Base.SamplerUnits[su]; - - texobj = st->ctx->Texture.Unit[texUnit]._Current; - if (!texobj) { - texobj = st_get_default_texture(st); - } - - teximg = texobj->Image[0][texobj->BaseLevel]; - - sampler->wrap_s = gl_wrap_xlate(texobj->WrapS); - sampler->wrap_t = gl_wrap_xlate(texobj->WrapT); - sampler->wrap_r = gl_wrap_xlate(texobj->WrapR); - - sampler->min_img_filter = gl_filter_to_img_filter(texobj->MinFilter); - sampler->min_mip_filter = gl_filter_to_mip_filter(texobj->MinFilter); - sampler->mag_img_filter = gl_filter_to_img_filter(texobj->MagFilter); - - if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB) - sampler->normalized_coords = 1; - - sampler->lod_bias = st->ctx->Texture.Unit[su].LodBias; - - sampler->min_lod = texobj->BaseLevel + texobj->MinLod; - if (sampler->min_lod < texobj->BaseLevel) - sampler->min_lod = texobj->BaseLevel; - - sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel, - (texobj->MaxLod + texobj->BaseLevel)); - if (sampler->max_lod < sampler->min_lod) { - /* The GL spec doesn't seem to specify what to do in this case. - * Swap the values. - */ - float tmp = sampler->max_lod; - sampler->max_lod = sampler->min_lod; - sampler->min_lod = tmp; - assert(sampler->min_lod <= sampler->max_lod); - } - - st_translate_color(texobj->BorderColor.f, - teximg ? teximg->_BaseFormat : GL_RGBA, - sampler->border_color); - - sampler->max_anisotropy = (texobj->MaxAnisotropy == 1.0 ? 0 : (GLuint)texobj->MaxAnisotropy); - - /* only care about ARB_shadow, not SGI shadow */ - if (texobj->CompareMode == GL_COMPARE_R_TO_TEXTURE) { - sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE; - sampler->compare_func - = st_compare_func_to_pipe(texobj->CompareFunc); - } - - st->state.num_samplers = su + 1; - - /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/ - cso_single_sampler(st->cso_context, su, sampler); - if (su < st->ctx->Const.MaxVertexTextureImageUnits) { - cso_single_vertex_sampler(st->cso_context, su, sampler); - } - } - else { - /*printf("%s su=%u null\n", __FUNCTION__, su);*/ - cso_single_sampler(st->cso_context, su, NULL); - if (su < st->ctx->Const.MaxVertexTextureImageUnits) { - cso_single_vertex_sampler(st->cso_context, su, NULL); - } - } - } - - cso_single_sampler_done(st->cso_context); - if (st->ctx->Const.MaxVertexTextureImageUnits > 0) { - cso_single_vertex_sampler_done(st->cso_context); - } -} - - -const struct st_tracked_state st_update_sampler = { - "st_update_sampler", /* name */ - { /* dirty */ - _NEW_TEXTURE, /* mesa */ - 0, /* st */ - }, - update_samplers /* update */ -}; +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+ /*
+ * Authors:
+ * Keith Whitwell <keith@tungstengraphics.com>
+ * Brian Paul
+ */
+
+
+#include "main/macros.h"
+
+#include "st_context.h"
+#include "st_cb_texture.h"
+#include "st_format.h"
+#include "st_atom.h"
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+
+#include "cso_cache/cso_context.h"
+
+
+/**
+ * Convert GLenum texcoord wrap tokens to pipe tokens.
+ */
+static GLuint
+gl_wrap_xlate(GLenum wrap)
+{
+ switch (wrap) {
+ case GL_REPEAT:
+ return PIPE_TEX_WRAP_REPEAT;
+ case GL_CLAMP:
+ return PIPE_TEX_WRAP_CLAMP;
+ case GL_CLAMP_TO_EDGE:
+ return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
+ case GL_CLAMP_TO_BORDER:
+ return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
+ case GL_MIRRORED_REPEAT:
+ return PIPE_TEX_WRAP_MIRROR_REPEAT;
+ case GL_MIRROR_CLAMP_EXT:
+ return PIPE_TEX_WRAP_MIRROR_CLAMP;
+ case GL_MIRROR_CLAMP_TO_EDGE_EXT:
+ return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
+ case GL_MIRROR_CLAMP_TO_BORDER_EXT:
+ return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER;
+ default:
+ assert(0);
+ return 0;
+ }
+}
+
+
+static GLuint
+gl_filter_to_mip_filter(GLenum filter)
+{
+ switch (filter) {
+ case GL_NEAREST:
+ case GL_LINEAR:
+ return PIPE_TEX_MIPFILTER_NONE;
+
+ case GL_NEAREST_MIPMAP_NEAREST:
+ case GL_LINEAR_MIPMAP_NEAREST:
+ return PIPE_TEX_MIPFILTER_NEAREST;
+
+ case GL_NEAREST_MIPMAP_LINEAR:
+ case GL_LINEAR_MIPMAP_LINEAR:
+ return PIPE_TEX_MIPFILTER_LINEAR;
+
+ default:
+ assert(0);
+ return PIPE_TEX_MIPFILTER_NONE;
+ }
+}
+
+
+static GLuint
+gl_filter_to_img_filter(GLenum filter)
+{
+ switch (filter) {
+ case GL_NEAREST:
+ case GL_NEAREST_MIPMAP_NEAREST:
+ case GL_NEAREST_MIPMAP_LINEAR:
+ return PIPE_TEX_FILTER_NEAREST;
+
+ case GL_LINEAR:
+ case GL_LINEAR_MIPMAP_NEAREST:
+ case GL_LINEAR_MIPMAP_LINEAR:
+ return PIPE_TEX_FILTER_LINEAR;
+
+ default:
+ assert(0);
+ return PIPE_TEX_FILTER_NEAREST;
+ }
+}
+
+
+static void
+update_samplers(struct st_context *st)
+{
+ struct gl_vertex_program *vprog = st->ctx->VertexProgram._Current;
+ struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
+ const GLbitfield samplersUsed = (vprog->Base.SamplersUsed |
+ fprog->Base.SamplersUsed);
+ GLuint su;
+
+ st->state.num_samplers = 0;
+
+ /* loop over sampler units (aka tex image units) */
+ for (su = 0; su < st->ctx->Const.MaxTextureImageUnits; su++) {
+ struct pipe_sampler_state *sampler = st->state.samplers + su;
+
+ memset(sampler, 0, sizeof(*sampler));
+
+ if (samplersUsed & (1 << su)) {
+ struct gl_texture_object *texobj;
+ struct gl_texture_image *teximg;
+ GLuint texUnit;
+
+ if (fprog->Base.SamplersUsed & (1 << su))
+ texUnit = fprog->Base.SamplerUnits[su];
+ else
+ texUnit = vprog->Base.SamplerUnits[su];
+
+ texobj = st->ctx->Texture.Unit[texUnit]._Current;
+ if (!texobj) {
+ texobj = st_get_default_texture(st);
+ }
+
+ teximg = texobj->Image[0][texobj->BaseLevel];
+
+ sampler->wrap_s = gl_wrap_xlate(texobj->WrapS);
+ sampler->wrap_t = gl_wrap_xlate(texobj->WrapT);
+ sampler->wrap_r = gl_wrap_xlate(texobj->WrapR);
+
+ sampler->min_img_filter = gl_filter_to_img_filter(texobj->MinFilter);
+ sampler->min_mip_filter = gl_filter_to_mip_filter(texobj->MinFilter);
+ sampler->mag_img_filter = gl_filter_to_img_filter(texobj->MagFilter);
+
+ if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
+ sampler->normalized_coords = 1;
+
+ sampler->lod_bias = st->ctx->Texture.Unit[su].LodBias;
+
+ sampler->min_lod = texobj->BaseLevel + texobj->MinLod;
+ if (sampler->min_lod < texobj->BaseLevel)
+ sampler->min_lod = texobj->BaseLevel;
+
+ sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel,
+ (texobj->MaxLod + texobj->BaseLevel));
+ if (sampler->max_lod < sampler->min_lod) {
+ /* The GL spec doesn't seem to specify what to do in this case.
+ * Swap the values.
+ */
+ float tmp = sampler->max_lod;
+ sampler->max_lod = sampler->min_lod;
+ sampler->min_lod = tmp;
+ assert(sampler->min_lod <= sampler->max_lod);
+ }
+
+ st_translate_color(texobj->BorderColor.f,
+ teximg ? teximg->_BaseFormat : GL_RGBA,
+ sampler->border_color);
+
+ sampler->max_anisotropy = (texobj->MaxAnisotropy == 1.0 ? 0 : (GLuint)texobj->MaxAnisotropy);
+
+ /* only care about ARB_shadow, not SGI shadow */
+ if (texobj->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
+ sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
+ sampler->compare_func
+ = st_compare_func_to_pipe(texobj->CompareFunc);
+ }
+
+ st->state.num_samplers = su + 1;
+
+ /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/
+ cso_single_sampler(st->cso_context, su, sampler);
+ if (su < st->ctx->Const.MaxVertexTextureImageUnits) {
+ cso_single_vertex_sampler(st->cso_context, su, sampler);
+ }
+ }
+ else {
+ /*printf("%s su=%u null\n", __FUNCTION__, su);*/
+ cso_single_sampler(st->cso_context, su, NULL);
+ if (su < st->ctx->Const.MaxVertexTextureImageUnits) {
+ cso_single_vertex_sampler(st->cso_context, su, NULL);
+ }
+ }
+ }
+
+ cso_single_sampler_done(st->cso_context);
+ if (st->ctx->Const.MaxVertexTextureImageUnits > 0) {
+ cso_single_vertex_sampler_done(st->cso_context);
+ }
+}
+
+
+const struct st_tracked_state st_update_sampler = {
+ "st_update_sampler", /* name */
+ { /* dirty */
+ _NEW_TEXTURE, /* mesa */
+ 0, /* st */
+ },
+ update_samplers /* update */
+};
diff --git a/mesalib/src/mesa/state_tracker/st_cb_drawpixels.c b/mesalib/src/mesa/state_tracker/st_cb_drawpixels.c index eccc20757..2909d9d95 100644 --- a/mesalib/src/mesa/state_tracker/st_cb_drawpixels.c +++ b/mesalib/src/mesa/state_tracker/st_cb_drawpixels.c @@ -1,1488 +1,1488 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - - /* - * Authors: - * Brian Paul - */ - -#include "main/imports.h" -#include "main/image.h" -#include "main/bufferobj.h" -#include "main/macros.h" -#include "main/mfeatures.h" -#include "main/mtypes.h" -#include "main/pack.h" -#include "main/pbo.h" -#include "main/texformat.h" -#include "main/texstore.h" -#include "program/program.h" -#include "program/prog_print.h" -#include "program/prog_instruction.h" - -#include "st_atom.h" -#include "st_atom_constbuf.h" -#include "st_cb_drawpixels.h" -#include "st_cb_readpixels.h" -#include "st_cb_fbo.h" -#include "st_context.h" -#include "st_debug.h" -#include "st_format.h" -#include "st_program.h" -#include "st_texture.h" - -#include "pipe/p_context.h" -#include "pipe/p_defines.h" -#include "tgsi/tgsi_ureg.h" -#include "util/u_draw_quad.h" -#include "util/u_format.h" -#include "util/u_inlines.h" -#include "util/u_math.h" -#include "util/u_tile.h" -#include "cso_cache/cso_context.h" - - -#if FEATURE_drawpix - -/** - * Check if the given program is: - * 0: MOVE result.color, fragment.color; - * 1: END; - */ -static GLboolean -is_passthrough_program(const struct gl_fragment_program *prog) -{ - if (prog->Base.NumInstructions == 2) { - const struct prog_instruction *inst = prog->Base.Instructions; - if (inst[0].Opcode == OPCODE_MOV && - inst[1].Opcode == OPCODE_END && - inst[0].DstReg.File == PROGRAM_OUTPUT && - inst[0].DstReg.Index == FRAG_RESULT_COLOR && - inst[0].DstReg.WriteMask == WRITEMASK_XYZW && - inst[0].SrcReg[0].File == PROGRAM_INPUT && - inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 && - inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) { - return GL_TRUE; - } - } - return GL_FALSE; -} - - - -/** - * Make fragment shader for glDraw/CopyPixels. This shader is made - * by combining the pixel transfer shader with the user-defined shader. - * \param fpIn the current/incoming fragment program - * \param fpOut returns the combined fragment program - */ -void -st_make_drawpix_fragment_program(struct st_context *st, - struct gl_fragment_program *fpIn, - struct gl_fragment_program **fpOut) -{ - struct gl_program *newProg; - - if (is_passthrough_program(fpIn)) { - newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx, - &st->pixel_xfer.program->Base); - } - else { -#if 0 - /* debug */ - printf("Base program:\n"); - _mesa_print_program(&fpIn->Base); - printf("DrawPix program:\n"); - _mesa_print_program(&st->pixel_xfer.program->Base.Base); -#endif - newProg = _mesa_combine_programs(st->ctx, - &st->pixel_xfer.program->Base.Base, - &fpIn->Base); - } - -#if 0 - /* debug */ - printf("Combined DrawPixels program:\n"); - _mesa_print_program(newProg); - printf("InputsRead: 0x%x\n", newProg->InputsRead); - printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten); - _mesa_print_parameter_list(newProg->Parameters); -#endif - - *fpOut = (struct gl_fragment_program *) newProg; -} - - -/** - * Create fragment program that does a TEX() instruction to get a Z and/or - * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL. - * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX). - * Pass fragment color through as-is. - * \return pointer to the gl_fragment program - */ -struct gl_fragment_program * -st_make_drawpix_z_stencil_program(struct st_context *st, - GLboolean write_depth, - GLboolean write_stencil) -{ - struct gl_context *ctx = st->ctx; - struct gl_program *p; - struct gl_fragment_program *fp; - GLuint ic = 0; - const GLuint shaderIndex = write_depth * 2 + write_stencil; - - assert(shaderIndex < Elements(st->drawpix.shaders)); - - if (st->drawpix.shaders[shaderIndex]) { - /* already have the proper shader */ - return st->drawpix.shaders[shaderIndex]; - } - - /* - * Create shader now - */ - p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0); - if (!p) - return NULL; - - p->NumInstructions = write_depth ? 2 : 1; - p->NumInstructions += write_stencil ? 1 : 0; - - p->Instructions = _mesa_alloc_instructions(p->NumInstructions); - if (!p->Instructions) { - ctx->Driver.DeleteProgram(ctx, p); - return NULL; - } - _mesa_init_instructions(p->Instructions, p->NumInstructions); - - if (write_depth) { - /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */ - p->Instructions[ic].Opcode = OPCODE_TEX; - p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT; - p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH; - p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z; - p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT; - p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0; - p->Instructions[ic].TexSrcUnit = 0; - p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX; - ic++; - } - - if (write_stencil) { - /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */ - p->Instructions[ic].Opcode = OPCODE_TEX; - p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT; - p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL; - p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y; - p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT; - p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0; - p->Instructions[ic].TexSrcUnit = 1; - p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX; - ic++; - } - - /* END; */ - p->Instructions[ic++].Opcode = OPCODE_END; - - assert(ic == p->NumInstructions); - - p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0; - p->OutputsWritten = 0; - if (write_depth) - p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_DEPTH); - if (write_stencil) - p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_STENCIL); - - p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */ - if (write_stencil) - p->SamplersUsed |= 1 << 1; - - fp = (struct gl_fragment_program *) p; - - /* save the new shader */ - st->drawpix.shaders[shaderIndex] = fp; - - return fp; -} - - -/** - * Create a simple vertex shader that just passes through the - * vertex position and texcoord (and optionally, color). - */ -static void * -make_passthrough_vertex_shader(struct st_context *st, - GLboolean passColor) -{ - if (!st->drawpix.vert_shaders[passColor]) { - struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX ); - - if (ureg == NULL) - return NULL; - - /* MOV result.pos, vertex.pos; */ - ureg_MOV(ureg, - ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ), - ureg_DECL_vs_input( ureg, 0 )); - - /* MOV result.texcoord0, vertex.attr[1]; */ - ureg_MOV(ureg, - ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ), - ureg_DECL_vs_input( ureg, 1 )); - - if (passColor) { - /* MOV result.color0, vertex.attr[2]; */ - ureg_MOV(ureg, - ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ), - ureg_DECL_vs_input( ureg, 2 )); - } - - ureg_END( ureg ); - - st->drawpix.vert_shaders[passColor] = - ureg_create_shader_and_destroy( ureg, st->pipe ); - } - - return st->drawpix.vert_shaders[passColor]; -} - - -/** - * Return a texture base format for drawing/copying an image - * of the given format. - */ -static GLenum -base_format(GLenum format) -{ - switch (format) { - case GL_DEPTH_COMPONENT: - return GL_DEPTH_COMPONENT; - case GL_DEPTH_STENCIL: - return GL_DEPTH_STENCIL; - case GL_STENCIL_INDEX: - return GL_STENCIL_INDEX; - default: - return GL_RGBA; - } -} - - -/** - * Return a texture internalFormat for drawing/copying an image - * of the given format and type. - */ -static GLenum -internal_format(GLenum format, GLenum type) -{ - switch (format) { - case GL_DEPTH_COMPONENT: - return GL_DEPTH_COMPONENT; - case GL_DEPTH_STENCIL: - return GL_DEPTH_STENCIL; - case GL_STENCIL_INDEX: - return GL_STENCIL_INDEX; - default: - if (_mesa_is_integer_format(format)) { - switch (type) { - case GL_BYTE: - return GL_RGBA8I; - case GL_UNSIGNED_BYTE: - return GL_RGBA8UI; - case GL_SHORT: - return GL_RGBA16I; - case GL_UNSIGNED_SHORT: - return GL_RGBA16UI; - case GL_INT: - return GL_RGBA32I; - case GL_UNSIGNED_INT: - return GL_RGBA32UI; - default: - assert(0 && "Unexpected type in internal_format()"); - return GL_RGBA_INTEGER; - } - } - else { - return GL_RGBA; - } - } -} - - -/** - * Create a temporary texture to hold an image of the given size. - * If width, height are not POT and the driver only handles POT textures, - * allocate the next larger size of texture that is POT. - */ -static struct pipe_resource * -alloc_texture(struct st_context *st, GLsizei width, GLsizei height, - enum pipe_format texFormat) -{ - struct pipe_resource *pt; - - pt = st_texture_create(st, st->internal_target, texFormat, 0, - width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW); - - return pt; -} - - -/** - * Make texture containing an image for glDrawPixels image. - * If 'pixels' is NULL, leave the texture image data undefined. - */ -static struct pipe_resource * -make_texture(struct st_context *st, - GLsizei width, GLsizei height, GLenum format, GLenum type, - const struct gl_pixelstore_attrib *unpack, - const GLvoid *pixels) -{ - struct gl_context *ctx = st->ctx; - struct pipe_context *pipe = st->pipe; - gl_format mformat; - struct pipe_resource *pt; - enum pipe_format pipeFormat; - GLuint cpp; - GLenum baseFormat, intFormat; - - baseFormat = base_format(format); - intFormat = internal_format(format, type); - - mformat = st_ChooseTextureFormat_renderable(ctx, intFormat, - format, type, GL_FALSE); - assert(mformat); - - pipeFormat = st_mesa_format_to_pipe_format(mformat); - assert(pipeFormat); - cpp = util_format_get_blocksize(pipeFormat); - - pixels = _mesa_map_pbo_source(ctx, unpack, pixels); - if (!pixels) - return NULL; - - /* alloc temporary texture */ - pt = alloc_texture(st, width, height, pipeFormat); - if (!pt) { - _mesa_unmap_pbo_source(ctx, unpack); - return NULL; - } - - { - struct pipe_transfer *transfer; - static const GLuint dstImageOffsets = 0; - GLboolean success; - GLubyte *dest; - const GLbitfield imageTransferStateSave = ctx->_ImageTransferState; - - /* we'll do pixel transfer in a fragment shader */ - ctx->_ImageTransferState = 0x0; - - transfer = pipe_get_transfer(st->pipe, pt, 0, 0, - PIPE_TRANSFER_WRITE, 0, 0, - width, height); - - /* map texture transfer */ - dest = pipe_transfer_map(pipe, transfer); - - - /* Put image into texture transfer. - * Note that the image is actually going to be upside down in - * the texture. We deal with that with texcoords. - */ - success = _mesa_texstore(ctx, 2, /* dims */ - baseFormat, /* baseInternalFormat */ - mformat, /* gl_format */ - dest, /* dest */ - 0, 0, 0, /* dstX/Y/Zoffset */ - transfer->stride, /* dstRowStride, bytes */ - &dstImageOffsets, /* dstImageOffsets */ - width, height, 1, /* size */ - format, type, /* src format/type */ - pixels, /* data source */ - unpack); - - /* unmap */ - pipe_transfer_unmap(pipe, transfer); - pipe->transfer_destroy(pipe, transfer); - - assert(success); - - /* restore */ - ctx->_ImageTransferState = imageTransferStateSave; - } - - _mesa_unmap_pbo_source(ctx, unpack); - - return pt; -} - - -/** - * Draw quad with texcoords and optional color. - * Coords are gallium window coords with y=0=top. - * \param color may be null - * \param invertTex if true, flip texcoords vertically - */ -static void -draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z, - GLfloat x1, GLfloat y1, const GLfloat *color, - GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */ - - /* setup vertex data */ - { - const struct gl_framebuffer *fb = st->ctx->DrawBuffer; - const GLfloat fb_width = (GLfloat) fb->Width; - const GLfloat fb_height = (GLfloat) fb->Height; - const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f; - const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f; - const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f; - const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f; - const GLfloat sLeft = 0.0f, sRight = maxXcoord; - const GLfloat tTop = invertTex ? maxYcoord : 0.0f; - const GLfloat tBot = invertTex ? 0.0f : maxYcoord; - GLuint i; - - /* upper-left */ - verts[0][0][0] = clip_x0; /* v[0].attr[0].x */ - verts[0][0][1] = clip_y0; /* v[0].attr[0].y */ - - /* upper-right */ - verts[1][0][0] = clip_x1; - verts[1][0][1] = clip_y0; - - /* lower-right */ - verts[2][0][0] = clip_x1; - verts[2][0][1] = clip_y1; - - /* lower-left */ - verts[3][0][0] = clip_x0; - verts[3][0][1] = clip_y1; - - verts[0][1][0] = sLeft; /* v[0].attr[1].S */ - verts[0][1][1] = tTop; /* v[0].attr[1].T */ - verts[1][1][0] = sRight; - verts[1][1][1] = tTop; - verts[2][1][0] = sRight; - verts[2][1][1] = tBot; - verts[3][1][0] = sLeft; - verts[3][1][1] = tBot; - - /* same for all verts: */ - if (color) { - for (i = 0; i < 4; i++) { - verts[i][0][2] = z; /* v[i].attr[0].z */ - verts[i][0][3] = 1.0f; /* v[i].attr[0].w */ - verts[i][2][0] = color[0]; /* v[i].attr[2].r */ - verts[i][2][1] = color[1]; /* v[i].attr[2].g */ - verts[i][2][2] = color[2]; /* v[i].attr[2].b */ - verts[i][2][3] = color[3]; /* v[i].attr[2].a */ - verts[i][1][2] = 0.0f; /* v[i].attr[1].R */ - verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */ - } - } - else { - for (i = 0; i < 4; i++) { - verts[i][0][2] = z; /*Z*/ - verts[i][0][3] = 1.0f; /*W*/ - verts[i][1][2] = 0.0f; /*R*/ - verts[i][1][3] = 1.0f; /*Q*/ - } - } - } - - { - struct pipe_resource *buf; - - /* allocate/load buffer object with vertex data */ - buf = pipe_buffer_create(pipe->screen, - PIPE_BIND_VERTEX_BUFFER, - PIPE_USAGE_STATIC, - sizeof(verts)); - pipe_buffer_write(st->pipe, buf, 0, sizeof(verts), verts); - - util_draw_vertex_buffer(pipe, st->cso_context, buf, 0, - PIPE_PRIM_QUADS, - 4, /* verts */ - 3); /* attribs/vert */ - pipe_resource_reference(&buf, NULL); - } -} - - - -static void -draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z, - GLsizei width, GLsizei height, - GLfloat zoomX, GLfloat zoomY, - struct pipe_sampler_view **sv, - int num_sampler_view, - void *driver_vp, - void *driver_fp, - const GLfloat *color, - GLboolean invertTex, - GLboolean write_depth, GLboolean write_stencil) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - struct cso_context *cso = st->cso_context; - GLfloat x0, y0, x1, y1; - GLsizei maxSize; - boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT; - - /* limit checks */ - /* XXX if DrawPixels image is larger than max texture size, break - * it up into chunks. - */ - maxSize = 1 << (pipe->screen->get_param(pipe->screen, - PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1); - assert(width <= maxSize); - assert(height <= maxSize); - - cso_save_rasterizer(cso); - cso_save_viewport(cso); - cso_save_samplers(cso); - cso_save_fragment_sampler_views(cso); - cso_save_fragment_shader(cso); - cso_save_vertex_shader(cso); - cso_save_vertex_elements(cso); - cso_save_vertex_buffers(cso); - if (write_stencil) { - cso_save_depth_stencil_alpha(cso); - cso_save_blend(cso); - } - - /* rasterizer state: just scissor */ - { - struct pipe_rasterizer_state rasterizer; - memset(&rasterizer, 0, sizeof(rasterizer)); - rasterizer.gl_rasterization_rules = 1; - rasterizer.scissor = ctx->Scissor.Enabled; - cso_set_rasterizer(cso, &rasterizer); - } - - if (write_stencil) { - /* Stencil writing bypasses the normal fragment pipeline to - * disable color writing and set stencil test to always pass. - */ - struct pipe_depth_stencil_alpha_state dsa; - struct pipe_blend_state blend; - - /* depth/stencil */ - memset(&dsa, 0, sizeof(dsa)); - dsa.stencil[0].enabled = 1; - dsa.stencil[0].func = PIPE_FUNC_ALWAYS; - dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff; - dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE; - if (write_depth) { - /* writing depth+stencil: depth test always passes */ - dsa.depth.enabled = 1; - dsa.depth.writemask = ctx->Depth.Mask; - dsa.depth.func = PIPE_FUNC_ALWAYS; - } - cso_set_depth_stencil_alpha(cso, &dsa); - - /* blend (colormask) */ - memset(&blend, 0, sizeof(blend)); - cso_set_blend(cso, &blend); - } - - /* fragment shader state: TEX lookup program */ - cso_set_fragment_shader_handle(cso, driver_fp); - - /* vertex shader state: position + texcoord pass-through */ - cso_set_vertex_shader_handle(cso, driver_vp); - - - /* texture sampling state: */ - { - struct pipe_sampler_state sampler; - memset(&sampler, 0, sizeof(sampler)); - sampler.wrap_s = PIPE_TEX_WRAP_CLAMP; - sampler.wrap_t = PIPE_TEX_WRAP_CLAMP; - sampler.wrap_r = PIPE_TEX_WRAP_CLAMP; - sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; - sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; - sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; - sampler.normalized_coords = normalized; - - cso_single_sampler(cso, 0, &sampler); - if (num_sampler_view > 1) { - cso_single_sampler(cso, 1, &sampler); - } - cso_single_sampler_done(cso); - } - - /* viewport state: viewport matching window dims */ - { - const float w = (float) ctx->DrawBuffer->Width; - const float h = (float) ctx->DrawBuffer->Height; - struct pipe_viewport_state vp; - vp.scale[0] = 0.5f * w; - vp.scale[1] = -0.5f * h; - vp.scale[2] = 0.5f; - vp.scale[3] = 1.0f; - vp.translate[0] = 0.5f * w; - vp.translate[1] = 0.5f * h; - vp.translate[2] = 0.5f; - vp.translate[3] = 0.0f; - cso_set_viewport(cso, &vp); - } - - cso_set_vertex_elements(cso, 3, st->velems_util_draw); - - /* texture state: */ - cso_set_fragment_sampler_views(cso, num_sampler_view, sv); - - /* Compute Gallium window coords (y=0=top) with pixel zoom. - * Recall that these coords are transformed by the current - * vertex shader and viewport transformation. - */ - if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) { - y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY); - invertTex = !invertTex; - } - - x0 = (GLfloat) x; - x1 = x + width * ctx->Pixel.ZoomX; - y0 = (GLfloat) y; - y1 = y + height * ctx->Pixel.ZoomY; - - /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */ - z = z * 2.0 - 1.0; - - draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex, - normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width, - normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height); - - /* restore state */ - cso_restore_rasterizer(cso); - cso_restore_viewport(cso); - cso_restore_samplers(cso); - cso_restore_fragment_sampler_views(cso); - cso_restore_fragment_shader(cso); - cso_restore_vertex_shader(cso); - cso_restore_vertex_elements(cso); - cso_restore_vertex_buffers(cso); - if (write_stencil) { - cso_restore_depth_stencil_alpha(cso); - cso_restore_blend(cso); - } -} - - -/** - * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we - * can't use a fragment shader to write stencil values. - */ -static void -draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y, - GLsizei width, GLsizei height, GLenum format, GLenum type, - const struct gl_pixelstore_attrib *unpack, - const GLvoid *pixels) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - struct st_renderbuffer *strb; - enum pipe_transfer_usage usage; - struct pipe_transfer *pt; - const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0; - GLint skipPixels; - ubyte *stmap; - struct gl_pixelstore_attrib clippedUnpack = *unpack; - - if (!zoom) { - if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height, - &clippedUnpack)) { - /* totally clipped */ - return; - } - } - - strb = st_renderbuffer(ctx->DrawBuffer-> - Attachment[BUFFER_STENCIL].Renderbuffer); - - if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { - y = ctx->DrawBuffer->Height - y - height; - } - - if(format != GL_DEPTH_STENCIL && - util_format_get_component_bits(strb->format, - UTIL_FORMAT_COLORSPACE_ZS, 0) != 0) - usage = PIPE_TRANSFER_READ_WRITE; - else - usage = PIPE_TRANSFER_WRITE; - - pt = pipe_get_transfer(st_context(ctx)->pipe, strb->texture, 0, 0, - usage, x, y, - width, height); - - stmap = pipe_transfer_map(pipe, pt); - - pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels); - assert(pixels); - - /* if width > MAX_WIDTH, have to process image in chunks */ - skipPixels = 0; - while (skipPixels < width) { - const GLint spanX = skipPixels; - const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH); - GLint row; - for (row = 0; row < height; row++) { - GLubyte sValues[MAX_WIDTH]; - GLuint zValues[MAX_WIDTH]; - GLenum destType = GL_UNSIGNED_BYTE; - const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels, - width, height, - format, type, - row, skipPixels); - _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues, - type, source, &clippedUnpack, - ctx->_ImageTransferState); - - if (format == GL_DEPTH_STENCIL) { - _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues, - (1 << 24) - 1, type, source, - &clippedUnpack); - } - - if (zoom) { - _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with " - "zoom not complete"); - } - - { - GLint spanY; - - if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { - spanY = height - row - 1; - } - else { - spanY = row; - } - - /* now pack the stencil (and Z) values in the dest format */ - switch (pt->resource->format) { - case PIPE_FORMAT_S8_USCALED: - { - ubyte *dest = stmap + spanY * pt->stride + spanX; - assert(usage == PIPE_TRANSFER_WRITE); - memcpy(dest, sValues, spanWidth); - } - break; - case PIPE_FORMAT_Z24_UNORM_S8_USCALED: - if (format == GL_DEPTH_STENCIL) { - uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4); - GLint k; - assert(usage == PIPE_TRANSFER_WRITE); - for (k = 0; k < spanWidth; k++) { - dest[k] = zValues[k] | (sValues[k] << 24); - } - } - else { - uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4); - GLint k; - assert(usage == PIPE_TRANSFER_READ_WRITE); - for (k = 0; k < spanWidth; k++) { - dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24); - } - } - break; - case PIPE_FORMAT_S8_USCALED_Z24_UNORM: - if (format == GL_DEPTH_STENCIL) { - uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4); - GLint k; - assert(usage == PIPE_TRANSFER_WRITE); - for (k = 0; k < spanWidth; k++) { - dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff); - } - } - else { - uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4); - GLint k; - assert(usage == PIPE_TRANSFER_READ_WRITE); - for (k = 0; k < spanWidth; k++) { - dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff); - } - } - break; - default: - assert(0); - } - } - } - skipPixels += spanWidth; - } - - _mesa_unmap_pbo_source(ctx, &clippedUnpack); - - /* unmap the stencil buffer */ - pipe_transfer_unmap(pipe, pt); - pipe->transfer_destroy(pipe, pt); -} - - -/** - * Get fragment program variant for a glDrawPixels or glCopyPixels - * command for RGBA data. - */ -static struct st_fp_variant * -get_color_fp_variant(struct st_context *st) -{ - struct gl_context *ctx = st->ctx; - struct st_fp_variant_key key; - struct st_fp_variant *fpv; - - memset(&key, 0, sizeof(key)); - - key.st = st; - key.drawpixels = 1; - key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 || - ctx->Pixel.RedScale != 1.0 || - ctx->Pixel.GreenBias != 0.0 || - ctx->Pixel.GreenScale != 1.0 || - ctx->Pixel.BlueBias != 0.0 || - ctx->Pixel.BlueScale != 1.0 || - ctx->Pixel.AlphaBias != 0.0 || - ctx->Pixel.AlphaScale != 1.0); - key.pixelMaps = ctx->Pixel.MapColorFlag; - - fpv = st_get_fp_variant(st, st->fp, &key); - - return fpv; -} - - -/** - * Get fragment program variant for a glDrawPixels or glCopyPixels - * command for depth/stencil data. - */ -static struct st_fp_variant * -get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth, - GLboolean write_stencil) -{ - struct st_fp_variant_key key; - struct st_fp_variant *fpv; - - memset(&key, 0, sizeof(key)); - - key.st = st; - key.drawpixels = 1; - key.drawpixels_z = write_depth; - key.drawpixels_stencil = write_stencil; - - fpv = st_get_fp_variant(st, st->fp, &key); - - return fpv; -} - - -/** - * Called via ctx->Driver.DrawPixels() - */ -static void -st_DrawPixels(struct gl_context *ctx, GLint x, GLint y, - GLsizei width, GLsizei height, - GLenum format, GLenum type, - const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels) -{ - void *driver_vp, *driver_fp; - struct st_context *st = st_context(ctx); - const GLfloat *color; - struct pipe_context *pipe = st->pipe; - GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE; - struct pipe_sampler_view *sv[2]; - int num_sampler_view = 1; - enum pipe_format stencil_format = PIPE_FORMAT_NONE; - struct st_fp_variant *fpv; - - if (format == GL_DEPTH_STENCIL) - write_stencil = write_depth = GL_TRUE; - else if (format == GL_STENCIL_INDEX) - write_stencil = GL_TRUE; - else if (format == GL_DEPTH_COMPONENT) - write_depth = GL_TRUE; - - if (write_stencil) { - enum pipe_format tex_format; - /* can we write to stencil if not fallback */ - if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT)) - goto stencil_fallback; - - tex_format = st_choose_format(st->pipe->screen, base_format(format), - PIPE_TEXTURE_2D, - 0, PIPE_BIND_SAMPLER_VIEW); - if (tex_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED) - stencil_format = PIPE_FORMAT_X24S8_USCALED; - else if (tex_format == PIPE_FORMAT_S8_USCALED_Z24_UNORM) - stencil_format = PIPE_FORMAT_S8X24_USCALED; - else - stencil_format = PIPE_FORMAT_S8_USCALED; - if (stencil_format == PIPE_FORMAT_NONE) - goto stencil_fallback; - } - - /* Mesa state should be up to date by now */ - assert(ctx->NewState == 0x0); - - st_validate_state(st); - - /* - * Get vertex/fragment shaders - */ - if (write_depth || write_stencil) { - fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil); - - driver_fp = fpv->driver_shader; - - driver_vp = make_passthrough_vertex_shader(st, GL_TRUE); - - color = ctx->Current.RasterColor; - } - else { - fpv = get_color_fp_variant(st); - - driver_fp = fpv->driver_shader; - - driver_vp = make_passthrough_vertex_shader(st, GL_FALSE); - - color = NULL; - if (st->pixel_xfer.pixelmap_enabled) { - sv[1] = st->pixel_xfer.pixelmap_sampler_view; - num_sampler_view++; - } - } - - /* update fragment program constants */ - st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT); - - /* draw with textured quad */ - { - struct pipe_resource *pt - = make_texture(st, width, height, format, type, unpack, pixels); - if (pt) { - sv[0] = st_create_texture_sampler_view(st->pipe, pt); - - if (sv[0]) { - if (write_stencil) { - sv[1] = st_create_texture_sampler_view_format(st->pipe, pt, - stencil_format); - num_sampler_view++; - } - - draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2], - width, height, - ctx->Pixel.ZoomX, ctx->Pixel.ZoomY, - sv, - num_sampler_view, - driver_vp, - driver_fp, - color, GL_FALSE, write_depth, write_stencil); - pipe_sampler_view_reference(&sv[0], NULL); - if (num_sampler_view > 1) - pipe_sampler_view_reference(&sv[1], NULL); - } - pipe_resource_reference(&pt, NULL); - } - } - return; - -stencil_fallback: - draw_stencil_pixels(ctx, x, y, width, height, format, type, - unpack, pixels); -} - - - -/** - * Software fallback for glCopyPixels(GL_STENCIL). - */ -static void -copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy, - GLsizei width, GLsizei height, - GLint dstx, GLint dsty) -{ - struct st_renderbuffer *rbDraw; - struct pipe_context *pipe = st_context(ctx)->pipe; - enum pipe_transfer_usage usage; - struct pipe_transfer *ptDraw; - ubyte *drawMap; - ubyte *buffer; - int i; - - buffer = malloc(width * height * sizeof(ubyte)); - if (!buffer) { - _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)"); - return; - } - - /* Get the dest renderbuffer. If there's a wrapper, use the - * underlying renderbuffer. - */ - rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer); - if (rbDraw->Base.Wrapped) - rbDraw = st_renderbuffer(rbDraw->Base.Wrapped); - - /* this will do stencil pixel transfer ops */ - st_read_stencil_pixels(ctx, srcx, srcy, width, height, - GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, - &ctx->DefaultPacking, buffer); - - if (0) { - /* debug code: dump stencil values */ - GLint row, col; - for (row = 0; row < height; row++) { - printf("%3d: ", row); - for (col = 0; col < width; col++) { - printf("%02x ", buffer[col + row * width]); - } - printf("\n"); - } - } - - if (util_format_get_component_bits(rbDraw->format, - UTIL_FORMAT_COLORSPACE_ZS, 0) != 0) - usage = PIPE_TRANSFER_READ_WRITE; - else - usage = PIPE_TRANSFER_WRITE; - - if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { - dsty = rbDraw->Base.Height - dsty - height; - } - - ptDraw = pipe_get_transfer(st_context(ctx)->pipe, - rbDraw->texture, 0, 0, - usage, dstx, dsty, - width, height); - - assert(util_format_get_blockwidth(ptDraw->resource->format) == 1); - assert(util_format_get_blockheight(ptDraw->resource->format) == 1); - - /* map the stencil buffer */ - drawMap = pipe_transfer_map(pipe, ptDraw); - - /* draw */ - /* XXX PixelZoom not handled yet */ - for (i = 0; i < height; i++) { - ubyte *dst; - const ubyte *src; - int y; - - y = i; - - if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { - y = height - y - 1; - } - - dst = drawMap + y * ptDraw->stride; - src = buffer + i * width; - - switch (ptDraw->resource->format) { - case PIPE_FORMAT_Z24_UNORM_S8_USCALED: - { - uint *dst4 = (uint *) dst; - int j; - assert(usage == PIPE_TRANSFER_READ_WRITE); - for (j = 0; j < width; j++) { - *dst4 = (*dst4 & 0xffffff) | (src[j] << 24); - dst4++; - } - } - break; - case PIPE_FORMAT_S8_USCALED_Z24_UNORM: - { - uint *dst4 = (uint *) dst; - int j; - assert(usage == PIPE_TRANSFER_READ_WRITE); - for (j = 0; j < width; j++) { - *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff); - dst4++; - } - } - break; - case PIPE_FORMAT_S8_USCALED: - assert(usage == PIPE_TRANSFER_WRITE); - memcpy(dst, src, width); - break; - default: - assert(0); - } - } - - free(buffer); - - /* unmap the stencil buffer */ - pipe_transfer_unmap(pipe, ptDraw); - pipe->transfer_destroy(pipe, ptDraw); -} - - -/** Do the src/dest regions overlap? */ -static GLboolean -regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY, - GLsizei width, GLsizei height) -{ - if (srcX + width <= dstX || - dstX + width <= srcX || - srcY + height <= dstY || - dstY + height <= srcY) - return GL_FALSE; - else - return GL_TRUE; -} - - -/** - * Try to do a glCopyPixels for simple cases with a blit by calling - * pipe->resource_copy_region(). - * - * We can do this when we're copying color pixels (depth/stencil - * eventually) with no pixel zoom, no pixel transfer ops, no - * per-fragment ops, the src/dest regions don't overlap and the - * src/dest pixel formats are the same. - */ -static GLboolean -blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy, - GLsizei width, GLsizei height, - GLint dstx, GLint dsty, GLenum type) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - struct gl_pixelstore_attrib pack, unpack; - GLint readX, readY, readW, readH; - - if (type == GL_COLOR && - ctx->Pixel.ZoomX == 1.0 && - ctx->Pixel.ZoomY == 1.0 && - ctx->_ImageTransferState == 0x0 && - !ctx->Color.BlendEnabled && - !ctx->Color.AlphaEnabled && - !ctx->Depth.Test && - !ctx->Fog.Enabled && - !ctx->Stencil.Enabled && - !ctx->FragmentProgram.Enabled && - !ctx->VertexProgram.Enabled && - !ctx->Shader.CurrentFragmentProgram && - st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) && - ctx->DrawBuffer->_NumColorDrawBuffers == 1) { - struct st_renderbuffer *rbRead, *rbDraw; - GLint drawX, drawY; - - /* - * Clip the read region against the src buffer bounds. - * We'll still allocate a temporary buffer/texture for the original - * src region size but we'll only read the region which is on-screen. - * This may mean that we draw garbage pixels into the dest region, but - * that's expected. - */ - readX = srcx; - readY = srcy; - readW = width; - readH = height; - pack = ctx->DefaultPacking; - if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack)) - return GL_TRUE; /* all done */ - - /* clip against dest buffer bounds and scissor box */ - drawX = dstx + pack.SkipPixels; - drawY = dsty + pack.SkipRows; - unpack = pack; - if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack)) - return GL_TRUE; /* all done */ - - readX = readX - pack.SkipPixels + unpack.SkipPixels; - readY = readY - pack.SkipRows + unpack.SkipRows; - - rbRead = st_get_color_read_renderbuffer(ctx); - rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]); - - if ((rbRead != rbDraw || - !regions_overlap(readX, readY, drawX, drawY, readW, readH)) && - rbRead->Base.Format == rbDraw->Base.Format) { - struct pipe_box srcBox; - - /* flip src/dst position if needed */ - if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { - /* both buffers will have the same orientation */ - readY = ctx->ReadBuffer->Height - readY - readH; - drawY = ctx->DrawBuffer->Height - drawY - readH; - } - - u_box_2d(readX, readY, readW, readH, &srcBox); - - pipe->resource_copy_region(pipe, - rbDraw->texture, 0, drawX, drawY, 0, - rbRead->texture, 0, &srcBox); - return GL_TRUE; - } - } - - return GL_FALSE; -} - - -static void -st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy, - GLsizei width, GLsizei height, - GLint dstx, GLint dsty, GLenum type) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - struct pipe_screen *screen = pipe->screen; - struct st_renderbuffer *rbRead; - void *driver_vp, *driver_fp; - struct pipe_resource *pt; - struct pipe_sampler_view *sv[2]; - int num_sampler_view = 1; - GLfloat *color; - enum pipe_format srcFormat, texFormat; - GLboolean invertTex = GL_FALSE; - GLint readX, readY, readW, readH; - GLuint sample_count; - struct gl_pixelstore_attrib pack = ctx->DefaultPacking; - struct st_fp_variant *fpv; - - st_validate_state(st); - - if (type == GL_STENCIL) { - /* can't use texturing to do stencil */ - copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty); - return; - } - - if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type)) - return; - - /* - * The subsequent code implements glCopyPixels by copying the source - * pixels into a temporary texture that's then applied to a textured quad. - * When we draw the textured quad, all the usual per-fragment operations - * are handled. - */ - - - /* - * Get vertex/fragment shaders - */ - if (type == GL_COLOR) { - rbRead = st_get_color_read_renderbuffer(ctx); - color = NULL; - - fpv = get_color_fp_variant(st); - driver_fp = fpv->driver_shader; - - driver_vp = make_passthrough_vertex_shader(st, GL_FALSE); - - if (st->pixel_xfer.pixelmap_enabled) { - sv[1] = st->pixel_xfer.pixelmap_sampler_view; - num_sampler_view++; - } - } - else { - assert(type == GL_DEPTH); - rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer); - color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0]; - - fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE); - driver_fp = fpv->driver_shader; - - driver_vp = make_passthrough_vertex_shader(st, GL_TRUE); - } - - /* update fragment program constants */ - st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT); - - - if (rbRead->Base.Wrapped) - rbRead = st_renderbuffer(rbRead->Base.Wrapped); - - sample_count = rbRead->texture->nr_samples; - /* I believe this would be legal, presumably would need to do a resolve - for color, and for depth/stencil spec says to just use one of the - depth/stencil samples per pixel? Need some transfer clarifications. */ - assert(sample_count < 2); - - srcFormat = rbRead->texture->format; - - if (screen->is_format_supported(screen, srcFormat, st->internal_target, - sample_count, - PIPE_BIND_SAMPLER_VIEW)) { - texFormat = srcFormat; - } - else { - /* srcFormat can't be used as a texture format */ - if (type == GL_DEPTH) { - texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT, - st->internal_target, sample_count, - PIPE_BIND_DEPTH_STENCIL); - assert(texFormat != PIPE_FORMAT_NONE); - } - else { - /* default color format */ - texFormat = st_choose_format(screen, GL_RGBA, st->internal_target, - sample_count, PIPE_BIND_SAMPLER_VIEW); - assert(texFormat != PIPE_FORMAT_NONE); - } - } - - /* Invert src region if needed */ - if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { - srcy = ctx->ReadBuffer->Height - srcy - height; - invertTex = !invertTex; - } - - /* Clip the read region against the src buffer bounds. - * We'll still allocate a temporary buffer/texture for the original - * src region size but we'll only read the region which is on-screen. - * This may mean that we draw garbage pixels into the dest region, but - * that's expected. - */ - readX = srcx; - readY = srcy; - readW = width; - readH = height; - _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack); - readW = MAX2(0, readW); - readH = MAX2(0, readH); - - /* alloc temporary texture */ - pt = alloc_texture(st, width, height, texFormat); - if (!pt) - return; - - sv[0] = st_create_texture_sampler_view(st->pipe, pt); - if (!sv[0]) { - pipe_resource_reference(&pt, NULL); - return; - } - - /* Make temporary texture which is a copy of the src region. - */ - if (srcFormat == texFormat) { - struct pipe_box src_box; - u_box_2d(readX, readY, readW, readH, &src_box); - /* copy source framebuffer surface into mipmap/texture */ - pipe->resource_copy_region(pipe, - pt, /* dest tex */ - 0, - pack.SkipPixels, pack.SkipRows, 0, /* dest pos */ - rbRead->texture, /* src tex */ - 0, - &src_box); - - } - else { - /* CPU-based fallback/conversion */ - struct pipe_transfer *ptRead = - pipe_get_transfer(st->pipe, rbRead->texture, - 0, 0, /* level, layer */ - PIPE_TRANSFER_READ, - readX, readY, readW, readH); - struct pipe_transfer *ptTex; - enum pipe_transfer_usage transfer_usage; - - if (ST_DEBUG & DEBUG_FALLBACK) - debug_printf("%s: fallback processing\n", __FUNCTION__); - - if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format)) - transfer_usage = PIPE_TRANSFER_READ_WRITE; - else - transfer_usage = PIPE_TRANSFER_WRITE; - - ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage, - 0, 0, width, height); - - /* copy image from ptRead surface to ptTex surface */ - if (type == GL_COLOR) { - /* alternate path using get/put_tile() */ - GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat)); - enum pipe_format readFormat, drawFormat; - readFormat = util_format_linear(rbRead->texture->format); - drawFormat = util_format_linear(pt->format); - pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH, - readFormat, buf); - pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows, - readW, readH, drawFormat, buf); - free(buf); - } - else { - /* GL_DEPTH */ - GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint)); - pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf); - pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows, - readW, readH, buf); - free(buf); - } - - pipe->transfer_destroy(pipe, ptRead); - pipe->transfer_destroy(pipe, ptTex); - } - - /* OK, the texture 'pt' contains the src image/pixels. Now draw a - * textured quad with that texture. - */ - draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2], - width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY, - sv, - num_sampler_view, - driver_vp, - driver_fp, - color, invertTex, GL_FALSE, GL_FALSE); - - pipe_resource_reference(&pt, NULL); - pipe_sampler_view_reference(&sv[0], NULL); -} - - - -void st_init_drawpixels_functions(struct dd_function_table *functions) -{ - functions->DrawPixels = st_DrawPixels; - functions->CopyPixels = st_CopyPixels; -} - - -void -st_destroy_drawpix(struct st_context *st) -{ - GLuint i; - - for (i = 0; i < Elements(st->drawpix.shaders); i++) { - if (st->drawpix.shaders[i]) - _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL); - } - - st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL); - if (st->drawpix.vert_shaders[0]) - ureg_free_tokens(st->drawpix.vert_shaders[0]); - if (st->drawpix.vert_shaders[1]) - ureg_free_tokens(st->drawpix.vert_shaders[1]); -} - -#endif /* FEATURE_drawpix */ +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+ /*
+ * Authors:
+ * Brian Paul
+ */
+
+#include "main/imports.h"
+#include "main/image.h"
+#include "main/bufferobj.h"
+#include "main/macros.h"
+#include "main/mfeatures.h"
+#include "main/mtypes.h"
+#include "main/pack.h"
+#include "main/pbo.h"
+#include "main/texformat.h"
+#include "main/texstore.h"
+#include "program/program.h"
+#include "program/prog_print.h"
+#include "program/prog_instruction.h"
+
+#include "st_atom.h"
+#include "st_atom_constbuf.h"
+#include "st_cb_drawpixels.h"
+#include "st_cb_readpixels.h"
+#include "st_cb_fbo.h"
+#include "st_context.h"
+#include "st_debug.h"
+#include "st_format.h"
+#include "st_program.h"
+#include "st_texture.h"
+
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "tgsi/tgsi_ureg.h"
+#include "util/u_draw_quad.h"
+#include "util/u_format.h"
+#include "util/u_inlines.h"
+#include "util/u_math.h"
+#include "util/u_tile.h"
+#include "cso_cache/cso_context.h"
+
+
+#if FEATURE_drawpix
+
+/**
+ * Check if the given program is:
+ * 0: MOVE result.color, fragment.color;
+ * 1: END;
+ */
+static GLboolean
+is_passthrough_program(const struct gl_fragment_program *prog)
+{
+ if (prog->Base.NumInstructions == 2) {
+ const struct prog_instruction *inst = prog->Base.Instructions;
+ if (inst[0].Opcode == OPCODE_MOV &&
+ inst[1].Opcode == OPCODE_END &&
+ inst[0].DstReg.File == PROGRAM_OUTPUT &&
+ inst[0].DstReg.Index == FRAG_RESULT_COLOR &&
+ inst[0].DstReg.WriteMask == WRITEMASK_XYZW &&
+ inst[0].SrcReg[0].File == PROGRAM_INPUT &&
+ inst[0].SrcReg[0].Index == FRAG_ATTRIB_COL0 &&
+ inst[0].SrcReg[0].Swizzle == SWIZZLE_XYZW) {
+ return GL_TRUE;
+ }
+ }
+ return GL_FALSE;
+}
+
+
+
+/**
+ * Make fragment shader for glDraw/CopyPixels. This shader is made
+ * by combining the pixel transfer shader with the user-defined shader.
+ * \param fpIn the current/incoming fragment program
+ * \param fpOut returns the combined fragment program
+ */
+void
+st_make_drawpix_fragment_program(struct st_context *st,
+ struct gl_fragment_program *fpIn,
+ struct gl_fragment_program **fpOut)
+{
+ struct gl_program *newProg;
+
+ if (is_passthrough_program(fpIn)) {
+ newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx,
+ &st->pixel_xfer.program->Base);
+ }
+ else {
+#if 0
+ /* debug */
+ printf("Base program:\n");
+ _mesa_print_program(&fpIn->Base);
+ printf("DrawPix program:\n");
+ _mesa_print_program(&st->pixel_xfer.program->Base.Base);
+#endif
+ newProg = _mesa_combine_programs(st->ctx,
+ &st->pixel_xfer.program->Base.Base,
+ &fpIn->Base);
+ }
+
+#if 0
+ /* debug */
+ printf("Combined DrawPixels program:\n");
+ _mesa_print_program(newProg);
+ printf("InputsRead: 0x%x\n", newProg->InputsRead);
+ printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten);
+ _mesa_print_parameter_list(newProg->Parameters);
+#endif
+
+ *fpOut = (struct gl_fragment_program *) newProg;
+}
+
+
+/**
+ * Create fragment program that does a TEX() instruction to get a Z and/or
+ * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
+ * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
+ * Pass fragment color through as-is.
+ * \return pointer to the gl_fragment program
+ */
+struct gl_fragment_program *
+st_make_drawpix_z_stencil_program(struct st_context *st,
+ GLboolean write_depth,
+ GLboolean write_stencil)
+{
+ struct gl_context *ctx = st->ctx;
+ struct gl_program *p;
+ struct gl_fragment_program *fp;
+ GLuint ic = 0;
+ const GLuint shaderIndex = write_depth * 2 + write_stencil;
+
+ assert(shaderIndex < Elements(st->drawpix.shaders));
+
+ if (st->drawpix.shaders[shaderIndex]) {
+ /* already have the proper shader */
+ return st->drawpix.shaders[shaderIndex];
+ }
+
+ /*
+ * Create shader now
+ */
+ p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
+ if (!p)
+ return NULL;
+
+ p->NumInstructions = write_depth ? 2 : 1;
+ p->NumInstructions += write_stencil ? 1 : 0;
+
+ p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
+ if (!p->Instructions) {
+ ctx->Driver.DeleteProgram(ctx, p);
+ return NULL;
+ }
+ _mesa_init_instructions(p->Instructions, p->NumInstructions);
+
+ if (write_depth) {
+ /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
+ p->Instructions[ic].Opcode = OPCODE_TEX;
+ p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
+ p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
+ p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
+ p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
+ p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
+ p->Instructions[ic].TexSrcUnit = 0;
+ p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+ ic++;
+ }
+
+ if (write_stencil) {
+ /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
+ p->Instructions[ic].Opcode = OPCODE_TEX;
+ p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
+ p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL;
+ p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y;
+ p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
+ p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
+ p->Instructions[ic].TexSrcUnit = 1;
+ p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+ ic++;
+ }
+
+ /* END; */
+ p->Instructions[ic++].Opcode = OPCODE_END;
+
+ assert(ic == p->NumInstructions);
+
+ p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
+ p->OutputsWritten = 0;
+ if (write_depth)
+ p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_DEPTH);
+ if (write_stencil)
+ p->OutputsWritten |= BITFIELD64_BIT(FRAG_RESULT_STENCIL);
+
+ p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
+ if (write_stencil)
+ p->SamplersUsed |= 1 << 1;
+
+ fp = (struct gl_fragment_program *) p;
+
+ /* save the new shader */
+ st->drawpix.shaders[shaderIndex] = fp;
+
+ return fp;
+}
+
+
+/**
+ * Create a simple vertex shader that just passes through the
+ * vertex position and texcoord (and optionally, color).
+ */
+static void *
+make_passthrough_vertex_shader(struct st_context *st,
+ GLboolean passColor)
+{
+ if (!st->drawpix.vert_shaders[passColor]) {
+ struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
+
+ if (ureg == NULL)
+ return NULL;
+
+ /* MOV result.pos, vertex.pos; */
+ ureg_MOV(ureg,
+ ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
+ ureg_DECL_vs_input( ureg, 0 ));
+
+ /* MOV result.texcoord0, vertex.attr[1]; */
+ ureg_MOV(ureg,
+ ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ),
+ ureg_DECL_vs_input( ureg, 1 ));
+
+ if (passColor) {
+ /* MOV result.color0, vertex.attr[2]; */
+ ureg_MOV(ureg,
+ ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
+ ureg_DECL_vs_input( ureg, 2 ));
+ }
+
+ ureg_END( ureg );
+
+ st->drawpix.vert_shaders[passColor] =
+ ureg_create_shader_and_destroy( ureg, st->pipe );
+ }
+
+ return st->drawpix.vert_shaders[passColor];
+}
+
+
+/**
+ * Return a texture base format for drawing/copying an image
+ * of the given format.
+ */
+static GLenum
+base_format(GLenum format)
+{
+ switch (format) {
+ case GL_DEPTH_COMPONENT:
+ return GL_DEPTH_COMPONENT;
+ case GL_DEPTH_STENCIL:
+ return GL_DEPTH_STENCIL;
+ case GL_STENCIL_INDEX:
+ return GL_STENCIL_INDEX;
+ default:
+ return GL_RGBA;
+ }
+}
+
+
+/**
+ * Return a texture internalFormat for drawing/copying an image
+ * of the given format and type.
+ */
+static GLenum
+internal_format(GLenum format, GLenum type)
+{
+ switch (format) {
+ case GL_DEPTH_COMPONENT:
+ return GL_DEPTH_COMPONENT;
+ case GL_DEPTH_STENCIL:
+ return GL_DEPTH_STENCIL;
+ case GL_STENCIL_INDEX:
+ return GL_STENCIL_INDEX;
+ default:
+ if (_mesa_is_integer_format(format)) {
+ switch (type) {
+ case GL_BYTE:
+ return GL_RGBA8I;
+ case GL_UNSIGNED_BYTE:
+ return GL_RGBA8UI;
+ case GL_SHORT:
+ return GL_RGBA16I;
+ case GL_UNSIGNED_SHORT:
+ return GL_RGBA16UI;
+ case GL_INT:
+ return GL_RGBA32I;
+ case GL_UNSIGNED_INT:
+ return GL_RGBA32UI;
+ default:
+ assert(0 && "Unexpected type in internal_format()");
+ return GL_RGBA_INTEGER;
+ }
+ }
+ else {
+ return GL_RGBA;
+ }
+ }
+}
+
+
+/**
+ * Create a temporary texture to hold an image of the given size.
+ * If width, height are not POT and the driver only handles POT textures,
+ * allocate the next larger size of texture that is POT.
+ */
+static struct pipe_resource *
+alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
+ enum pipe_format texFormat)
+{
+ struct pipe_resource *pt;
+
+ pt = st_texture_create(st, st->internal_target, texFormat, 0,
+ width, height, 1, 1, PIPE_BIND_SAMPLER_VIEW);
+
+ return pt;
+}
+
+
+/**
+ * Make texture containing an image for glDrawPixels image.
+ * If 'pixels' is NULL, leave the texture image data undefined.
+ */
+static struct pipe_resource *
+make_texture(struct st_context *st,
+ GLsizei width, GLsizei height, GLenum format, GLenum type,
+ const struct gl_pixelstore_attrib *unpack,
+ const GLvoid *pixels)
+{
+ struct gl_context *ctx = st->ctx;
+ struct pipe_context *pipe = st->pipe;
+ gl_format mformat;
+ struct pipe_resource *pt;
+ enum pipe_format pipeFormat;
+ GLuint cpp;
+ GLenum baseFormat, intFormat;
+
+ baseFormat = base_format(format);
+ intFormat = internal_format(format, type);
+
+ mformat = st_ChooseTextureFormat_renderable(ctx, intFormat,
+ format, type, GL_FALSE);
+ assert(mformat);
+
+ pipeFormat = st_mesa_format_to_pipe_format(mformat);
+ assert(pipeFormat);
+ cpp = util_format_get_blocksize(pipeFormat);
+
+ pixels = _mesa_map_pbo_source(ctx, unpack, pixels);
+ if (!pixels)
+ return NULL;
+
+ /* alloc temporary texture */
+ pt = alloc_texture(st, width, height, pipeFormat);
+ if (!pt) {
+ _mesa_unmap_pbo_source(ctx, unpack);
+ return NULL;
+ }
+
+ {
+ struct pipe_transfer *transfer;
+ static const GLuint dstImageOffsets = 0;
+ GLboolean success;
+ GLubyte *dest;
+ const GLbitfield imageTransferStateSave = ctx->_ImageTransferState;
+
+ /* we'll do pixel transfer in a fragment shader */
+ ctx->_ImageTransferState = 0x0;
+
+ transfer = pipe_get_transfer(st->pipe, pt, 0, 0,
+ PIPE_TRANSFER_WRITE, 0, 0,
+ width, height);
+
+ /* map texture transfer */
+ dest = pipe_transfer_map(pipe, transfer);
+
+
+ /* Put image into texture transfer.
+ * Note that the image is actually going to be upside down in
+ * the texture. We deal with that with texcoords.
+ */
+ success = _mesa_texstore(ctx, 2, /* dims */
+ baseFormat, /* baseInternalFormat */
+ mformat, /* gl_format */
+ dest, /* dest */
+ 0, 0, 0, /* dstX/Y/Zoffset */
+ transfer->stride, /* dstRowStride, bytes */
+ &dstImageOffsets, /* dstImageOffsets */
+ width, height, 1, /* size */
+ format, type, /* src format/type */
+ pixels, /* data source */
+ unpack);
+
+ /* unmap */
+ pipe_transfer_unmap(pipe, transfer);
+ pipe->transfer_destroy(pipe, transfer);
+
+ assert(success);
+
+ /* restore */
+ ctx->_ImageTransferState = imageTransferStateSave;
+ }
+
+ _mesa_unmap_pbo_source(ctx, unpack);
+
+ return pt;
+}
+
+
+/**
+ * Draw quad with texcoords and optional color.
+ * Coords are gallium window coords with y=0=top.
+ * \param color may be null
+ * \param invertTex if true, flip texcoords vertically
+ */
+static void
+draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
+ GLfloat x1, GLfloat y1, const GLfloat *color,
+ GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
+
+ /* setup vertex data */
+ {
+ const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
+ const GLfloat fb_width = (GLfloat) fb->Width;
+ const GLfloat fb_height = (GLfloat) fb->Height;
+ const GLfloat clip_x0 = x0 / fb_width * 2.0f - 1.0f;
+ const GLfloat clip_y0 = y0 / fb_height * 2.0f - 1.0f;
+ const GLfloat clip_x1 = x1 / fb_width * 2.0f - 1.0f;
+ const GLfloat clip_y1 = y1 / fb_height * 2.0f - 1.0f;
+ const GLfloat sLeft = 0.0f, sRight = maxXcoord;
+ const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
+ const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
+ GLuint i;
+
+ /* upper-left */
+ verts[0][0][0] = clip_x0; /* v[0].attr[0].x */
+ verts[0][0][1] = clip_y0; /* v[0].attr[0].y */
+
+ /* upper-right */
+ verts[1][0][0] = clip_x1;
+ verts[1][0][1] = clip_y0;
+
+ /* lower-right */
+ verts[2][0][0] = clip_x1;
+ verts[2][0][1] = clip_y1;
+
+ /* lower-left */
+ verts[3][0][0] = clip_x0;
+ verts[3][0][1] = clip_y1;
+
+ verts[0][1][0] = sLeft; /* v[0].attr[1].S */
+ verts[0][1][1] = tTop; /* v[0].attr[1].T */
+ verts[1][1][0] = sRight;
+ verts[1][1][1] = tTop;
+ verts[2][1][0] = sRight;
+ verts[2][1][1] = tBot;
+ verts[3][1][0] = sLeft;
+ verts[3][1][1] = tBot;
+
+ /* same for all verts: */
+ if (color) {
+ for (i = 0; i < 4; i++) {
+ verts[i][0][2] = z; /* v[i].attr[0].z */
+ verts[i][0][3] = 1.0f; /* v[i].attr[0].w */
+ verts[i][2][0] = color[0]; /* v[i].attr[2].r */
+ verts[i][2][1] = color[1]; /* v[i].attr[2].g */
+ verts[i][2][2] = color[2]; /* v[i].attr[2].b */
+ verts[i][2][3] = color[3]; /* v[i].attr[2].a */
+ verts[i][1][2] = 0.0f; /* v[i].attr[1].R */
+ verts[i][1][3] = 1.0f; /* v[i].attr[1].Q */
+ }
+ }
+ else {
+ for (i = 0; i < 4; i++) {
+ verts[i][0][2] = z; /*Z*/
+ verts[i][0][3] = 1.0f; /*W*/
+ verts[i][1][2] = 0.0f; /*R*/
+ verts[i][1][3] = 1.0f; /*Q*/
+ }
+ }
+ }
+
+ {
+ struct pipe_resource *buf;
+
+ /* allocate/load buffer object with vertex data */
+ buf = pipe_buffer_create(pipe->screen,
+ PIPE_BIND_VERTEX_BUFFER,
+ PIPE_USAGE_STATIC,
+ sizeof(verts));
+ pipe_buffer_write(st->pipe, buf, 0, sizeof(verts), verts);
+
+ util_draw_vertex_buffer(pipe, st->cso_context, buf, 0,
+ PIPE_PRIM_QUADS,
+ 4, /* verts */
+ 3); /* attribs/vert */
+ pipe_resource_reference(&buf, NULL);
+ }
+}
+
+
+
+static void
+draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
+ GLsizei width, GLsizei height,
+ GLfloat zoomX, GLfloat zoomY,
+ struct pipe_sampler_view **sv,
+ int num_sampler_view,
+ void *driver_vp,
+ void *driver_fp,
+ const GLfloat *color,
+ GLboolean invertTex,
+ GLboolean write_depth, GLboolean write_stencil)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct cso_context *cso = st->cso_context;
+ GLfloat x0, y0, x1, y1;
+ GLsizei maxSize;
+ boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
+
+ /* limit checks */
+ /* XXX if DrawPixels image is larger than max texture size, break
+ * it up into chunks.
+ */
+ maxSize = 1 << (pipe->screen->get_param(pipe->screen,
+ PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
+ assert(width <= maxSize);
+ assert(height <= maxSize);
+
+ cso_save_rasterizer(cso);
+ cso_save_viewport(cso);
+ cso_save_samplers(cso);
+ cso_save_fragment_sampler_views(cso);
+ cso_save_fragment_shader(cso);
+ cso_save_vertex_shader(cso);
+ cso_save_vertex_elements(cso);
+ cso_save_vertex_buffers(cso);
+ if (write_stencil) {
+ cso_save_depth_stencil_alpha(cso);
+ cso_save_blend(cso);
+ }
+
+ /* rasterizer state: just scissor */
+ {
+ struct pipe_rasterizer_state rasterizer;
+ memset(&rasterizer, 0, sizeof(rasterizer));
+ rasterizer.gl_rasterization_rules = 1;
+ rasterizer.scissor = ctx->Scissor.Enabled;
+ cso_set_rasterizer(cso, &rasterizer);
+ }
+
+ if (write_stencil) {
+ /* Stencil writing bypasses the normal fragment pipeline to
+ * disable color writing and set stencil test to always pass.
+ */
+ struct pipe_depth_stencil_alpha_state dsa;
+ struct pipe_blend_state blend;
+
+ /* depth/stencil */
+ memset(&dsa, 0, sizeof(dsa));
+ dsa.stencil[0].enabled = 1;
+ dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
+ dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
+ dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
+ if (write_depth) {
+ /* writing depth+stencil: depth test always passes */
+ dsa.depth.enabled = 1;
+ dsa.depth.writemask = ctx->Depth.Mask;
+ dsa.depth.func = PIPE_FUNC_ALWAYS;
+ }
+ cso_set_depth_stencil_alpha(cso, &dsa);
+
+ /* blend (colormask) */
+ memset(&blend, 0, sizeof(blend));
+ cso_set_blend(cso, &blend);
+ }
+
+ /* fragment shader state: TEX lookup program */
+ cso_set_fragment_shader_handle(cso, driver_fp);
+
+ /* vertex shader state: position + texcoord pass-through */
+ cso_set_vertex_shader_handle(cso, driver_vp);
+
+
+ /* texture sampling state: */
+ {
+ struct pipe_sampler_state sampler;
+ memset(&sampler, 0, sizeof(sampler));
+ sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
+ sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
+ sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
+ sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
+ sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
+ sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
+ sampler.normalized_coords = normalized;
+
+ cso_single_sampler(cso, 0, &sampler);
+ if (num_sampler_view > 1) {
+ cso_single_sampler(cso, 1, &sampler);
+ }
+ cso_single_sampler_done(cso);
+ }
+
+ /* viewport state: viewport matching window dims */
+ {
+ const float w = (float) ctx->DrawBuffer->Width;
+ const float h = (float) ctx->DrawBuffer->Height;
+ struct pipe_viewport_state vp;
+ vp.scale[0] = 0.5f * w;
+ vp.scale[1] = -0.5f * h;
+ vp.scale[2] = 0.5f;
+ vp.scale[3] = 1.0f;
+ vp.translate[0] = 0.5f * w;
+ vp.translate[1] = 0.5f * h;
+ vp.translate[2] = 0.5f;
+ vp.translate[3] = 0.0f;
+ cso_set_viewport(cso, &vp);
+ }
+
+ cso_set_vertex_elements(cso, 3, st->velems_util_draw);
+
+ /* texture state: */
+ cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
+
+ /* Compute Gallium window coords (y=0=top) with pixel zoom.
+ * Recall that these coords are transformed by the current
+ * vertex shader and viewport transformation.
+ */
+ if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
+ y = ctx->DrawBuffer->Height - (int) (y + height * ctx->Pixel.ZoomY);
+ invertTex = !invertTex;
+ }
+
+ x0 = (GLfloat) x;
+ x1 = x + width * ctx->Pixel.ZoomX;
+ y0 = (GLfloat) y;
+ y1 = y + height * ctx->Pixel.ZoomY;
+
+ /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
+ z = z * 2.0 - 1.0;
+
+ draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
+ normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
+ normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
+
+ /* restore state */
+ cso_restore_rasterizer(cso);
+ cso_restore_viewport(cso);
+ cso_restore_samplers(cso);
+ cso_restore_fragment_sampler_views(cso);
+ cso_restore_fragment_shader(cso);
+ cso_restore_vertex_shader(cso);
+ cso_restore_vertex_elements(cso);
+ cso_restore_vertex_buffers(cso);
+ if (write_stencil) {
+ cso_restore_depth_stencil_alpha(cso);
+ cso_restore_blend(cso);
+ }
+}
+
+
+/**
+ * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
+ * can't use a fragment shader to write stencil values.
+ */
+static void
+draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
+ GLsizei width, GLsizei height, GLenum format, GLenum type,
+ const struct gl_pixelstore_attrib *unpack,
+ const GLvoid *pixels)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct st_renderbuffer *strb;
+ enum pipe_transfer_usage usage;
+ struct pipe_transfer *pt;
+ const GLboolean zoom = ctx->Pixel.ZoomX != 1.0 || ctx->Pixel.ZoomY != 1.0;
+ GLint skipPixels;
+ ubyte *stmap;
+ struct gl_pixelstore_attrib clippedUnpack = *unpack;
+
+ if (!zoom) {
+ if (!_mesa_clip_drawpixels(ctx, &x, &y, &width, &height,
+ &clippedUnpack)) {
+ /* totally clipped */
+ return;
+ }
+ }
+
+ strb = st_renderbuffer(ctx->DrawBuffer->
+ Attachment[BUFFER_STENCIL].Renderbuffer);
+
+ if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
+ y = ctx->DrawBuffer->Height - y - height;
+ }
+
+ if(format != GL_DEPTH_STENCIL &&
+ util_format_get_component_bits(strb->format,
+ UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
+ usage = PIPE_TRANSFER_READ_WRITE;
+ else
+ usage = PIPE_TRANSFER_WRITE;
+
+ pt = pipe_get_transfer(st_context(ctx)->pipe, strb->texture, 0, 0,
+ usage, x, y,
+ width, height);
+
+ stmap = pipe_transfer_map(pipe, pt);
+
+ pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
+ assert(pixels);
+
+ /* if width > MAX_WIDTH, have to process image in chunks */
+ skipPixels = 0;
+ while (skipPixels < width) {
+ const GLint spanX = skipPixels;
+ const GLint spanWidth = MIN2(width - skipPixels, MAX_WIDTH);
+ GLint row;
+ for (row = 0; row < height; row++) {
+ GLubyte sValues[MAX_WIDTH];
+ GLuint zValues[MAX_WIDTH];
+ GLenum destType = GL_UNSIGNED_BYTE;
+ const GLvoid *source = _mesa_image_address2d(&clippedUnpack, pixels,
+ width, height,
+ format, type,
+ row, skipPixels);
+ _mesa_unpack_stencil_span(ctx, spanWidth, destType, sValues,
+ type, source, &clippedUnpack,
+ ctx->_ImageTransferState);
+
+ if (format == GL_DEPTH_STENCIL) {
+ _mesa_unpack_depth_span(ctx, spanWidth, GL_UNSIGNED_INT, zValues,
+ (1 << 24) - 1, type, source,
+ &clippedUnpack);
+ }
+
+ if (zoom) {
+ _mesa_problem(ctx, "Gallium glDrawPixels(GL_STENCIL) with "
+ "zoom not complete");
+ }
+
+ {
+ GLint spanY;
+
+ if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
+ spanY = height - row - 1;
+ }
+ else {
+ spanY = row;
+ }
+
+ /* now pack the stencil (and Z) values in the dest format */
+ switch (pt->resource->format) {
+ case PIPE_FORMAT_S8_USCALED:
+ {
+ ubyte *dest = stmap + spanY * pt->stride + spanX;
+ assert(usage == PIPE_TRANSFER_WRITE);
+ memcpy(dest, sValues, spanWidth);
+ }
+ break;
+ case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
+ if (format == GL_DEPTH_STENCIL) {
+ uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
+ GLint k;
+ assert(usage == PIPE_TRANSFER_WRITE);
+ for (k = 0; k < spanWidth; k++) {
+ dest[k] = zValues[k] | (sValues[k] << 24);
+ }
+ }
+ else {
+ uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
+ GLint k;
+ assert(usage == PIPE_TRANSFER_READ_WRITE);
+ for (k = 0; k < spanWidth; k++) {
+ dest[k] = (dest[k] & 0xffffff) | (sValues[k] << 24);
+ }
+ }
+ break;
+ case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
+ if (format == GL_DEPTH_STENCIL) {
+ uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
+ GLint k;
+ assert(usage == PIPE_TRANSFER_WRITE);
+ for (k = 0; k < spanWidth; k++) {
+ dest[k] = (zValues[k] << 8) | (sValues[k] & 0xff);
+ }
+ }
+ else {
+ uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
+ GLint k;
+ assert(usage == PIPE_TRANSFER_READ_WRITE);
+ for (k = 0; k < spanWidth; k++) {
+ dest[k] = (dest[k] & 0xffffff00) | (sValues[k] & 0xff);
+ }
+ }
+ break;
+ default:
+ assert(0);
+ }
+ }
+ }
+ skipPixels += spanWidth;
+ }
+
+ _mesa_unmap_pbo_source(ctx, &clippedUnpack);
+
+ /* unmap the stencil buffer */
+ pipe_transfer_unmap(pipe, pt);
+ pipe->transfer_destroy(pipe, pt);
+}
+
+
+/**
+ * Get fragment program variant for a glDrawPixels or glCopyPixels
+ * command for RGBA data.
+ */
+static struct st_fp_variant *
+get_color_fp_variant(struct st_context *st)
+{
+ struct gl_context *ctx = st->ctx;
+ struct st_fp_variant_key key;
+ struct st_fp_variant *fpv;
+
+ memset(&key, 0, sizeof(key));
+
+ key.st = st;
+ key.drawpixels = 1;
+ key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
+ ctx->Pixel.RedScale != 1.0 ||
+ ctx->Pixel.GreenBias != 0.0 ||
+ ctx->Pixel.GreenScale != 1.0 ||
+ ctx->Pixel.BlueBias != 0.0 ||
+ ctx->Pixel.BlueScale != 1.0 ||
+ ctx->Pixel.AlphaBias != 0.0 ||
+ ctx->Pixel.AlphaScale != 1.0);
+ key.pixelMaps = ctx->Pixel.MapColorFlag;
+
+ fpv = st_get_fp_variant(st, st->fp, &key);
+
+ return fpv;
+}
+
+
+/**
+ * Get fragment program variant for a glDrawPixels or glCopyPixels
+ * command for depth/stencil data.
+ */
+static struct st_fp_variant *
+get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
+ GLboolean write_stencil)
+{
+ struct st_fp_variant_key key;
+ struct st_fp_variant *fpv;
+
+ memset(&key, 0, sizeof(key));
+
+ key.st = st;
+ key.drawpixels = 1;
+ key.drawpixels_z = write_depth;
+ key.drawpixels_stencil = write_stencil;
+
+ fpv = st_get_fp_variant(st, st->fp, &key);
+
+ return fpv;
+}
+
+
+/**
+ * Called via ctx->Driver.DrawPixels()
+ */
+static void
+st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
+ GLsizei width, GLsizei height,
+ GLenum format, GLenum type,
+ const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
+{
+ void *driver_vp, *driver_fp;
+ struct st_context *st = st_context(ctx);
+ const GLfloat *color;
+ struct pipe_context *pipe = st->pipe;
+ GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
+ struct pipe_sampler_view *sv[2];
+ int num_sampler_view = 1;
+ enum pipe_format stencil_format = PIPE_FORMAT_NONE;
+ struct st_fp_variant *fpv;
+
+ if (format == GL_DEPTH_STENCIL)
+ write_stencil = write_depth = GL_TRUE;
+ else if (format == GL_STENCIL_INDEX)
+ write_stencil = GL_TRUE;
+ else if (format == GL_DEPTH_COMPONENT)
+ write_depth = GL_TRUE;
+
+ if (write_stencil) {
+ enum pipe_format tex_format;
+ /* can we write to stencil if not fallback */
+ if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT))
+ goto stencil_fallback;
+
+ tex_format = st_choose_format(st->pipe->screen, base_format(format),
+ PIPE_TEXTURE_2D,
+ 0, PIPE_BIND_SAMPLER_VIEW);
+ if (tex_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED)
+ stencil_format = PIPE_FORMAT_X24S8_USCALED;
+ else if (tex_format == PIPE_FORMAT_S8_USCALED_Z24_UNORM)
+ stencil_format = PIPE_FORMAT_S8X24_USCALED;
+ else
+ stencil_format = PIPE_FORMAT_S8_USCALED;
+ if (stencil_format == PIPE_FORMAT_NONE)
+ goto stencil_fallback;
+ }
+
+ /* Mesa state should be up to date by now */
+ assert(ctx->NewState == 0x0);
+
+ st_validate_state(st);
+
+ /*
+ * Get vertex/fragment shaders
+ */
+ if (write_depth || write_stencil) {
+ fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
+
+ driver_fp = fpv->driver_shader;
+
+ driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
+
+ color = ctx->Current.RasterColor;
+ }
+ else {
+ fpv = get_color_fp_variant(st);
+
+ driver_fp = fpv->driver_shader;
+
+ driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
+
+ color = NULL;
+ if (st->pixel_xfer.pixelmap_enabled) {
+ sv[1] = st->pixel_xfer.pixelmap_sampler_view;
+ num_sampler_view++;
+ }
+ }
+
+ /* update fragment program constants */
+ st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
+
+ /* draw with textured quad */
+ {
+ struct pipe_resource *pt
+ = make_texture(st, width, height, format, type, unpack, pixels);
+ if (pt) {
+ sv[0] = st_create_texture_sampler_view(st->pipe, pt);
+
+ if (sv[0]) {
+ if (write_stencil) {
+ sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
+ stencil_format);
+ num_sampler_view++;
+ }
+
+ draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
+ width, height,
+ ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
+ sv,
+ num_sampler_view,
+ driver_vp,
+ driver_fp,
+ color, GL_FALSE, write_depth, write_stencil);
+ pipe_sampler_view_reference(&sv[0], NULL);
+ if (num_sampler_view > 1)
+ pipe_sampler_view_reference(&sv[1], NULL);
+ }
+ pipe_resource_reference(&pt, NULL);
+ }
+ }
+ return;
+
+stencil_fallback:
+ draw_stencil_pixels(ctx, x, y, width, height, format, type,
+ unpack, pixels);
+}
+
+
+
+/**
+ * Software fallback for glCopyPixels(GL_STENCIL).
+ */
+static void
+copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
+ GLsizei width, GLsizei height,
+ GLint dstx, GLint dsty)
+{
+ struct st_renderbuffer *rbDraw;
+ struct pipe_context *pipe = st_context(ctx)->pipe;
+ enum pipe_transfer_usage usage;
+ struct pipe_transfer *ptDraw;
+ ubyte *drawMap;
+ ubyte *buffer;
+ int i;
+
+ buffer = malloc(width * height * sizeof(ubyte));
+ if (!buffer) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels(stencil)");
+ return;
+ }
+
+ /* Get the dest renderbuffer. If there's a wrapper, use the
+ * underlying renderbuffer.
+ */
+ rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
+ if (rbDraw->Base.Wrapped)
+ rbDraw = st_renderbuffer(rbDraw->Base.Wrapped);
+
+ /* this will do stencil pixel transfer ops */
+ st_read_stencil_pixels(ctx, srcx, srcy, width, height,
+ GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
+ &ctx->DefaultPacking, buffer);
+
+ if (0) {
+ /* debug code: dump stencil values */
+ GLint row, col;
+ for (row = 0; row < height; row++) {
+ printf("%3d: ", row);
+ for (col = 0; col < width; col++) {
+ printf("%02x ", buffer[col + row * width]);
+ }
+ printf("\n");
+ }
+ }
+
+ if (util_format_get_component_bits(rbDraw->format,
+ UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
+ usage = PIPE_TRANSFER_READ_WRITE;
+ else
+ usage = PIPE_TRANSFER_WRITE;
+
+ if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
+ dsty = rbDraw->Base.Height - dsty - height;
+ }
+
+ ptDraw = pipe_get_transfer(st_context(ctx)->pipe,
+ rbDraw->texture, 0, 0,
+ usage, dstx, dsty,
+ width, height);
+
+ assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
+ assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
+
+ /* map the stencil buffer */
+ drawMap = pipe_transfer_map(pipe, ptDraw);
+
+ /* draw */
+ /* XXX PixelZoom not handled yet */
+ for (i = 0; i < height; i++) {
+ ubyte *dst;
+ const ubyte *src;
+ int y;
+
+ y = i;
+
+ if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
+ y = height - y - 1;
+ }
+
+ dst = drawMap + y * ptDraw->stride;
+ src = buffer + i * width;
+
+ switch (ptDraw->resource->format) {
+ case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
+ {
+ uint *dst4 = (uint *) dst;
+ int j;
+ assert(usage == PIPE_TRANSFER_READ_WRITE);
+ for (j = 0; j < width; j++) {
+ *dst4 = (*dst4 & 0xffffff) | (src[j] << 24);
+ dst4++;
+ }
+ }
+ break;
+ case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
+ {
+ uint *dst4 = (uint *) dst;
+ int j;
+ assert(usage == PIPE_TRANSFER_READ_WRITE);
+ for (j = 0; j < width; j++) {
+ *dst4 = (*dst4 & 0xffffff00) | (src[j] & 0xff);
+ dst4++;
+ }
+ }
+ break;
+ case PIPE_FORMAT_S8_USCALED:
+ assert(usage == PIPE_TRANSFER_WRITE);
+ memcpy(dst, src, width);
+ break;
+ default:
+ assert(0);
+ }
+ }
+
+ free(buffer);
+
+ /* unmap the stencil buffer */
+ pipe_transfer_unmap(pipe, ptDraw);
+ pipe->transfer_destroy(pipe, ptDraw);
+}
+
+
+/** Do the src/dest regions overlap? */
+static GLboolean
+regions_overlap(GLint srcX, GLint srcY, GLint dstX, GLint dstY,
+ GLsizei width, GLsizei height)
+{
+ if (srcX + width <= dstX ||
+ dstX + width <= srcX ||
+ srcY + height <= dstY ||
+ dstY + height <= srcY)
+ return GL_FALSE;
+ else
+ return GL_TRUE;
+}
+
+
+/**
+ * Try to do a glCopyPixels for simple cases with a blit by calling
+ * pipe->resource_copy_region().
+ *
+ * We can do this when we're copying color pixels (depth/stencil
+ * eventually) with no pixel zoom, no pixel transfer ops, no
+ * per-fragment ops, the src/dest regions don't overlap and the
+ * src/dest pixel formats are the same.
+ */
+static GLboolean
+blit_copy_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
+ GLsizei width, GLsizei height,
+ GLint dstx, GLint dsty, GLenum type)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct gl_pixelstore_attrib pack, unpack;
+ GLint readX, readY, readW, readH;
+
+ if (type == GL_COLOR &&
+ ctx->Pixel.ZoomX == 1.0 &&
+ ctx->Pixel.ZoomY == 1.0 &&
+ ctx->_ImageTransferState == 0x0 &&
+ !ctx->Color.BlendEnabled &&
+ !ctx->Color.AlphaEnabled &&
+ !ctx->Depth.Test &&
+ !ctx->Fog.Enabled &&
+ !ctx->Stencil.Enabled &&
+ !ctx->FragmentProgram.Enabled &&
+ !ctx->VertexProgram.Enabled &&
+ !ctx->Shader.CurrentFragmentProgram &&
+ st_fb_orientation(ctx->ReadBuffer) == st_fb_orientation(ctx->DrawBuffer) &&
+ ctx->DrawBuffer->_NumColorDrawBuffers == 1) {
+ struct st_renderbuffer *rbRead, *rbDraw;
+ GLint drawX, drawY;
+
+ /*
+ * Clip the read region against the src buffer bounds.
+ * We'll still allocate a temporary buffer/texture for the original
+ * src region size but we'll only read the region which is on-screen.
+ * This may mean that we draw garbage pixels into the dest region, but
+ * that's expected.
+ */
+ readX = srcx;
+ readY = srcy;
+ readW = width;
+ readH = height;
+ pack = ctx->DefaultPacking;
+ if (!_mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack))
+ return GL_TRUE; /* all done */
+
+ /* clip against dest buffer bounds and scissor box */
+ drawX = dstx + pack.SkipPixels;
+ drawY = dsty + pack.SkipRows;
+ unpack = pack;
+ if (!_mesa_clip_drawpixels(ctx, &drawX, &drawY, &readW, &readH, &unpack))
+ return GL_TRUE; /* all done */
+
+ readX = readX - pack.SkipPixels + unpack.SkipPixels;
+ readY = readY - pack.SkipRows + unpack.SkipRows;
+
+ rbRead = st_get_color_read_renderbuffer(ctx);
+ rbDraw = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
+
+ if ((rbRead != rbDraw ||
+ !regions_overlap(readX, readY, drawX, drawY, readW, readH)) &&
+ rbRead->Base.Format == rbDraw->Base.Format) {
+ struct pipe_box srcBox;
+
+ /* flip src/dst position if needed */
+ if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+ /* both buffers will have the same orientation */
+ readY = ctx->ReadBuffer->Height - readY - readH;
+ drawY = ctx->DrawBuffer->Height - drawY - readH;
+ }
+
+ u_box_2d(readX, readY, readW, readH, &srcBox);
+
+ pipe->resource_copy_region(pipe,
+ rbDraw->texture, 0, drawX, drawY, 0,
+ rbRead->texture, 0, &srcBox);
+ return GL_TRUE;
+ }
+ }
+
+ return GL_FALSE;
+}
+
+
+static void
+st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
+ GLsizei width, GLsizei height,
+ GLint dstx, GLint dsty, GLenum type)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_screen *screen = pipe->screen;
+ struct st_renderbuffer *rbRead;
+ void *driver_vp, *driver_fp;
+ struct pipe_resource *pt;
+ struct pipe_sampler_view *sv[2];
+ int num_sampler_view = 1;
+ GLfloat *color;
+ enum pipe_format srcFormat, texFormat;
+ GLboolean invertTex = GL_FALSE;
+ GLint readX, readY, readW, readH;
+ GLuint sample_count;
+ struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
+ struct st_fp_variant *fpv;
+
+ st_validate_state(st);
+
+ if (type == GL_STENCIL) {
+ /* can't use texturing to do stencil */
+ copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
+ return;
+ }
+
+ if (blit_copy_pixels(ctx, srcx, srcy, width, height, dstx, dsty, type))
+ return;
+
+ /*
+ * The subsequent code implements glCopyPixels by copying the source
+ * pixels into a temporary texture that's then applied to a textured quad.
+ * When we draw the textured quad, all the usual per-fragment operations
+ * are handled.
+ */
+
+
+ /*
+ * Get vertex/fragment shaders
+ */
+ if (type == GL_COLOR) {
+ rbRead = st_get_color_read_renderbuffer(ctx);
+ color = NULL;
+
+ fpv = get_color_fp_variant(st);
+ driver_fp = fpv->driver_shader;
+
+ driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
+
+ if (st->pixel_xfer.pixelmap_enabled) {
+ sv[1] = st->pixel_xfer.pixelmap_sampler_view;
+ num_sampler_view++;
+ }
+ }
+ else {
+ assert(type == GL_DEPTH);
+ rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
+ color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
+
+ fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
+ driver_fp = fpv->driver_shader;
+
+ driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
+ }
+
+ /* update fragment program constants */
+ st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
+
+
+ if (rbRead->Base.Wrapped)
+ rbRead = st_renderbuffer(rbRead->Base.Wrapped);
+
+ sample_count = rbRead->texture->nr_samples;
+ /* I believe this would be legal, presumably would need to do a resolve
+ for color, and for depth/stencil spec says to just use one of the
+ depth/stencil samples per pixel? Need some transfer clarifications. */
+ assert(sample_count < 2);
+
+ srcFormat = rbRead->texture->format;
+
+ if (screen->is_format_supported(screen, srcFormat, st->internal_target,
+ sample_count,
+ PIPE_BIND_SAMPLER_VIEW)) {
+ texFormat = srcFormat;
+ }
+ else {
+ /* srcFormat can't be used as a texture format */
+ if (type == GL_DEPTH) {
+ texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
+ st->internal_target, sample_count,
+ PIPE_BIND_DEPTH_STENCIL);
+ assert(texFormat != PIPE_FORMAT_NONE);
+ }
+ else {
+ /* default color format */
+ texFormat = st_choose_format(screen, GL_RGBA, st->internal_target,
+ sample_count, PIPE_BIND_SAMPLER_VIEW);
+ assert(texFormat != PIPE_FORMAT_NONE);
+ }
+ }
+
+ /* Invert src region if needed */
+ if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+ srcy = ctx->ReadBuffer->Height - srcy - height;
+ invertTex = !invertTex;
+ }
+
+ /* Clip the read region against the src buffer bounds.
+ * We'll still allocate a temporary buffer/texture for the original
+ * src region size but we'll only read the region which is on-screen.
+ * This may mean that we draw garbage pixels into the dest region, but
+ * that's expected.
+ */
+ readX = srcx;
+ readY = srcy;
+ readW = width;
+ readH = height;
+ _mesa_clip_readpixels(ctx, &readX, &readY, &readW, &readH, &pack);
+ readW = MAX2(0, readW);
+ readH = MAX2(0, readH);
+
+ /* alloc temporary texture */
+ pt = alloc_texture(st, width, height, texFormat);
+ if (!pt)
+ return;
+
+ sv[0] = st_create_texture_sampler_view(st->pipe, pt);
+ if (!sv[0]) {
+ pipe_resource_reference(&pt, NULL);
+ return;
+ }
+
+ /* Make temporary texture which is a copy of the src region.
+ */
+ if (srcFormat == texFormat) {
+ struct pipe_box src_box;
+ u_box_2d(readX, readY, readW, readH, &src_box);
+ /* copy source framebuffer surface into mipmap/texture */
+ pipe->resource_copy_region(pipe,
+ pt, /* dest tex */
+ 0,
+ pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
+ rbRead->texture, /* src tex */
+ 0,
+ &src_box);
+
+ }
+ else {
+ /* CPU-based fallback/conversion */
+ struct pipe_transfer *ptRead =
+ pipe_get_transfer(st->pipe, rbRead->texture,
+ 0, 0, /* level, layer */
+ PIPE_TRANSFER_READ,
+ readX, readY, readW, readH);
+ struct pipe_transfer *ptTex;
+ enum pipe_transfer_usage transfer_usage;
+
+ if (ST_DEBUG & DEBUG_FALLBACK)
+ debug_printf("%s: fallback processing\n", __FUNCTION__);
+
+ if (type == GL_DEPTH && util_format_is_depth_and_stencil(pt->format))
+ transfer_usage = PIPE_TRANSFER_READ_WRITE;
+ else
+ transfer_usage = PIPE_TRANSFER_WRITE;
+
+ ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
+ 0, 0, width, height);
+
+ /* copy image from ptRead surface to ptTex surface */
+ if (type == GL_COLOR) {
+ /* alternate path using get/put_tile() */
+ GLfloat *buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
+ enum pipe_format readFormat, drawFormat;
+ readFormat = util_format_linear(rbRead->texture->format);
+ drawFormat = util_format_linear(pt->format);
+ pipe_get_tile_rgba_format(pipe, ptRead, 0, 0, readW, readH,
+ readFormat, buf);
+ pipe_put_tile_rgba_format(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
+ readW, readH, drawFormat, buf);
+ free(buf);
+ }
+ else {
+ /* GL_DEPTH */
+ GLuint *buf = (GLuint *) malloc(width * height * sizeof(GLuint));
+ pipe_get_tile_z(pipe, ptRead, 0, 0, readW, readH, buf);
+ pipe_put_tile_z(pipe, ptTex, pack.SkipPixels, pack.SkipRows,
+ readW, readH, buf);
+ free(buf);
+ }
+
+ pipe->transfer_destroy(pipe, ptRead);
+ pipe->transfer_destroy(pipe, ptTex);
+ }
+
+ /* OK, the texture 'pt' contains the src image/pixels. Now draw a
+ * textured quad with that texture.
+ */
+ draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
+ width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
+ sv,
+ num_sampler_view,
+ driver_vp,
+ driver_fp,
+ color, invertTex, GL_FALSE, GL_FALSE);
+
+ pipe_resource_reference(&pt, NULL);
+ pipe_sampler_view_reference(&sv[0], NULL);
+}
+
+
+
+void st_init_drawpixels_functions(struct dd_function_table *functions)
+{
+ functions->DrawPixels = st_DrawPixels;
+ functions->CopyPixels = st_CopyPixels;
+}
+
+
+void
+st_destroy_drawpix(struct st_context *st)
+{
+ GLuint i;
+
+ for (i = 0; i < Elements(st->drawpix.shaders); i++) {
+ if (st->drawpix.shaders[i])
+ _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
+ }
+
+ st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
+ if (st->drawpix.vert_shaders[0])
+ ureg_free_tokens(st->drawpix.vert_shaders[0]);
+ if (st->drawpix.vert_shaders[1])
+ ureg_free_tokens(st->drawpix.vert_shaders[1]);
+}
+
+#endif /* FEATURE_drawpix */
diff --git a/mesalib/src/mesa/state_tracker/st_cb_drawtex.c b/mesalib/src/mesa/state_tracker/st_cb_drawtex.c index 86ceb9d78..db299eb8d 100644 --- a/mesalib/src/mesa/state_tracker/st_cb_drawtex.c +++ b/mesalib/src/mesa/state_tracker/st_cb_drawtex.c @@ -1,307 +1,307 @@ -/************************************************************************** - * - * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - **************************************************************************/ - - -/** - * Implementation of glDrawTex() for GL_OES_draw_tex - */ - - - -#include "main/imports.h" -#include "main/image.h" -#include "main/macros.h" -#include "main/mfeatures.h" -#include "program/program.h" -#include "program/prog_print.h" - -#include "st_context.h" -#include "st_atom.h" -#include "st_cb_drawtex.h" - -#include "pipe/p_context.h" -#include "pipe/p_defines.h" -#include "util/u_inlines.h" -#include "pipe/p_shader_tokens.h" -#include "util/u_draw_quad.h" -#include "util/u_simple_shaders.h" - -#include "cso_cache/cso_context.h" - - -#if FEATURE_OES_draw_texture - - -struct cached_shader -{ - void *handle; - - uint num_attribs; - uint semantic_names[2 + MAX_TEXTURE_UNITS]; - uint semantic_indexes[2 + MAX_TEXTURE_UNITS]; -}; - -#define MAX_SHADERS (2 * MAX_TEXTURE_UNITS) - -/** - * Simple linear list cache. - * Most of the time there'll only be one cached shader. - */ -static struct cached_shader CachedShaders[MAX_SHADERS]; -static GLuint NumCachedShaders = 0; - - -static void * -lookup_shader(struct pipe_context *pipe, - uint num_attribs, - const uint *semantic_names, - const uint *semantic_indexes) -{ - GLuint i, j; - - /* look for existing shader with same attributes */ - for (i = 0; i < NumCachedShaders; i++) { - if (CachedShaders[i].num_attribs == num_attribs) { - GLboolean match = GL_TRUE; - for (j = 0; j < num_attribs; j++) { - if (semantic_names[j] != CachedShaders[i].semantic_names[j] || - semantic_indexes[j] != CachedShaders[i].semantic_indexes[j]) { - match = GL_FALSE; - break; - } - } - if (match) - return CachedShaders[i].handle; - } - } - - /* not found - create new one now */ - if (NumCachedShaders >= MAX_SHADERS) { - return NULL; - } - - CachedShaders[i].num_attribs = num_attribs; - for (j = 0; j < num_attribs; j++) { - CachedShaders[i].semantic_names[j] = semantic_names[j]; - CachedShaders[i].semantic_indexes[j] = semantic_indexes[j]; - } - - CachedShaders[i].handle = - util_make_vertex_passthrough_shader(pipe, - num_attribs, - semantic_names, - semantic_indexes); - NumCachedShaders++; - - return CachedShaders[i].handle; -} - -static void -st_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z, - GLfloat width, GLfloat height) -{ - struct st_context *st = ctx->st; - struct pipe_context *pipe = st->pipe; - struct cso_context *cso = ctx->st->cso_context; - struct pipe_resource *vbuffer; - struct pipe_transfer *vbuffer_transfer; - GLuint i, numTexCoords, numAttribs; - GLboolean emitColor; - uint semantic_names[2 + MAX_TEXTURE_UNITS]; - uint semantic_indexes[2 + MAX_TEXTURE_UNITS]; - struct pipe_vertex_element velements[2 + MAX_TEXTURE_UNITS]; - GLbitfield inputs = VERT_BIT_POS; - - st_validate_state(st); - - /* determine if we need vertex color */ - if (ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_COL0) - emitColor = GL_TRUE; - else - emitColor = GL_FALSE; - - /* determine how many enabled sets of texcoords */ - numTexCoords = 0; - for (i = 0; i < ctx->Const.MaxTextureUnits; i++) { - if (ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_2D_BIT) { - inputs |= VERT_BIT_TEX(i); - numTexCoords++; - } - } - - /* total number of attributes per vertex */ - numAttribs = 1 + emitColor + numTexCoords; - - - /* create the vertex buffer */ - vbuffer = pipe_buffer_create(pipe->screen, PIPE_BIND_VERTEX_BUFFER, - PIPE_USAGE_STREAM, - numAttribs * 4 * 4 * sizeof(GLfloat)); - - /* load vertex buffer */ - { -#define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \ - do { \ - GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \ - assert(k < 4 * 4 * numAttribs); \ - vbuf[k + 0] = X; \ - vbuf[k + 1] = Y; \ - vbuf[k + 2] = Z; \ - vbuf[k + 3] = W; \ - } while (0) - - const GLfloat x0 = x, y0 = y, x1 = x + width, y1 = y + height; - GLfloat *vbuf = (GLfloat *) pipe_buffer_map(pipe, vbuffer, - PIPE_TRANSFER_WRITE, - &vbuffer_transfer); - GLuint attr; - - z = CLAMP(z, 0.0f, 1.0f); - - /* positions (in clip coords) */ - { - const struct gl_framebuffer *fb = st->ctx->DrawBuffer; - const GLfloat fb_width = (GLfloat)fb->Width; - const GLfloat fb_height = (GLfloat)fb->Height; - - const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0); - const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0); - const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0); - const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0); - - SET_ATTRIB(0, 0, clip_x0, clip_y0, z, 1.0f); /* lower left */ - SET_ATTRIB(1, 0, clip_x1, clip_y0, z, 1.0f); /* lower right */ - SET_ATTRIB(2, 0, clip_x1, clip_y1, z, 1.0f); /* upper right */ - SET_ATTRIB(3, 0, clip_x0, clip_y1, z, 1.0f); /* upper left */ - - semantic_names[0] = TGSI_SEMANTIC_POSITION; - semantic_indexes[0] = 0; - } - - /* colors */ - if (emitColor) { - const GLfloat *c = ctx->Current.Attrib[VERT_ATTRIB_COLOR0]; - SET_ATTRIB(0, 1, c[0], c[1], c[2], c[3]); - SET_ATTRIB(1, 1, c[0], c[1], c[2], c[3]); - SET_ATTRIB(2, 1, c[0], c[1], c[2], c[3]); - SET_ATTRIB(3, 1, c[0], c[1], c[2], c[3]); - semantic_names[1] = TGSI_SEMANTIC_COLOR; - semantic_indexes[1] = 0; - attr = 2; - } - else { - attr = 1; - } - - /* texcoords */ - for (i = 0; i < ctx->Const.MaxTextureUnits; i++) { - if (ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_2D_BIT) { - struct gl_texture_object *obj = ctx->Texture.Unit[i]._Current; - struct gl_texture_image *img = obj->Image[0][obj->BaseLevel]; - const GLfloat wt = (GLfloat) img->Width; - const GLfloat ht = (GLfloat) img->Height; - const GLfloat s0 = obj->CropRect[0] / wt; - const GLfloat t0 = obj->CropRect[1] / ht; - const GLfloat s1 = (obj->CropRect[0] + obj->CropRect[2]) / wt; - const GLfloat t1 = (obj->CropRect[1] + obj->CropRect[3]) / ht; - - /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/ - SET_ATTRIB(0, attr, s0, t0, 0.0f, 1.0f); /* lower left */ - SET_ATTRIB(1, attr, s1, t0, 0.0f, 1.0f); /* lower right */ - SET_ATTRIB(2, attr, s1, t1, 0.0f, 1.0f); /* upper right */ - SET_ATTRIB(3, attr, s0, t1, 0.0f, 1.0f); /* upper left */ - - semantic_names[attr] = TGSI_SEMANTIC_GENERIC; - semantic_indexes[attr] = 0; - - attr++; - } - } - - pipe_buffer_unmap(pipe, vbuffer_transfer); - -#undef SET_ATTRIB - } - - - cso_save_viewport(cso); - cso_save_vertex_shader(cso); - cso_save_vertex_elements(cso); - cso_save_vertex_buffers(cso); - - { - void *vs = lookup_shader(pipe, numAttribs, - semantic_names, semantic_indexes); - cso_set_vertex_shader_handle(cso, vs); - } - - for (i = 0; i < numAttribs; i++) { - velements[i].src_offset = i * 4 * sizeof(float); - velements[i].instance_divisor = 0; - velements[i].vertex_buffer_index = 0; - velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; - } - cso_set_vertex_elements(cso, numAttribs, velements); - - /* viewport state: viewport matching window dims */ - { - const struct gl_framebuffer *fb = st->ctx->DrawBuffer; - const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP); - const GLfloat width = (GLfloat)fb->Width; - const GLfloat height = (GLfloat)fb->Height; - struct pipe_viewport_state vp; - vp.scale[0] = 0.5f * width; - vp.scale[1] = height * (invert ? -0.5f : 0.5f); - vp.scale[2] = 1.0f; - vp.scale[3] = 1.0f; - vp.translate[0] = 0.5f * width; - vp.translate[1] = 0.5f * height; - vp.translate[2] = 0.0f; - vp.translate[3] = 0.0f; - cso_set_viewport(cso, &vp); - } - - - util_draw_vertex_buffer(pipe, cso, vbuffer, - 0, /* offset */ - PIPE_PRIM_TRIANGLE_FAN, - 4, /* verts */ - numAttribs); /* attribs/vert */ - - - pipe_resource_reference(&vbuffer, NULL); - - /* restore state */ - cso_restore_viewport(cso); - cso_restore_vertex_shader(cso); - cso_restore_vertex_elements(cso); - cso_restore_vertex_buffers(cso); -} - - -void -st_init_drawtex_functions(struct dd_function_table *functions) -{ - functions->DrawTex = st_DrawTex; -} - - -/** - * Free any cached shaders - */ -void -st_destroy_drawtex(struct st_context *st) -{ - GLuint i; - for (i = 0; i < NumCachedShaders; i++) { - cso_delete_vertex_shader(st->cso_context, CachedShaders[i].handle); - } - NumCachedShaders = 0; -} - - -#endif /* FEATURE_OES_draw_texture */ +/**************************************************************************
+ *
+ * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ **************************************************************************/
+
+
+/**
+ * Implementation of glDrawTex() for GL_OES_draw_tex
+ */
+
+
+
+#include "main/imports.h"
+#include "main/image.h"
+#include "main/macros.h"
+#include "main/mfeatures.h"
+#include "program/program.h"
+#include "program/prog_print.h"
+
+#include "st_context.h"
+#include "st_atom.h"
+#include "st_cb_drawtex.h"
+
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "util/u_inlines.h"
+#include "pipe/p_shader_tokens.h"
+#include "util/u_draw_quad.h"
+#include "util/u_simple_shaders.h"
+
+#include "cso_cache/cso_context.h"
+
+
+#if FEATURE_OES_draw_texture
+
+
+struct cached_shader
+{
+ void *handle;
+
+ uint num_attribs;
+ uint semantic_names[2 + MAX_TEXTURE_UNITS];
+ uint semantic_indexes[2 + MAX_TEXTURE_UNITS];
+};
+
+#define MAX_SHADERS (2 * MAX_TEXTURE_UNITS)
+
+/**
+ * Simple linear list cache.
+ * Most of the time there'll only be one cached shader.
+ */
+static struct cached_shader CachedShaders[MAX_SHADERS];
+static GLuint NumCachedShaders = 0;
+
+
+static void *
+lookup_shader(struct pipe_context *pipe,
+ uint num_attribs,
+ const uint *semantic_names,
+ const uint *semantic_indexes)
+{
+ GLuint i, j;
+
+ /* look for existing shader with same attributes */
+ for (i = 0; i < NumCachedShaders; i++) {
+ if (CachedShaders[i].num_attribs == num_attribs) {
+ GLboolean match = GL_TRUE;
+ for (j = 0; j < num_attribs; j++) {
+ if (semantic_names[j] != CachedShaders[i].semantic_names[j] ||
+ semantic_indexes[j] != CachedShaders[i].semantic_indexes[j]) {
+ match = GL_FALSE;
+ break;
+ }
+ }
+ if (match)
+ return CachedShaders[i].handle;
+ }
+ }
+
+ /* not found - create new one now */
+ if (NumCachedShaders >= MAX_SHADERS) {
+ return NULL;
+ }
+
+ CachedShaders[i].num_attribs = num_attribs;
+ for (j = 0; j < num_attribs; j++) {
+ CachedShaders[i].semantic_names[j] = semantic_names[j];
+ CachedShaders[i].semantic_indexes[j] = semantic_indexes[j];
+ }
+
+ CachedShaders[i].handle =
+ util_make_vertex_passthrough_shader(pipe,
+ num_attribs,
+ semantic_names,
+ semantic_indexes);
+ NumCachedShaders++;
+
+ return CachedShaders[i].handle;
+}
+
+static void
+st_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
+ GLfloat width, GLfloat height)
+{
+ struct st_context *st = ctx->st;
+ struct pipe_context *pipe = st->pipe;
+ struct cso_context *cso = ctx->st->cso_context;
+ struct pipe_resource *vbuffer;
+ struct pipe_transfer *vbuffer_transfer;
+ GLuint i, numTexCoords, numAttribs;
+ GLboolean emitColor;
+ uint semantic_names[2 + MAX_TEXTURE_UNITS];
+ uint semantic_indexes[2 + MAX_TEXTURE_UNITS];
+ struct pipe_vertex_element velements[2 + MAX_TEXTURE_UNITS];
+ GLbitfield inputs = VERT_BIT_POS;
+
+ st_validate_state(st);
+
+ /* determine if we need vertex color */
+ if (ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_COL0)
+ emitColor = GL_TRUE;
+ else
+ emitColor = GL_FALSE;
+
+ /* determine how many enabled sets of texcoords */
+ numTexCoords = 0;
+ for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
+ if (ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_2D_BIT) {
+ inputs |= VERT_BIT_TEX(i);
+ numTexCoords++;
+ }
+ }
+
+ /* total number of attributes per vertex */
+ numAttribs = 1 + emitColor + numTexCoords;
+
+
+ /* create the vertex buffer */
+ vbuffer = pipe_buffer_create(pipe->screen, PIPE_BIND_VERTEX_BUFFER,
+ PIPE_USAGE_STREAM,
+ numAttribs * 4 * 4 * sizeof(GLfloat));
+
+ /* load vertex buffer */
+ {
+#define SET_ATTRIB(VERT, ATTR, X, Y, Z, W) \
+ do { \
+ GLuint k = (((VERT) * numAttribs + (ATTR)) * 4); \
+ assert(k < 4 * 4 * numAttribs); \
+ vbuf[k + 0] = X; \
+ vbuf[k + 1] = Y; \
+ vbuf[k + 2] = Z; \
+ vbuf[k + 3] = W; \
+ } while (0)
+
+ const GLfloat x0 = x, y0 = y, x1 = x + width, y1 = y + height;
+ GLfloat *vbuf = (GLfloat *) pipe_buffer_map(pipe, vbuffer,
+ PIPE_TRANSFER_WRITE,
+ &vbuffer_transfer);
+ GLuint attr;
+
+ z = CLAMP(z, 0.0f, 1.0f);
+
+ /* positions (in clip coords) */
+ {
+ const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
+ const GLfloat fb_width = (GLfloat)fb->Width;
+ const GLfloat fb_height = (GLfloat)fb->Height;
+
+ const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
+ const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
+ const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
+ const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
+
+ SET_ATTRIB(0, 0, clip_x0, clip_y0, z, 1.0f); /* lower left */
+ SET_ATTRIB(1, 0, clip_x1, clip_y0, z, 1.0f); /* lower right */
+ SET_ATTRIB(2, 0, clip_x1, clip_y1, z, 1.0f); /* upper right */
+ SET_ATTRIB(3, 0, clip_x0, clip_y1, z, 1.0f); /* upper left */
+
+ semantic_names[0] = TGSI_SEMANTIC_POSITION;
+ semantic_indexes[0] = 0;
+ }
+
+ /* colors */
+ if (emitColor) {
+ const GLfloat *c = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
+ SET_ATTRIB(0, 1, c[0], c[1], c[2], c[3]);
+ SET_ATTRIB(1, 1, c[0], c[1], c[2], c[3]);
+ SET_ATTRIB(2, 1, c[0], c[1], c[2], c[3]);
+ SET_ATTRIB(3, 1, c[0], c[1], c[2], c[3]);
+ semantic_names[1] = TGSI_SEMANTIC_COLOR;
+ semantic_indexes[1] = 0;
+ attr = 2;
+ }
+ else {
+ attr = 1;
+ }
+
+ /* texcoords */
+ for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
+ if (ctx->Texture.Unit[i]._ReallyEnabled & TEXTURE_2D_BIT) {
+ struct gl_texture_object *obj = ctx->Texture.Unit[i]._Current;
+ struct gl_texture_image *img = obj->Image[0][obj->BaseLevel];
+ const GLfloat wt = (GLfloat) img->Width;
+ const GLfloat ht = (GLfloat) img->Height;
+ const GLfloat s0 = obj->CropRect[0] / wt;
+ const GLfloat t0 = obj->CropRect[1] / ht;
+ const GLfloat s1 = (obj->CropRect[0] + obj->CropRect[2]) / wt;
+ const GLfloat t1 = (obj->CropRect[1] + obj->CropRect[3]) / ht;
+
+ /*printf("crop texcoords: %g, %g .. %g, %g\n", s0, t0, s1, t1);*/
+ SET_ATTRIB(0, attr, s0, t0, 0.0f, 1.0f); /* lower left */
+ SET_ATTRIB(1, attr, s1, t0, 0.0f, 1.0f); /* lower right */
+ SET_ATTRIB(2, attr, s1, t1, 0.0f, 1.0f); /* upper right */
+ SET_ATTRIB(3, attr, s0, t1, 0.0f, 1.0f); /* upper left */
+
+ semantic_names[attr] = TGSI_SEMANTIC_GENERIC;
+ semantic_indexes[attr] = 0;
+
+ attr++;
+ }
+ }
+
+ pipe_buffer_unmap(pipe, vbuffer_transfer);
+
+#undef SET_ATTRIB
+ }
+
+
+ cso_save_viewport(cso);
+ cso_save_vertex_shader(cso);
+ cso_save_vertex_elements(cso);
+ cso_save_vertex_buffers(cso);
+
+ {
+ void *vs = lookup_shader(pipe, numAttribs,
+ semantic_names, semantic_indexes);
+ cso_set_vertex_shader_handle(cso, vs);
+ }
+
+ for (i = 0; i < numAttribs; i++) {
+ velements[i].src_offset = i * 4 * sizeof(float);
+ velements[i].instance_divisor = 0;
+ velements[i].vertex_buffer_index = 0;
+ velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
+ }
+ cso_set_vertex_elements(cso, numAttribs, velements);
+
+ /* viewport state: viewport matching window dims */
+ {
+ const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
+ const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
+ const GLfloat width = (GLfloat)fb->Width;
+ const GLfloat height = (GLfloat)fb->Height;
+ struct pipe_viewport_state vp;
+ vp.scale[0] = 0.5f * width;
+ vp.scale[1] = height * (invert ? -0.5f : 0.5f);
+ vp.scale[2] = 1.0f;
+ vp.scale[3] = 1.0f;
+ vp.translate[0] = 0.5f * width;
+ vp.translate[1] = 0.5f * height;
+ vp.translate[2] = 0.0f;
+ vp.translate[3] = 0.0f;
+ cso_set_viewport(cso, &vp);
+ }
+
+
+ util_draw_vertex_buffer(pipe, cso, vbuffer,
+ 0, /* offset */
+ PIPE_PRIM_TRIANGLE_FAN,
+ 4, /* verts */
+ numAttribs); /* attribs/vert */
+
+
+ pipe_resource_reference(&vbuffer, NULL);
+
+ /* restore state */
+ cso_restore_viewport(cso);
+ cso_restore_vertex_shader(cso);
+ cso_restore_vertex_elements(cso);
+ cso_restore_vertex_buffers(cso);
+}
+
+
+void
+st_init_drawtex_functions(struct dd_function_table *functions)
+{
+ functions->DrawTex = st_DrawTex;
+}
+
+
+/**
+ * Free any cached shaders
+ */
+void
+st_destroy_drawtex(struct st_context *st)
+{
+ GLuint i;
+ for (i = 0; i < NumCachedShaders; i++) {
+ cso_delete_vertex_shader(st->cso_context, CachedShaders[i].handle);
+ }
+ NumCachedShaders = 0;
+}
+
+
+#endif /* FEATURE_OES_draw_texture */
diff --git a/mesalib/src/mesa/state_tracker/st_cb_readpixels.c b/mesalib/src/mesa/state_tracker/st_cb_readpixels.c index f8da2a4d1..687a49a57 100644 --- a/mesalib/src/mesa/state_tracker/st_cb_readpixels.c +++ b/mesalib/src/mesa/state_tracker/st_cb_readpixels.c @@ -1,561 +1,561 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - - -/** - * glReadPixels interface to pipe - * - * \author Brian Paul - */ - - -#include "main/imports.h" -#include "main/bufferobj.h" -#include "main/context.h" -#include "main/image.h" -#include "main/pack.h" -#include "main/pbo.h" - -#include "pipe/p_context.h" -#include "pipe/p_defines.h" -#include "util/u_format.h" -#include "util/u_inlines.h" -#include "util/u_tile.h" - -#include "st_debug.h" -#include "st_context.h" -#include "st_atom.h" -#include "st_cb_bitmap.h" -#include "st_cb_readpixels.h" -#include "st_cb_fbo.h" - -/** - * Special case for reading stencil buffer. - * For color/depth we use get_tile(). For stencil, map the stencil buffer. - */ -void -st_read_stencil_pixels(struct gl_context *ctx, GLint x, GLint y, - GLsizei width, GLsizei height, - GLenum format, GLenum type, - const struct gl_pixelstore_attrib *packing, - GLvoid *pixels) -{ - struct gl_framebuffer *fb = ctx->ReadBuffer; - struct pipe_context *pipe = st_context(ctx)->pipe; - struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer); - struct pipe_transfer *pt; - ubyte *stmap; - GLint j; - - if (strb->Base.Wrapped) { - strb = st_renderbuffer(strb->Base.Wrapped); - } - - if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { - y = ctx->DrawBuffer->Height - y - height; - } - - /* Create a read transfer from the renderbuffer's texture */ - - pt = pipe_get_transfer(pipe, strb->texture, - 0, 0, - PIPE_TRANSFER_READ, - x, y, width, height); - - /* map the stencil buffer */ - stmap = pipe_transfer_map(pipe, pt); - - /* width should never be > MAX_WIDTH since we did clipping earlier */ - ASSERT(width <= MAX_WIDTH); - - /* process image row by row */ - for (j = 0; j < height; j++) { - GLvoid *dest; - GLstencil sValues[MAX_WIDTH]; - GLfloat zValues[MAX_WIDTH]; - GLint srcY; - - if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) { - srcY = height - j - 1; - } - else { - srcY = j; - } - - /* get stencil (and Z) values */ - switch (pt->resource->format) { - case PIPE_FORMAT_S8_USCALED: - { - const ubyte *src = stmap + srcY * pt->stride; - memcpy(sValues, src, width); - } - break; - case PIPE_FORMAT_Z24_UNORM_S8_USCALED: - if (format == GL_DEPTH_STENCIL) { - const uint *src = (uint *) (stmap + srcY * pt->stride); - const GLfloat scale = 1.0f / (0xffffff); - GLint k; - for (k = 0; k < width; k++) { - sValues[k] = src[k] >> 24; - zValues[k] = (src[k] & 0xffffff) * scale; - } - } - else { - const uint *src = (uint *) (stmap + srcY * pt->stride); - GLint k; - for (k = 0; k < width; k++) { - sValues[k] = src[k] >> 24; - } - } - break; - case PIPE_FORMAT_S8_USCALED_Z24_UNORM: - if (format == GL_DEPTH_STENCIL) { - const uint *src = (uint *) (stmap + srcY * pt->stride); - const GLfloat scale = 1.0f / (0xffffff); - GLint k; - for (k = 0; k < width; k++) { - sValues[k] = src[k] & 0xff; - zValues[k] = (src[k] >> 8) * scale; - } - } - else { - const uint *src = (uint *) (stmap + srcY * pt->stride); - GLint k; - for (k = 0; k < width; k++) { - sValues[k] = src[k] & 0xff; - } - } - break; - default: - assert(0); - } - - /* store */ - dest = _mesa_image_address2d(packing, pixels, width, height, - format, type, j, 0); - if (format == GL_DEPTH_STENCIL) { - _mesa_pack_depth_stencil_span(ctx, width, dest, - zValues, sValues, packing); - } - else { - _mesa_pack_stencil_span(ctx, width, type, dest, sValues, packing); - } - } - - /* unmap the stencil buffer */ - pipe_transfer_unmap(pipe, pt); - pipe->transfer_destroy(pipe, pt); -} - - -/** - * Return renderbuffer to use for reading color pixels for glRead/CopyPixel - * commands. - */ -struct st_renderbuffer * -st_get_color_read_renderbuffer(struct gl_context *ctx) -{ - struct gl_framebuffer *fb = ctx->ReadBuffer; - struct st_renderbuffer *strb = - st_renderbuffer(fb->_ColorReadBuffer); - - return strb; -} - - -/** - * Try to do glReadPixels in a fast manner for common cases. - * \return GL_TRUE for success, GL_FALSE for failure - */ -static GLboolean -st_fast_readpixels(struct gl_context *ctx, struct st_renderbuffer *strb, - GLint x, GLint y, GLsizei width, GLsizei height, - GLenum format, GLenum type, - const struct gl_pixelstore_attrib *pack, - GLvoid *dest) -{ - enum combination { - A8R8G8B8_UNORM_TO_RGBA_UBYTE, - A8R8G8B8_UNORM_TO_RGB_UBYTE, - A8R8G8B8_UNORM_TO_BGRA_UINT - } combo; - - if (ctx->_ImageTransferState) - return GL_FALSE; - - if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM && - format == GL_RGBA && type == GL_UNSIGNED_BYTE) { - combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE; - } - else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM && - format == GL_RGB && type == GL_UNSIGNED_BYTE) { - combo = A8R8G8B8_UNORM_TO_RGB_UBYTE; - } - else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM && - format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) { - combo = A8R8G8B8_UNORM_TO_BGRA_UINT; - } - else { - return GL_FALSE; - } - - /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/ - - { - struct pipe_context *pipe = st_context(ctx)->pipe; - struct pipe_transfer *trans; - const GLubyte *map; - GLubyte *dst; - GLint row, col, dy, dstStride; - - if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { - /* convert GL Y to Gallium Y */ - y = strb->texture->height0 - y - height; - } - - trans = pipe_get_transfer(pipe, strb->texture, - 0, 0, - PIPE_TRANSFER_READ, - x, y, width, height); - if (!trans) { - return GL_FALSE; - } - - map = pipe_transfer_map(pipe, trans); - if (!map) { - pipe->transfer_destroy(pipe, trans); - return GL_FALSE; - } - - /* We always write to the user/dest buffer from low addr to high addr - * but the read order depends on renderbuffer orientation - */ - if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { - /* read source rows from bottom to top */ - y = height - 1; - dy = -1; - } - else { - /* read source rows from top to bottom */ - y = 0; - dy = 1; - } - - dst = _mesa_image_address2d(pack, dest, width, height, - format, type, 0, 0); - dstStride = _mesa_image_row_stride(pack, width, format, type); - - switch (combo) { - case A8R8G8B8_UNORM_TO_RGBA_UBYTE: - for (row = 0; row < height; row++) { - const GLubyte *src = map + y * trans->stride; - for (col = 0; col < width; col++) { - GLuint pixel = ((GLuint *) src)[col]; - dst[col*4+0] = (pixel >> 16) & 0xff; - dst[col*4+1] = (pixel >> 8) & 0xff; - dst[col*4+2] = (pixel >> 0) & 0xff; - dst[col*4+3] = (pixel >> 24) & 0xff; - } - dst += dstStride; - y += dy; - } - break; - case A8R8G8B8_UNORM_TO_RGB_UBYTE: - for (row = 0; row < height; row++) { - const GLubyte *src = map + y * trans->stride; - for (col = 0; col < width; col++) { - GLuint pixel = ((GLuint *) src)[col]; - dst[col*3+0] = (pixel >> 16) & 0xff; - dst[col*3+1] = (pixel >> 8) & 0xff; - dst[col*3+2] = (pixel >> 0) & 0xff; - } - dst += dstStride; - y += dy; - } - break; - case A8R8G8B8_UNORM_TO_BGRA_UINT: - for (row = 0; row < height; row++) { - const GLubyte *src = map + y * trans->stride; - memcpy(dst, src, 4 * width); - dst += dstStride; - y += dy; - } - break; - default: - ; /* nothing */ - } - - pipe_transfer_unmap(pipe, trans); - pipe->transfer_destroy(pipe, trans); - } - - return GL_TRUE; -} - - -/** - * Do glReadPixels by getting rows from the framebuffer transfer with - * get_tile(). Convert to requested format/type with Mesa image routines. - * Image transfer ops are done in software too. - */ -static void -st_readpixels(struct gl_context *ctx, GLint x, GLint y, GLsizei width, GLsizei height, - GLenum format, GLenum type, - const struct gl_pixelstore_attrib *pack, - GLvoid *dest) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - GLfloat (*temp)[4]; - const GLbitfield transferOps = ctx->_ImageTransferState; - GLsizei i, j; - GLint yStep, dfStride; - GLfloat *df; - struct st_renderbuffer *strb; - struct gl_pixelstore_attrib clippedPacking = *pack; - struct pipe_transfer *trans; - enum pipe_format pformat; - - assert(ctx->ReadBuffer->Width > 0); - - st_validate_state(st); - - /* Do all needed clipping here, so that we can forget about it later */ - if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) { - /* The ReadPixels transfer is totally outside the window bounds */ - return; - } - - st_flush_bitmap_cache(st); - - dest = _mesa_map_pbo_dest(ctx, &clippedPacking, dest); - if (!dest) - return; - - if (format == GL_STENCIL_INDEX || - format == GL_DEPTH_STENCIL) { - st_read_stencil_pixels(ctx, x, y, width, height, - format, type, pack, dest); - return; - } - else if (format == GL_DEPTH_COMPONENT) { - strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer); - if (strb->Base.Wrapped) { - strb = st_renderbuffer(strb->Base.Wrapped); - } - } - else { - /* Read color buffer */ - strb = st_get_color_read_renderbuffer(ctx); - } - - if (!strb) - return; - - /* try a fast-path readpixels before anything else */ - if (st_fast_readpixels(ctx, strb, x, y, width, height, - format, type, pack, dest)) { - /* success! */ - _mesa_unmap_pbo_dest(ctx, &clippedPacking); - return; - } - - /* allocate temp pixel row buffer */ - temp = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat)); - if (!temp) { - _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels"); - return; - } - - if (format == GL_RGBA && type == GL_FLOAT) { - /* write tile(row) directly into user's buffer */ - df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width, - height, format, type, 0, 0); - dfStride = width * 4; - } - else { - /* write tile(row) into temp row buffer */ - df = (GLfloat *) temp; - dfStride = 0; - } - - if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { - /* convert GL Y to Gallium Y */ - y = strb->Base.Height - y - height; - } - - /* Create a read transfer from the renderbuffer's texture */ - trans = pipe_get_transfer(pipe, strb->texture, - 0, 0, - PIPE_TRANSFER_READ, - x, y, width, height); - - /* determine bottom-to-top vs. top-to-bottom order */ - if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { - y = height - 1; - yStep = -1; - } - else { - y = 0; - yStep = 1; - } - - /* possibly convert sRGB format to linear RGB format */ - pformat = util_format_linear(trans->resource->format); - - if (ST_DEBUG & DEBUG_FALLBACK) - debug_printf("%s: fallback processing\n", __FUNCTION__); - - /* - * Copy pixels from pipe_transfer to user memory - */ - { - /* dest of first pixel in client memory */ - GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width, - height, format, type, 0, 0); - /* dest row stride */ - const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width, - format, type); - - if (pformat == PIPE_FORMAT_Z24_UNORM_S8_USCALED || - pformat == PIPE_FORMAT_Z24X8_UNORM) { - if (format == GL_DEPTH_COMPONENT) { - for (i = 0; i < height; i++) { - GLuint ztemp[MAX_WIDTH]; - GLfloat zfloat[MAX_WIDTH]; - const double scale = 1.0 / ((1 << 24) - 1); - pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0); - y += yStep; - for (j = 0; j < width; j++) { - zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff)); - } - _mesa_pack_depth_span(ctx, width, dst, type, - zfloat, &clippedPacking); - dst += dstStride; - } - } - else { - /* XXX: unreachable code -- should be before st_read_stencil_pixels */ - assert(format == GL_DEPTH_STENCIL_EXT); - for (i = 0; i < height; i++) { - GLuint *zshort = (GLuint *)dst; - pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0); - y += yStep; - /* Reverse into 24/8 */ - for (j = 0; j < width; j++) { - zshort[j] = (zshort[j] << 8) | (zshort[j] >> 24); - } - dst += dstStride; - } - } - } - else if (pformat == PIPE_FORMAT_S8_USCALED_Z24_UNORM || - pformat == PIPE_FORMAT_X8Z24_UNORM) { - if (format == GL_DEPTH_COMPONENT) { - for (i = 0; i < height; i++) { - GLuint ztemp[MAX_WIDTH]; - GLfloat zfloat[MAX_WIDTH]; - const double scale = 1.0 / ((1 << 24) - 1); - pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0); - y += yStep; - for (j = 0; j < width; j++) { - zfloat[j] = (float) (scale * ((ztemp[j] >> 8) & 0xffffff)); - } - _mesa_pack_depth_span(ctx, width, dst, type, - zfloat, &clippedPacking); - dst += dstStride; - } - } - else { - /* XXX: unreachable code -- should be before st_read_stencil_pixels */ - assert(format == GL_DEPTH_STENCIL_EXT); - for (i = 0; i < height; i++) { - pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0); - y += yStep; - dst += dstStride; - } - } - } - else if (pformat == PIPE_FORMAT_Z16_UNORM) { - for (i = 0; i < height; i++) { - GLushort ztemp[MAX_WIDTH]; - GLfloat zfloat[MAX_WIDTH]; - const double scale = 1.0 / 0xffff; - pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0); - y += yStep; - for (j = 0; j < width; j++) { - zfloat[j] = (float) (scale * ztemp[j]); - } - _mesa_pack_depth_span(ctx, width, dst, type, - zfloat, &clippedPacking); - dst += dstStride; - } - } - else if (pformat == PIPE_FORMAT_Z32_UNORM) { - for (i = 0; i < height; i++) { - GLuint ztemp[MAX_WIDTH]; - GLfloat zfloat[MAX_WIDTH]; - const double scale = 1.0 / 0xffffffff; - pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0); - y += yStep; - for (j = 0; j < width; j++) { - zfloat[j] = (float) (scale * ztemp[j]); - } - _mesa_pack_depth_span(ctx, width, dst, type, - zfloat, &clippedPacking); - dst += dstStride; - } - } - else { - /* RGBA format */ - /* Do a row at a time to flip image data vertically */ - for (i = 0; i < height; i++) { - pipe_get_tile_rgba_format(pipe, trans, 0, y, width, 1, - pformat, df); - y += yStep; - df += dfStride; - if (!dfStride) { - _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst, - &clippedPacking, transferOps); - dst += dstStride; - } - } - } - } - - free(temp); - - pipe->transfer_destroy(pipe, trans); - - _mesa_unmap_pbo_dest(ctx, &clippedPacking); -} - - -void st_init_readpixels_functions(struct dd_function_table *functions) -{ - functions->ReadPixels = st_readpixels; -} +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+/**
+ * glReadPixels interface to pipe
+ *
+ * \author Brian Paul
+ */
+
+
+#include "main/imports.h"
+#include "main/bufferobj.h"
+#include "main/context.h"
+#include "main/image.h"
+#include "main/pack.h"
+#include "main/pbo.h"
+
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "util/u_format.h"
+#include "util/u_inlines.h"
+#include "util/u_tile.h"
+
+#include "st_debug.h"
+#include "st_context.h"
+#include "st_atom.h"
+#include "st_cb_bitmap.h"
+#include "st_cb_readpixels.h"
+#include "st_cb_fbo.h"
+
+/**
+ * Special case for reading stencil buffer.
+ * For color/depth we use get_tile(). For stencil, map the stencil buffer.
+ */
+void
+st_read_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
+ GLsizei width, GLsizei height,
+ GLenum format, GLenum type,
+ const struct gl_pixelstore_attrib *packing,
+ GLvoid *pixels)
+{
+ struct gl_framebuffer *fb = ctx->ReadBuffer;
+ struct pipe_context *pipe = st_context(ctx)->pipe;
+ struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer);
+ struct pipe_transfer *pt;
+ ubyte *stmap;
+ GLint j;
+
+ if (strb->Base.Wrapped) {
+ strb = st_renderbuffer(strb->Base.Wrapped);
+ }
+
+ if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
+ y = ctx->DrawBuffer->Height - y - height;
+ }
+
+ /* Create a read transfer from the renderbuffer's texture */
+
+ pt = pipe_get_transfer(pipe, strb->texture,
+ 0, 0,
+ PIPE_TRANSFER_READ,
+ x, y, width, height);
+
+ /* map the stencil buffer */
+ stmap = pipe_transfer_map(pipe, pt);
+
+ /* width should never be > MAX_WIDTH since we did clipping earlier */
+ ASSERT(width <= MAX_WIDTH);
+
+ /* process image row by row */
+ for (j = 0; j < height; j++) {
+ GLvoid *dest;
+ GLstencil sValues[MAX_WIDTH];
+ GLfloat zValues[MAX_WIDTH];
+ GLint srcY;
+
+ if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
+ srcY = height - j - 1;
+ }
+ else {
+ srcY = j;
+ }
+
+ /* get stencil (and Z) values */
+ switch (pt->resource->format) {
+ case PIPE_FORMAT_S8_USCALED:
+ {
+ const ubyte *src = stmap + srcY * pt->stride;
+ memcpy(sValues, src, width);
+ }
+ break;
+ case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
+ if (format == GL_DEPTH_STENCIL) {
+ const uint *src = (uint *) (stmap + srcY * pt->stride);
+ const GLfloat scale = 1.0f / (0xffffff);
+ GLint k;
+ for (k = 0; k < width; k++) {
+ sValues[k] = src[k] >> 24;
+ zValues[k] = (src[k] & 0xffffff) * scale;
+ }
+ }
+ else {
+ const uint *src = (uint *) (stmap + srcY * pt->stride);
+ GLint k;
+ for (k = 0; k < width; k++) {
+ sValues[k] = src[k] >> 24;
+ }
+ }
+ break;
+ case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
+ if (format == GL_DEPTH_STENCIL) {
+ const uint *src = (uint *) (stmap + srcY * pt->stride);
+ const GLfloat scale = 1.0f / (0xffffff);
+ GLint k;
+ for (k = 0; k < width; k++) {
+ sValues[k] = src[k] & 0xff;
+ zValues[k] = (src[k] >> 8) * scale;
+ }
+ }
+ else {
+ const uint *src = (uint *) (stmap + srcY * pt->stride);
+ GLint k;
+ for (k = 0; k < width; k++) {
+ sValues[k] = src[k] & 0xff;
+ }
+ }
+ break;
+ default:
+ assert(0);
+ }
+
+ /* store */
+ dest = _mesa_image_address2d(packing, pixels, width, height,
+ format, type, j, 0);
+ if (format == GL_DEPTH_STENCIL) {
+ _mesa_pack_depth_stencil_span(ctx, width, dest,
+ zValues, sValues, packing);
+ }
+ else {
+ _mesa_pack_stencil_span(ctx, width, type, dest, sValues, packing);
+ }
+ }
+
+ /* unmap the stencil buffer */
+ pipe_transfer_unmap(pipe, pt);
+ pipe->transfer_destroy(pipe, pt);
+}
+
+
+/**
+ * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
+ * commands.
+ */
+struct st_renderbuffer *
+st_get_color_read_renderbuffer(struct gl_context *ctx)
+{
+ struct gl_framebuffer *fb = ctx->ReadBuffer;
+ struct st_renderbuffer *strb =
+ st_renderbuffer(fb->_ColorReadBuffer);
+
+ return strb;
+}
+
+
+/**
+ * Try to do glReadPixels in a fast manner for common cases.
+ * \return GL_TRUE for success, GL_FALSE for failure
+ */
+static GLboolean
+st_fast_readpixels(struct gl_context *ctx, struct st_renderbuffer *strb,
+ GLint x, GLint y, GLsizei width, GLsizei height,
+ GLenum format, GLenum type,
+ const struct gl_pixelstore_attrib *pack,
+ GLvoid *dest)
+{
+ enum combination {
+ A8R8G8B8_UNORM_TO_RGBA_UBYTE,
+ A8R8G8B8_UNORM_TO_RGB_UBYTE,
+ A8R8G8B8_UNORM_TO_BGRA_UINT
+ } combo;
+
+ if (ctx->_ImageTransferState)
+ return GL_FALSE;
+
+ if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
+ format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
+ combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE;
+ }
+ else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
+ format == GL_RGB && type == GL_UNSIGNED_BYTE) {
+ combo = A8R8G8B8_UNORM_TO_RGB_UBYTE;
+ }
+ else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
+ format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) {
+ combo = A8R8G8B8_UNORM_TO_BGRA_UINT;
+ }
+ else {
+ return GL_FALSE;
+ }
+
+ /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
+
+ {
+ struct pipe_context *pipe = st_context(ctx)->pipe;
+ struct pipe_transfer *trans;
+ const GLubyte *map;
+ GLubyte *dst;
+ GLint row, col, dy, dstStride;
+
+ if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+ /* convert GL Y to Gallium Y */
+ y = strb->texture->height0 - y - height;
+ }
+
+ trans = pipe_get_transfer(pipe, strb->texture,
+ 0, 0,
+ PIPE_TRANSFER_READ,
+ x, y, width, height);
+ if (!trans) {
+ return GL_FALSE;
+ }
+
+ map = pipe_transfer_map(pipe, trans);
+ if (!map) {
+ pipe->transfer_destroy(pipe, trans);
+ return GL_FALSE;
+ }
+
+ /* We always write to the user/dest buffer from low addr to high addr
+ * but the read order depends on renderbuffer orientation
+ */
+ if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+ /* read source rows from bottom to top */
+ y = height - 1;
+ dy = -1;
+ }
+ else {
+ /* read source rows from top to bottom */
+ y = 0;
+ dy = 1;
+ }
+
+ dst = _mesa_image_address2d(pack, dest, width, height,
+ format, type, 0, 0);
+ dstStride = _mesa_image_row_stride(pack, width, format, type);
+
+ switch (combo) {
+ case A8R8G8B8_UNORM_TO_RGBA_UBYTE:
+ for (row = 0; row < height; row++) {
+ const GLubyte *src = map + y * trans->stride;
+ for (col = 0; col < width; col++) {
+ GLuint pixel = ((GLuint *) src)[col];
+ dst[col*4+0] = (pixel >> 16) & 0xff;
+ dst[col*4+1] = (pixel >> 8) & 0xff;
+ dst[col*4+2] = (pixel >> 0) & 0xff;
+ dst[col*4+3] = (pixel >> 24) & 0xff;
+ }
+ dst += dstStride;
+ y += dy;
+ }
+ break;
+ case A8R8G8B8_UNORM_TO_RGB_UBYTE:
+ for (row = 0; row < height; row++) {
+ const GLubyte *src = map + y * trans->stride;
+ for (col = 0; col < width; col++) {
+ GLuint pixel = ((GLuint *) src)[col];
+ dst[col*3+0] = (pixel >> 16) & 0xff;
+ dst[col*3+1] = (pixel >> 8) & 0xff;
+ dst[col*3+2] = (pixel >> 0) & 0xff;
+ }
+ dst += dstStride;
+ y += dy;
+ }
+ break;
+ case A8R8G8B8_UNORM_TO_BGRA_UINT:
+ for (row = 0; row < height; row++) {
+ const GLubyte *src = map + y * trans->stride;
+ memcpy(dst, src, 4 * width);
+ dst += dstStride;
+ y += dy;
+ }
+ break;
+ default:
+ ; /* nothing */
+ }
+
+ pipe_transfer_unmap(pipe, trans);
+ pipe->transfer_destroy(pipe, trans);
+ }
+
+ return GL_TRUE;
+}
+
+
+/**
+ * Do glReadPixels by getting rows from the framebuffer transfer with
+ * get_tile(). Convert to requested format/type with Mesa image routines.
+ * Image transfer ops are done in software too.
+ */
+static void
+st_readpixels(struct gl_context *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
+ GLenum format, GLenum type,
+ const struct gl_pixelstore_attrib *pack,
+ GLvoid *dest)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ GLfloat (*temp)[4];
+ const GLbitfield transferOps = ctx->_ImageTransferState;
+ GLsizei i, j;
+ GLint yStep, dfStride;
+ GLfloat *df;
+ struct st_renderbuffer *strb;
+ struct gl_pixelstore_attrib clippedPacking = *pack;
+ struct pipe_transfer *trans;
+ enum pipe_format pformat;
+
+ assert(ctx->ReadBuffer->Width > 0);
+
+ st_validate_state(st);
+
+ /* Do all needed clipping here, so that we can forget about it later */
+ if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
+ /* The ReadPixels transfer is totally outside the window bounds */
+ return;
+ }
+
+ st_flush_bitmap_cache(st);
+
+ dest = _mesa_map_pbo_dest(ctx, &clippedPacking, dest);
+ if (!dest)
+ return;
+
+ if (format == GL_STENCIL_INDEX ||
+ format == GL_DEPTH_STENCIL) {
+ st_read_stencil_pixels(ctx, x, y, width, height,
+ format, type, pack, dest);
+ return;
+ }
+ else if (format == GL_DEPTH_COMPONENT) {
+ strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
+ if (strb->Base.Wrapped) {
+ strb = st_renderbuffer(strb->Base.Wrapped);
+ }
+ }
+ else {
+ /* Read color buffer */
+ strb = st_get_color_read_renderbuffer(ctx);
+ }
+
+ if (!strb)
+ return;
+
+ /* try a fast-path readpixels before anything else */
+ if (st_fast_readpixels(ctx, strb, x, y, width, height,
+ format, type, pack, dest)) {
+ /* success! */
+ _mesa_unmap_pbo_dest(ctx, &clippedPacking);
+ return;
+ }
+
+ /* allocate temp pixel row buffer */
+ temp = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
+ if (!temp) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
+ return;
+ }
+
+ if (format == GL_RGBA && type == GL_FLOAT) {
+ /* write tile(row) directly into user's buffer */
+ df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
+ height, format, type, 0, 0);
+ dfStride = width * 4;
+ }
+ else {
+ /* write tile(row) into temp row buffer */
+ df = (GLfloat *) temp;
+ dfStride = 0;
+ }
+
+ if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+ /* convert GL Y to Gallium Y */
+ y = strb->Base.Height - y - height;
+ }
+
+ /* Create a read transfer from the renderbuffer's texture */
+ trans = pipe_get_transfer(pipe, strb->texture,
+ 0, 0,
+ PIPE_TRANSFER_READ,
+ x, y, width, height);
+
+ /* determine bottom-to-top vs. top-to-bottom order */
+ if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+ y = height - 1;
+ yStep = -1;
+ }
+ else {
+ y = 0;
+ yStep = 1;
+ }
+
+ /* possibly convert sRGB format to linear RGB format */
+ pformat = util_format_linear(trans->resource->format);
+
+ if (ST_DEBUG & DEBUG_FALLBACK)
+ debug_printf("%s: fallback processing\n", __FUNCTION__);
+
+ /*
+ * Copy pixels from pipe_transfer to user memory
+ */
+ {
+ /* dest of first pixel in client memory */
+ GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
+ height, format, type, 0, 0);
+ /* dest row stride */
+ const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
+ format, type);
+
+ if (pformat == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
+ pformat == PIPE_FORMAT_Z24X8_UNORM) {
+ if (format == GL_DEPTH_COMPONENT) {
+ for (i = 0; i < height; i++) {
+ GLuint ztemp[MAX_WIDTH];
+ GLfloat zfloat[MAX_WIDTH];
+ const double scale = 1.0 / ((1 << 24) - 1);
+ pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
+ y += yStep;
+ for (j = 0; j < width; j++) {
+ zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
+ }
+ _mesa_pack_depth_span(ctx, width, dst, type,
+ zfloat, &clippedPacking);
+ dst += dstStride;
+ }
+ }
+ else {
+ /* XXX: unreachable code -- should be before st_read_stencil_pixels */
+ assert(format == GL_DEPTH_STENCIL_EXT);
+ for (i = 0; i < height; i++) {
+ GLuint *zshort = (GLuint *)dst;
+ pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
+ y += yStep;
+ /* Reverse into 24/8 */
+ for (j = 0; j < width; j++) {
+ zshort[j] = (zshort[j] << 8) | (zshort[j] >> 24);
+ }
+ dst += dstStride;
+ }
+ }
+ }
+ else if (pformat == PIPE_FORMAT_S8_USCALED_Z24_UNORM ||
+ pformat == PIPE_FORMAT_X8Z24_UNORM) {
+ if (format == GL_DEPTH_COMPONENT) {
+ for (i = 0; i < height; i++) {
+ GLuint ztemp[MAX_WIDTH];
+ GLfloat zfloat[MAX_WIDTH];
+ const double scale = 1.0 / ((1 << 24) - 1);
+ pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
+ y += yStep;
+ for (j = 0; j < width; j++) {
+ zfloat[j] = (float) (scale * ((ztemp[j] >> 8) & 0xffffff));
+ }
+ _mesa_pack_depth_span(ctx, width, dst, type,
+ zfloat, &clippedPacking);
+ dst += dstStride;
+ }
+ }
+ else {
+ /* XXX: unreachable code -- should be before st_read_stencil_pixels */
+ assert(format == GL_DEPTH_STENCIL_EXT);
+ for (i = 0; i < height; i++) {
+ pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
+ y += yStep;
+ dst += dstStride;
+ }
+ }
+ }
+ else if (pformat == PIPE_FORMAT_Z16_UNORM) {
+ for (i = 0; i < height; i++) {
+ GLushort ztemp[MAX_WIDTH];
+ GLfloat zfloat[MAX_WIDTH];
+ const double scale = 1.0 / 0xffff;
+ pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
+ y += yStep;
+ for (j = 0; j < width; j++) {
+ zfloat[j] = (float) (scale * ztemp[j]);
+ }
+ _mesa_pack_depth_span(ctx, width, dst, type,
+ zfloat, &clippedPacking);
+ dst += dstStride;
+ }
+ }
+ else if (pformat == PIPE_FORMAT_Z32_UNORM) {
+ for (i = 0; i < height; i++) {
+ GLuint ztemp[MAX_WIDTH];
+ GLfloat zfloat[MAX_WIDTH];
+ const double scale = 1.0 / 0xffffffff;
+ pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
+ y += yStep;
+ for (j = 0; j < width; j++) {
+ zfloat[j] = (float) (scale * ztemp[j]);
+ }
+ _mesa_pack_depth_span(ctx, width, dst, type,
+ zfloat, &clippedPacking);
+ dst += dstStride;
+ }
+ }
+ else {
+ /* RGBA format */
+ /* Do a row at a time to flip image data vertically */
+ for (i = 0; i < height; i++) {
+ pipe_get_tile_rgba_format(pipe, trans, 0, y, width, 1,
+ pformat, df);
+ y += yStep;
+ df += dfStride;
+ if (!dfStride) {
+ _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
+ &clippedPacking, transferOps);
+ dst += dstStride;
+ }
+ }
+ }
+ }
+
+ free(temp);
+
+ pipe->transfer_destroy(pipe, trans);
+
+ _mesa_unmap_pbo_dest(ctx, &clippedPacking);
+}
+
+
+void st_init_readpixels_functions(struct dd_function_table *functions)
+{
+ functions->ReadPixels = st_readpixels;
+}
diff --git a/mesalib/src/mesa/state_tracker/st_context.h b/mesalib/src/mesa/state_tracker/st_context.h index 77765f023..ef54fd7cd 100644 --- a/mesalib/src/mesa/state_tracker/st_context.h +++ b/mesalib/src/mesa/state_tracker/st_context.h @@ -1,270 +1,270 @@ -/************************************************************************** - * - * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - -#ifndef ST_CONTEXT_H -#define ST_CONTEXT_H - -#include "main/mtypes.h" -#include "pipe/p_state.h" -#include "state_tracker/st_api.h" - -struct bitmap_cache; -struct blit_state; -struct dd_function_table; -struct draw_context; -struct draw_stage; -struct gen_mipmap_state; -struct st_context; -struct st_fragment_program; - - -#define ST_NEW_MESA 0x1 /* Mesa state has changed */ -#define ST_NEW_FRAGMENT_PROGRAM 0x2 -#define ST_NEW_VERTEX_PROGRAM 0x4 -#define ST_NEW_FRAMEBUFFER 0x8 -#define ST_NEW_EDGEFLAGS_DATA 0x10 -#define ST_NEW_GEOMETRY_PROGRAM 0x20 - - -struct st_state_flags { - GLuint mesa; - GLuint st; -}; - -struct st_tracked_state { - const char *name; - struct st_state_flags dirty; - void (*update)( struct st_context *st ); -}; - - - -struct st_context -{ - struct st_context_iface iface; - - struct gl_context *ctx; - - struct pipe_context *pipe; - - struct draw_context *draw; /**< For selection/feedback/rastpos only */ - struct draw_stage *feedback_stage; /**< For GL_FEEDBACK rendermode */ - struct draw_stage *selection_stage; /**< For GL_SELECT rendermode */ - struct draw_stage *rastpos_stage; /**< For glRasterPos */ - - - /* On old libGL's for linux we need to invalidate the drawables - * on glViewpport calls, this is set via a option. - */ - boolean invalidate_on_gl_viewport; - - /* Some state is contained in constant objects. - * Other state is just parameter values. - */ - struct { - struct pipe_blend_state blend; - struct pipe_depth_stencil_alpha_state depth_stencil; - struct pipe_rasterizer_state rasterizer; - struct pipe_sampler_state samplers[PIPE_MAX_SAMPLERS]; - struct pipe_sampler_state *sampler_list[PIPE_MAX_SAMPLERS]; - struct pipe_clip_state clip; - struct { - void *ptr; - unsigned size; - } constants[PIPE_SHADER_TYPES]; - struct pipe_framebuffer_state framebuffer; - struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS]; - struct pipe_scissor_state scissor; - struct pipe_viewport_state viewport; - unsigned sample_mask; - - GLuint num_samplers; - GLuint num_textures; - - GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */ - } state; - - char vendor[100]; - char renderer[100]; - - struct st_state_flags dirty; - - GLboolean missing_textures; - GLboolean vertdata_edgeflags; - - /** Mapping from VERT_RESULT_x to post-transformed vertex slot */ - const GLuint *vertex_result_to_slot; - - struct st_vertex_program *vp; /**< Currently bound vertex program */ - struct st_fragment_program *fp; /**< Currently bound fragment program */ - struct st_geometry_program *gp; /**< Currently bound geometry program */ - - struct st_vp_variant *vp_variant; - struct st_fp_variant *fp_variant; - struct st_gp_variant *gp_variant; - - struct gl_texture_object *default_texture; - - struct { - struct gl_program_cache *cache; - struct st_fragment_program *program; /**< cur pixel transfer prog */ - GLuint xfer_prog_sn; /**< pixel xfer program serial no. */ - GLuint user_prog_sn; /**< user fragment program serial no. */ - struct st_fragment_program *combined_prog; - GLuint combined_prog_sn; - struct pipe_resource *pixelmap_texture; - struct pipe_sampler_view *pixelmap_sampler_view; - boolean pixelmap_enabled; /**< use the pixelmap texture? */ - } pixel_xfer; - - /** for glBitmap */ - struct { - struct pipe_rasterizer_state rasterizer; - struct pipe_sampler_state samplers[2]; - enum pipe_format tex_format; - void *vs; - float vertices[4][3][4]; /**< vertex pos + color + texcoord */ - struct pipe_resource *vbuf; - unsigned vbuf_slot; /* next free slot in vbuf */ - struct bitmap_cache *cache; - } bitmap; - - /** for glDraw/CopyPixels */ - struct { - struct gl_fragment_program *shaders[4]; - void *vert_shaders[2]; /**< ureg shaders */ - } drawpix; - - /** for glClear */ - struct { - struct pipe_rasterizer_state raster; - struct pipe_viewport_state viewport; - struct pipe_clip_state clip; - void *vs; - void *fs; - float vertices[4][2][4]; /**< vertex pos + color */ - struct pipe_resource *vbuf; - unsigned vbuf_slot; - boolean enable_ds_separate; - } clear; - - /** used for anything using util_draw_vertex_buffer */ - struct pipe_vertex_element velems_util_draw[3]; - - void *passthrough_fs; /**< simple pass-through frag shader */ - - enum pipe_texture_target internal_target; - struct gen_mipmap_state *gen_mipmap; - struct blit_state *blit; - - struct cso_context *cso_context; - - int force_msaa; - void *winsys_drawable_handle; - - /* User vertex buffers. */ - struct pipe_resource *user_vb[PIPE_MAX_ATTRIBS]; - unsigned user_vb_stride[PIPE_MAX_ATTRIBS]; - unsigned num_user_vbs; -}; - - -/* Need this so that we can implement Mesa callbacks in this module. - */ -static INLINE struct st_context *st_context(struct gl_context *ctx) -{ - return ctx->st; -} - - -/** - * Wrapper for struct gl_framebuffer. - * This is an opaque type to the outside world. - */ -struct st_framebuffer -{ - struct gl_framebuffer Base; - void *Private; - - struct st_framebuffer_iface *iface; - enum st_attachment_type statts[ST_ATTACHMENT_COUNT]; - unsigned num_statts; - int32_t revalidate; -}; - - -extern void st_init_driver_functions(struct dd_function_table *functions); - -void st_invalidate_state(struct gl_context * ctx, GLuint new_state); - - - -#define Y_0_TOP 1 -#define Y_0_BOTTOM 2 - -static INLINE GLuint -st_fb_orientation(const struct gl_framebuffer *fb) -{ - if (fb && fb->Name == 0) { - /* Drawing into a window (on-screen buffer). - * - * Negate Y scale to flip image vertically. - * The NDC Y coords prior to viewport transformation are in the range - * [y=-1=bottom, y=1=top] - * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where - * H is the window height. - * Use the viewport transformation to invert Y. - */ - return Y_0_TOP; - } - else { - /* Drawing into user-created FBO (very likely a texture). - * - * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering. - */ - return Y_0_BOTTOM; - } -} - - -/** clear-alloc a struct-sized object, with casting */ -#define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T)) - - -extern int -st_get_msaa(void); - -extern struct st_context * -st_create_context(gl_api api, struct pipe_context *pipe, - const struct gl_config *visual, - struct st_context *share); - -extern void -st_destroy_context(struct st_context *st); - - -#endif +/**************************************************************************
+ *
+ * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#ifndef ST_CONTEXT_H
+#define ST_CONTEXT_H
+
+#include "main/mtypes.h"
+#include "pipe/p_state.h"
+#include "state_tracker/st_api.h"
+
+struct bitmap_cache;
+struct blit_state;
+struct dd_function_table;
+struct draw_context;
+struct draw_stage;
+struct gen_mipmap_state;
+struct st_context;
+struct st_fragment_program;
+
+
+#define ST_NEW_MESA 0x1 /* Mesa state has changed */
+#define ST_NEW_FRAGMENT_PROGRAM 0x2
+#define ST_NEW_VERTEX_PROGRAM 0x4
+#define ST_NEW_FRAMEBUFFER 0x8
+#define ST_NEW_EDGEFLAGS_DATA 0x10
+#define ST_NEW_GEOMETRY_PROGRAM 0x20
+
+
+struct st_state_flags {
+ GLuint mesa;
+ GLuint st;
+};
+
+struct st_tracked_state {
+ const char *name;
+ struct st_state_flags dirty;
+ void (*update)( struct st_context *st );
+};
+
+
+
+struct st_context
+{
+ struct st_context_iface iface;
+
+ struct gl_context *ctx;
+
+ struct pipe_context *pipe;
+
+ struct draw_context *draw; /**< For selection/feedback/rastpos only */
+ struct draw_stage *feedback_stage; /**< For GL_FEEDBACK rendermode */
+ struct draw_stage *selection_stage; /**< For GL_SELECT rendermode */
+ struct draw_stage *rastpos_stage; /**< For glRasterPos */
+
+
+ /* On old libGL's for linux we need to invalidate the drawables
+ * on glViewpport calls, this is set via a option.
+ */
+ boolean invalidate_on_gl_viewport;
+
+ /* Some state is contained in constant objects.
+ * Other state is just parameter values.
+ */
+ struct {
+ struct pipe_blend_state blend;
+ struct pipe_depth_stencil_alpha_state depth_stencil;
+ struct pipe_rasterizer_state rasterizer;
+ struct pipe_sampler_state samplers[PIPE_MAX_SAMPLERS];
+ struct pipe_sampler_state *sampler_list[PIPE_MAX_SAMPLERS];
+ struct pipe_clip_state clip;
+ struct {
+ void *ptr;
+ unsigned size;
+ } constants[PIPE_SHADER_TYPES];
+ struct pipe_framebuffer_state framebuffer;
+ struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
+ struct pipe_scissor_state scissor;
+ struct pipe_viewport_state viewport;
+ unsigned sample_mask;
+
+ GLuint num_samplers;
+ GLuint num_textures;
+
+ GLuint poly_stipple[32]; /**< In OpenGL's bottom-to-top order */
+ } state;
+
+ char vendor[100];
+ char renderer[100];
+
+ struct st_state_flags dirty;
+
+ GLboolean missing_textures;
+ GLboolean vertdata_edgeflags;
+
+ /** Mapping from VERT_RESULT_x to post-transformed vertex slot */
+ const GLuint *vertex_result_to_slot;
+
+ struct st_vertex_program *vp; /**< Currently bound vertex program */
+ struct st_fragment_program *fp; /**< Currently bound fragment program */
+ struct st_geometry_program *gp; /**< Currently bound geometry program */
+
+ struct st_vp_variant *vp_variant;
+ struct st_fp_variant *fp_variant;
+ struct st_gp_variant *gp_variant;
+
+ struct gl_texture_object *default_texture;
+
+ struct {
+ struct gl_program_cache *cache;
+ struct st_fragment_program *program; /**< cur pixel transfer prog */
+ GLuint xfer_prog_sn; /**< pixel xfer program serial no. */
+ GLuint user_prog_sn; /**< user fragment program serial no. */
+ struct st_fragment_program *combined_prog;
+ GLuint combined_prog_sn;
+ struct pipe_resource *pixelmap_texture;
+ struct pipe_sampler_view *pixelmap_sampler_view;
+ boolean pixelmap_enabled; /**< use the pixelmap texture? */
+ } pixel_xfer;
+
+ /** for glBitmap */
+ struct {
+ struct pipe_rasterizer_state rasterizer;
+ struct pipe_sampler_state samplers[2];
+ enum pipe_format tex_format;
+ void *vs;
+ float vertices[4][3][4]; /**< vertex pos + color + texcoord */
+ struct pipe_resource *vbuf;
+ unsigned vbuf_slot; /* next free slot in vbuf */
+ struct bitmap_cache *cache;
+ } bitmap;
+
+ /** for glDraw/CopyPixels */
+ struct {
+ struct gl_fragment_program *shaders[4];
+ void *vert_shaders[2]; /**< ureg shaders */
+ } drawpix;
+
+ /** for glClear */
+ struct {
+ struct pipe_rasterizer_state raster;
+ struct pipe_viewport_state viewport;
+ struct pipe_clip_state clip;
+ void *vs;
+ void *fs;
+ float vertices[4][2][4]; /**< vertex pos + color */
+ struct pipe_resource *vbuf;
+ unsigned vbuf_slot;
+ boolean enable_ds_separate;
+ } clear;
+
+ /** used for anything using util_draw_vertex_buffer */
+ struct pipe_vertex_element velems_util_draw[3];
+
+ void *passthrough_fs; /**< simple pass-through frag shader */
+
+ enum pipe_texture_target internal_target;
+ struct gen_mipmap_state *gen_mipmap;
+ struct blit_state *blit;
+
+ struct cso_context *cso_context;
+
+ int force_msaa;
+ void *winsys_drawable_handle;
+
+ /* User vertex buffers. */
+ struct pipe_resource *user_vb[PIPE_MAX_ATTRIBS];
+ unsigned user_vb_stride[PIPE_MAX_ATTRIBS];
+ unsigned num_user_vbs;
+};
+
+
+/* Need this so that we can implement Mesa callbacks in this module.
+ */
+static INLINE struct st_context *st_context(struct gl_context *ctx)
+{
+ return ctx->st;
+}
+
+
+/**
+ * Wrapper for struct gl_framebuffer.
+ * This is an opaque type to the outside world.
+ */
+struct st_framebuffer
+{
+ struct gl_framebuffer Base;
+ void *Private;
+
+ struct st_framebuffer_iface *iface;
+ enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
+ unsigned num_statts;
+ int32_t revalidate;
+};
+
+
+extern void st_init_driver_functions(struct dd_function_table *functions);
+
+void st_invalidate_state(struct gl_context * ctx, GLuint new_state);
+
+
+
+#define Y_0_TOP 1
+#define Y_0_BOTTOM 2
+
+static INLINE GLuint
+st_fb_orientation(const struct gl_framebuffer *fb)
+{
+ if (fb && fb->Name == 0) {
+ /* Drawing into a window (on-screen buffer).
+ *
+ * Negate Y scale to flip image vertically.
+ * The NDC Y coords prior to viewport transformation are in the range
+ * [y=-1=bottom, y=1=top]
+ * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where
+ * H is the window height.
+ * Use the viewport transformation to invert Y.
+ */
+ return Y_0_TOP;
+ }
+ else {
+ /* Drawing into user-created FBO (very likely a texture).
+ *
+ * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering.
+ */
+ return Y_0_BOTTOM;
+ }
+}
+
+
+/** clear-alloc a struct-sized object, with casting */
+#define ST_CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
+
+
+extern int
+st_get_msaa(void);
+
+extern struct st_context *
+st_create_context(gl_api api, struct pipe_context *pipe,
+ const struct gl_config *visual,
+ struct st_context *share);
+
+extern void
+st_destroy_context(struct st_context *st);
+
+
+#endif
diff --git a/mesalib/src/mesa/state_tracker/st_draw.c b/mesalib/src/mesa/state_tracker/st_draw.c index 40afa4362..2f4027bc4 100644 --- a/mesalib/src/mesa/state_tracker/st_draw.c +++ b/mesalib/src/mesa/state_tracker/st_draw.c @@ -1,773 +1,773 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - -/* - * This file implements the st_draw_vbo() function which is called from - * Mesa's VBO module. All point/line/triangle rendering is done through - * this function whether the user called glBegin/End, glDrawArrays, - * glDrawElements, glEvalMesh, or glCalList, etc. - * - * We basically convert the VBO's vertex attribute/array information into - * Gallium vertex state, bind the vertex buffer objects and call - * pipe->draw_elements(), pipe->draw_range_elements() or pipe->draw_arrays(). - * - * Authors: - * Keith Whitwell <keith@tungstengraphics.com> - */ - - -#include "main/imports.h" -#include "main/image.h" -#include "main/macros.h" -#include "main/mfeatures.h" -#include "program/prog_uniform.h" - -#include "vbo/vbo.h" - -#include "st_context.h" -#include "st_atom.h" -#include "st_cb_bufferobjects.h" -#include "st_draw.h" -#include "st_program.h" - -#include "pipe/p_context.h" -#include "pipe/p_defines.h" -#include "util/u_inlines.h" -#include "util/u_format.h" -#include "util/u_prim.h" -#include "util/u_draw_quad.h" -#include "draw/draw_context.h" -#include "cso_cache/cso_context.h" - - -static GLuint double_types[4] = { - PIPE_FORMAT_R64_FLOAT, - PIPE_FORMAT_R64G64_FLOAT, - PIPE_FORMAT_R64G64B64_FLOAT, - PIPE_FORMAT_R64G64B64A64_FLOAT -}; - -static GLuint float_types[4] = { - PIPE_FORMAT_R32_FLOAT, - PIPE_FORMAT_R32G32_FLOAT, - PIPE_FORMAT_R32G32B32_FLOAT, - PIPE_FORMAT_R32G32B32A32_FLOAT -}; - -static GLuint half_float_types[4] = { - PIPE_FORMAT_R16_FLOAT, - PIPE_FORMAT_R16G16_FLOAT, - PIPE_FORMAT_R16G16B16_FLOAT, - PIPE_FORMAT_R16G16B16A16_FLOAT -}; - -static GLuint uint_types_norm[4] = { - PIPE_FORMAT_R32_UNORM, - PIPE_FORMAT_R32G32_UNORM, - PIPE_FORMAT_R32G32B32_UNORM, - PIPE_FORMAT_R32G32B32A32_UNORM -}; - -static GLuint uint_types_scale[4] = { - PIPE_FORMAT_R32_USCALED, - PIPE_FORMAT_R32G32_USCALED, - PIPE_FORMAT_R32G32B32_USCALED, - PIPE_FORMAT_R32G32B32A32_USCALED -}; - -static GLuint int_types_norm[4] = { - PIPE_FORMAT_R32_SNORM, - PIPE_FORMAT_R32G32_SNORM, - PIPE_FORMAT_R32G32B32_SNORM, - PIPE_FORMAT_R32G32B32A32_SNORM -}; - -static GLuint int_types_scale[4] = { - PIPE_FORMAT_R32_SSCALED, - PIPE_FORMAT_R32G32_SSCALED, - PIPE_FORMAT_R32G32B32_SSCALED, - PIPE_FORMAT_R32G32B32A32_SSCALED -}; - -static GLuint ushort_types_norm[4] = { - PIPE_FORMAT_R16_UNORM, - PIPE_FORMAT_R16G16_UNORM, - PIPE_FORMAT_R16G16B16_UNORM, - PIPE_FORMAT_R16G16B16A16_UNORM -}; - -static GLuint ushort_types_scale[4] = { - PIPE_FORMAT_R16_USCALED, - PIPE_FORMAT_R16G16_USCALED, - PIPE_FORMAT_R16G16B16_USCALED, - PIPE_FORMAT_R16G16B16A16_USCALED -}; - -static GLuint short_types_norm[4] = { - PIPE_FORMAT_R16_SNORM, - PIPE_FORMAT_R16G16_SNORM, - PIPE_FORMAT_R16G16B16_SNORM, - PIPE_FORMAT_R16G16B16A16_SNORM -}; - -static GLuint short_types_scale[4] = { - PIPE_FORMAT_R16_SSCALED, - PIPE_FORMAT_R16G16_SSCALED, - PIPE_FORMAT_R16G16B16_SSCALED, - PIPE_FORMAT_R16G16B16A16_SSCALED -}; - -static GLuint ubyte_types_norm[4] = { - PIPE_FORMAT_R8_UNORM, - PIPE_FORMAT_R8G8_UNORM, - PIPE_FORMAT_R8G8B8_UNORM, - PIPE_FORMAT_R8G8B8A8_UNORM -}; - -static GLuint ubyte_types_scale[4] = { - PIPE_FORMAT_R8_USCALED, - PIPE_FORMAT_R8G8_USCALED, - PIPE_FORMAT_R8G8B8_USCALED, - PIPE_FORMAT_R8G8B8A8_USCALED -}; - -static GLuint byte_types_norm[4] = { - PIPE_FORMAT_R8_SNORM, - PIPE_FORMAT_R8G8_SNORM, - PIPE_FORMAT_R8G8B8_SNORM, - PIPE_FORMAT_R8G8B8A8_SNORM -}; - -static GLuint byte_types_scale[4] = { - PIPE_FORMAT_R8_SSCALED, - PIPE_FORMAT_R8G8_SSCALED, - PIPE_FORMAT_R8G8B8_SSCALED, - PIPE_FORMAT_R8G8B8A8_SSCALED -}; - -static GLuint fixed_types[4] = { - PIPE_FORMAT_R32_FIXED, - PIPE_FORMAT_R32G32_FIXED, - PIPE_FORMAT_R32G32B32_FIXED, - PIPE_FORMAT_R32G32B32A32_FIXED -}; - - - -/** - * Return a PIPE_FORMAT_x for the given GL datatype and size. - */ -GLuint -st_pipe_vertex_format(GLenum type, GLuint size, GLenum format, - GLboolean normalized) -{ - assert((type >= GL_BYTE && type <= GL_DOUBLE) || - type == GL_FIXED || type == GL_HALF_FLOAT); - assert(size >= 1); - assert(size <= 4); - assert(format == GL_RGBA || format == GL_BGRA); - - if (format == GL_BGRA) { - /* this is an odd-ball case */ - assert(type == GL_UNSIGNED_BYTE); - assert(normalized); - return PIPE_FORMAT_B8G8R8A8_UNORM; - } - - if (normalized) { - switch (type) { - case GL_DOUBLE: return double_types[size-1]; - case GL_FLOAT: return float_types[size-1]; - case GL_HALF_FLOAT: return half_float_types[size-1]; - case GL_INT: return int_types_norm[size-1]; - case GL_SHORT: return short_types_norm[size-1]; - case GL_BYTE: return byte_types_norm[size-1]; - case GL_UNSIGNED_INT: return uint_types_norm[size-1]; - case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1]; - case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1]; - case GL_FIXED: return fixed_types[size-1]; - default: assert(0); return 0; - } - } - else { - switch (type) { - case GL_DOUBLE: return double_types[size-1]; - case GL_FLOAT: return float_types[size-1]; - case GL_HALF_FLOAT: return half_float_types[size-1]; - case GL_INT: return int_types_scale[size-1]; - case GL_SHORT: return short_types_scale[size-1]; - case GL_BYTE: return byte_types_scale[size-1]; - case GL_UNSIGNED_INT: return uint_types_scale[size-1]; - case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1]; - case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1]; - case GL_FIXED: return fixed_types[size-1]; - default: assert(0); return 0; - } - } - return 0; /* silence compiler warning */ -} - - - - - -/** - * Examine the active arrays to determine if we have interleaved - * vertex arrays all living in one VBO, or all living in user space. - * \param userSpace returns whether the arrays are in user space. - */ -static GLboolean -is_interleaved_arrays(const struct st_vertex_program *vp, - const struct st_vp_variant *vpv, - const struct gl_client_array **arrays) -{ - GLuint attr; - const struct gl_buffer_object *firstBufObj = NULL; - GLint firstStride = -1; - const GLubyte *client_addr = NULL; - GLboolean user_memory; - - for (attr = 0; attr < vpv->num_inputs; attr++) { - const GLuint mesaAttr = vp->index_to_input[attr]; - const struct gl_buffer_object *bufObj = arrays[mesaAttr]->BufferObj; - const GLsizei stride = arrays[mesaAttr]->StrideB; /* in bytes */ - - if (firstStride < 0) { - firstStride = stride; - user_memory = !bufObj || !bufObj->Name; - } - else if (firstStride != stride) { - return GL_FALSE; - } - - if (!bufObj || !bufObj->Name) { - /* Try to detect if the client-space arrays are - * "close" to each other. - */ - if (!user_memory) { - return GL_FALSE; - } - if (!client_addr) { - client_addr = arrays[mesaAttr]->Ptr; - } - else if (abs(arrays[mesaAttr]->Ptr - client_addr) > firstStride) { - /* arrays start too far apart */ - return GL_FALSE; - } - } - else if (!firstBufObj) { - if (user_memory) { - return GL_FALSE; - } - firstBufObj = bufObj; - } - else if (bufObj != firstBufObj) { - return GL_FALSE; - } - } - - return GL_TRUE; -} - - -/** - * Set up for drawing interleaved arrays that all live in one VBO - * or all live in user space. - * \param vbuffer returns vertex buffer info - * \param velements returns vertex element info - */ -static void -setup_interleaved_attribs(struct gl_context *ctx, - const struct st_vertex_program *vp, - const struct st_vp_variant *vpv, - const struct gl_client_array **arrays, - struct pipe_vertex_buffer *vbuffer, - struct pipe_vertex_element velements[], - unsigned max_index, - unsigned num_instances) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - GLuint attr; - const GLubyte *low_addr = NULL; - - /* Find the lowest address. */ - if(vpv->num_inputs) { - low_addr = arrays[vp->index_to_input[0]]->Ptr; - - for (attr = 1; attr < vpv->num_inputs; attr++) { - const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr; - low_addr = MIN2(low_addr, start); - } - } - - for (attr = 0; attr < vpv->num_inputs; attr++) { - const GLuint mesaAttr = vp->index_to_input[attr]; - struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj; - struct st_buffer_object *stobj = st_buffer_object(bufobj); - GLsizei stride = arrays[mesaAttr]->StrideB; - - if (attr == 0) { - if (bufobj && bufobj->Name) { - vbuffer->buffer = NULL; - pipe_resource_reference(&vbuffer->buffer, stobj->buffer); - vbuffer->buffer_offset = pointer_to_offset(low_addr); - } else { - uint divisor = arrays[mesaAttr]->InstanceDivisor; - uint length = (divisor ? num_instances / divisor : max_index) + 1; - vbuffer->buffer = - pipe_user_buffer_create(pipe->screen, (void*)low_addr, - stride * length, - PIPE_BIND_VERTEX_BUFFER); - vbuffer->buffer_offset = 0; - - /* Track user vertex buffers. */ - pipe_resource_reference(&st->user_vb[0], vbuffer->buffer); - st->user_vb_stride[0] = stride; - st->num_user_vbs = 1; - } - vbuffer->stride = stride; /* in bytes */ - } - - velements[attr].src_offset = - (unsigned) (arrays[mesaAttr]->Ptr - low_addr); - velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor; - velements[attr].vertex_buffer_index = 0; - velements[attr].src_format = - st_pipe_vertex_format(arrays[mesaAttr]->Type, - arrays[mesaAttr]->Size, - arrays[mesaAttr]->Format, - arrays[mesaAttr]->Normalized); - assert(velements[attr].src_format); - } -} - - -/** - * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each - * vertex attribute. - * \param vbuffer returns vertex buffer info - * \param velements returns vertex element info - */ -static void -setup_non_interleaved_attribs(struct gl_context *ctx, - const struct st_vertex_program *vp, - const struct st_vp_variant *vpv, - const struct gl_client_array **arrays, - struct pipe_vertex_buffer vbuffer[], - struct pipe_vertex_element velements[], - unsigned max_index, - unsigned num_instances) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - GLuint attr; - - for (attr = 0; attr < vpv->num_inputs; attr++) { - const GLuint mesaAttr = vp->index_to_input[attr]; - struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj; - GLsizei stride = arrays[mesaAttr]->StrideB; - - if (bufobj && bufobj->Name) { - /* Attribute data is in a VBO. - * Recall that for VBOs, the gl_client_array->Ptr field is - * really an offset from the start of the VBO, not a pointer. - */ - struct st_buffer_object *stobj = st_buffer_object(bufobj); - assert(stobj->buffer); - - vbuffer[attr].buffer = NULL; - pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer); - vbuffer[attr].buffer_offset = pointer_to_offset(arrays[mesaAttr]->Ptr); - } - else { - /* wrap user data */ - if (arrays[mesaAttr]->Ptr) { - uint divisor = arrays[mesaAttr]->InstanceDivisor; - uint length = (divisor ? num_instances / divisor : max_index) + 1; - vbuffer[attr].buffer = - pipe_user_buffer_create(pipe->screen, - (void *) arrays[mesaAttr]->Ptr, - stride * length, - PIPE_BIND_VERTEX_BUFFER); - } - else { - /* no array, use ctx->Current.Attrib[] value */ - uint bytes = sizeof(ctx->Current.Attrib[0]); - vbuffer[attr].buffer = - pipe_user_buffer_create(pipe->screen, - (void *) ctx->Current.Attrib[mesaAttr], - bytes, - PIPE_BIND_VERTEX_BUFFER); - stride = 0; - } - - vbuffer[attr].buffer_offset = 0; - - /* Track user vertex buffers. */ - pipe_resource_reference(&st->user_vb[attr], vbuffer[attr].buffer); - st->user_vb_stride[attr] = stride; - st->num_user_vbs = MAX2(st->num_user_vbs, attr+1); - } - - /* common-case setup */ - vbuffer[attr].stride = stride; /* in bytes */ - - velements[attr].src_offset = 0; - velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor; - velements[attr].vertex_buffer_index = attr; - velements[attr].src_format - = st_pipe_vertex_format(arrays[mesaAttr]->Type, - arrays[mesaAttr]->Size, - arrays[mesaAttr]->Format, - arrays[mesaAttr]->Normalized); - assert(velements[attr].src_format); - } -} - - -static void -setup_index_buffer(struct gl_context *ctx, - const struct _mesa_index_buffer *ib, - struct pipe_index_buffer *ibuffer) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - - memset(ibuffer, 0, sizeof(*ibuffer)); - if (ib) { - struct gl_buffer_object *bufobj = ib->obj; - - switch (ib->type) { - case GL_UNSIGNED_INT: - ibuffer->index_size = 4; - break; - case GL_UNSIGNED_SHORT: - ibuffer->index_size = 2; - break; - case GL_UNSIGNED_BYTE: - ibuffer->index_size = 1; - break; - default: - assert(0); - return; - } - - /* get/create the index buffer object */ - if (bufobj && bufobj->Name) { - /* elements/indexes are in a real VBO */ - struct st_buffer_object *stobj = st_buffer_object(bufobj); - pipe_resource_reference(&ibuffer->buffer, stobj->buffer); - ibuffer->offset = pointer_to_offset(ib->ptr); - } - else { - /* element/indicies are in user space memory */ - ibuffer->buffer = - pipe_user_buffer_create(pipe->screen, (void *) ib->ptr, - ib->count * ibuffer->index_size, - PIPE_BIND_INDEX_BUFFER); - } - } -} - -/** - * Prior to drawing, check that any uniforms referenced by the - * current shader have been set. If a uniform has not been set, - * issue a warning. - */ -static void -check_uniforms(struct gl_context *ctx) -{ - struct gl_shader_program *shProg[3] = { - ctx->Shader.CurrentVertexProgram, - ctx->Shader.CurrentGeometryProgram, - ctx->Shader.CurrentFragmentProgram, - }; - unsigned j; - - for (j = 0; j < 3; j++) { - unsigned i; - - if (shProg[j] == NULL || !shProg[j]->LinkStatus) - continue; - - for (i = 0; i < shProg[j]->Uniforms->NumUniforms; i++) { - const struct gl_uniform *u = &shProg[j]->Uniforms->Uniforms[i]; - if (!u->Initialized) { - _mesa_warning(ctx, - "Using shader with uninitialized uniform: %s", - u->Name); - } - } - } -} - - -/** - * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to - * the corresponding Gallium type. - */ -static unsigned -translate_prim(const struct gl_context *ctx, unsigned prim) -{ - /* GL prims should match Gallium prims, spot-check a few */ - assert(GL_POINTS == PIPE_PRIM_POINTS); - assert(GL_QUADS == PIPE_PRIM_QUADS); - assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY); - - /* Avoid quadstrips if it's easy to do so: - * Note: it's imporant to do the correct trimming if we change the prim type! - * We do that wherever this function is called. - */ - if (prim == GL_QUAD_STRIP && - ctx->Light.ShadeModel != GL_FLAT && - ctx->Polygon.FrontMode == GL_FILL && - ctx->Polygon.BackMode == GL_FILL) - prim = GL_TRIANGLE_STRIP; - - return prim; -} - - -static void -st_validate_varrays(struct gl_context *ctx, - const struct gl_client_array **arrays, - unsigned max_index, - unsigned num_instances) -{ - struct st_context *st = st_context(ctx); - const struct st_vertex_program *vp; - const struct st_vp_variant *vpv; - struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS]; - struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS]; - unsigned num_vbuffers, num_velements; - GLuint attr; - unsigned i; - - /* must get these after state validation! */ - vp = st->vp; - vpv = st->vp_variant; - - memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs); - - /* Unreference any user vertex buffers. */ - for (i = 0; i < st->num_user_vbs; i++) { - pipe_resource_reference(&st->user_vb[i], NULL); - } - st->num_user_vbs = 0; - - /* - * Setup the vbuffer[] and velements[] arrays. - */ - if (is_interleaved_arrays(vp, vpv, arrays)) { - setup_interleaved_attribs(ctx, vp, vpv, arrays, vbuffer, velements, - max_index, num_instances); - - num_vbuffers = 1; - num_velements = vpv->num_inputs; - if (num_velements == 0) - num_vbuffers = 0; - } - else { - setup_non_interleaved_attribs(ctx, vp, vpv, arrays, - vbuffer, velements, max_index, num_instances); - num_vbuffers = vpv->num_inputs; - num_velements = vpv->num_inputs; - } - - cso_set_vertex_buffers(st->cso_context, num_vbuffers, vbuffer); - cso_set_vertex_elements(st->cso_context, num_velements, velements); - - /* unreference buffers (frees wrapped user-space buffer objects) - * This is OK, because the pipe driver should reference buffers by itself - * in set_vertex_buffers. */ - for (attr = 0; attr < num_vbuffers; attr++) { - pipe_resource_reference(&vbuffer[attr].buffer, NULL); - assert(!vbuffer[attr].buffer); - } -} - - -/** - * This function gets plugged into the VBO module and is called when - * we have something to render. - * Basically, translate the information into the format expected by gallium. - */ -void -st_draw_vbo(struct gl_context *ctx, - const struct gl_client_array **arrays, - const struct _mesa_prim *prims, - GLuint nr_prims, - const struct _mesa_index_buffer *ib, - GLboolean index_bounds_valid, - GLuint min_index, - GLuint max_index) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - struct pipe_index_buffer ibuffer; - struct pipe_draw_info info; - unsigned i, num_instances = 1; - GLboolean new_array = - st->dirty.st && (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM)) != 0; - - /* Mesa core state should have been validated already */ - assert(ctx->NewState == 0x0); - - if (ib) { - /* Gallium probably doesn't want this in some cases. */ - if (!index_bounds_valid) - if (!vbo_all_varyings_in_vbos(arrays)) - vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index); - - for (i = 0; i < nr_prims; i++) { - num_instances = MAX2(num_instances, prims[i].num_instances); - } - } else { - /* Get min/max index for non-indexed drawing. */ - min_index = ~0; - max_index = 0; - - for (i = 0; i < nr_prims; i++) { - min_index = MIN2(min_index, prims[i].start); - max_index = MAX2(max_index, prims[i].start + prims[i].count - 1); - num_instances = MAX2(num_instances, prims[i].num_instances); - } - } - - /* Validate state. */ - if (st->dirty.st) { - GLboolean vertDataEdgeFlags; - - /* sanity check for pointer arithmetic below */ - assert(sizeof(arrays[0]->Ptr[0]) == 1); - - vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj && - arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name; - if (vertDataEdgeFlags != st->vertdata_edgeflags) { - st->vertdata_edgeflags = vertDataEdgeFlags; - st->dirty.st |= ST_NEW_EDGEFLAGS_DATA; - } - - st_validate_state(st); - - if (new_array) { - st_validate_varrays(ctx, arrays, max_index, num_instances); - } - -#if 0 - if (MESA_VERBOSE & VERBOSE_GLSL) { - check_uniforms(ctx); - } -#else - (void) check_uniforms; -#endif - } - - /* Notify the driver that the content of user buffers may have been - * changed. */ - if (!new_array && st->num_user_vbs) { - for (i = 0; i < st->num_user_vbs; i++) { - if (st->user_vb[i]) { - unsigned stride = st->user_vb_stride[i]; - - if (stride) { - pipe->redefine_user_buffer(pipe, st->user_vb[i], - min_index * stride, - (max_index + 1 - min_index) * stride); - } else { - /* stride == 0 */ - pipe->redefine_user_buffer(pipe, st->user_vb[i], - 0, st->user_vb[i]->width0); - } - } - } - } - - setup_index_buffer(ctx, ib, &ibuffer); - pipe->set_index_buffer(pipe, &ibuffer); - - util_draw_init_info(&info); - if (ib) { - info.indexed = TRUE; - if (min_index != ~0 && max_index != ~0) { - info.min_index = min_index; - info.max_index = max_index; - } - } - - info.primitive_restart = st->ctx->Array.PrimitiveRestart; - info.restart_index = st->ctx->Array.RestartIndex; - - /* do actual drawing */ - for (i = 0; i < nr_prims; i++) { - info.mode = translate_prim( ctx, prims[i].mode ); - info.start = prims[i].start; - info.count = prims[i].count; - info.instance_count = prims[i].num_instances; - info.index_bias = prims[i].basevertex; - if (!ib) { - info.min_index = info.start; - info.max_index = info.start + info.count - 1; - } - - if (u_trim_pipe_prim(info.mode, &info.count)) - pipe->draw_vbo(pipe, &info); - } - - pipe_resource_reference(&ibuffer.buffer, NULL); -} - - -void st_init_draw( struct st_context *st ) -{ - struct gl_context *ctx = st->ctx; - - vbo_set_draw_func(ctx, st_draw_vbo); - -#if FEATURE_feedback || FEATURE_rastpos - st->draw = draw_create(st->pipe); /* for selection/feedback */ - - /* Disable draw options that might convert points/lines to tris, etc. - * as that would foul-up feedback/selection mode. - */ - draw_wide_line_threshold(st->draw, 1000.0f); - draw_wide_point_threshold(st->draw, 1000.0f); - draw_enable_line_stipple(st->draw, FALSE); - draw_enable_point_sprites(st->draw, FALSE); -#endif -} - - -void st_destroy_draw( struct st_context *st ) -{ -#if FEATURE_feedback || FEATURE_rastpos - draw_destroy(st->draw); -#endif -} - - +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+/*
+ * This file implements the st_draw_vbo() function which is called from
+ * Mesa's VBO module. All point/line/triangle rendering is done through
+ * this function whether the user called glBegin/End, glDrawArrays,
+ * glDrawElements, glEvalMesh, or glCalList, etc.
+ *
+ * We basically convert the VBO's vertex attribute/array information into
+ * Gallium vertex state, bind the vertex buffer objects and call
+ * pipe->draw_elements(), pipe->draw_range_elements() or pipe->draw_arrays().
+ *
+ * Authors:
+ * Keith Whitwell <keith@tungstengraphics.com>
+ */
+
+
+#include "main/imports.h"
+#include "main/image.h"
+#include "main/macros.h"
+#include "main/mfeatures.h"
+#include "program/prog_uniform.h"
+
+#include "vbo/vbo.h"
+
+#include "st_context.h"
+#include "st_atom.h"
+#include "st_cb_bufferobjects.h"
+#include "st_draw.h"
+#include "st_program.h"
+
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "util/u_inlines.h"
+#include "util/u_format.h"
+#include "util/u_prim.h"
+#include "util/u_draw_quad.h"
+#include "draw/draw_context.h"
+#include "cso_cache/cso_context.h"
+
+
+static GLuint double_types[4] = {
+ PIPE_FORMAT_R64_FLOAT,
+ PIPE_FORMAT_R64G64_FLOAT,
+ PIPE_FORMAT_R64G64B64_FLOAT,
+ PIPE_FORMAT_R64G64B64A64_FLOAT
+};
+
+static GLuint float_types[4] = {
+ PIPE_FORMAT_R32_FLOAT,
+ PIPE_FORMAT_R32G32_FLOAT,
+ PIPE_FORMAT_R32G32B32_FLOAT,
+ PIPE_FORMAT_R32G32B32A32_FLOAT
+};
+
+static GLuint half_float_types[4] = {
+ PIPE_FORMAT_R16_FLOAT,
+ PIPE_FORMAT_R16G16_FLOAT,
+ PIPE_FORMAT_R16G16B16_FLOAT,
+ PIPE_FORMAT_R16G16B16A16_FLOAT
+};
+
+static GLuint uint_types_norm[4] = {
+ PIPE_FORMAT_R32_UNORM,
+ PIPE_FORMAT_R32G32_UNORM,
+ PIPE_FORMAT_R32G32B32_UNORM,
+ PIPE_FORMAT_R32G32B32A32_UNORM
+};
+
+static GLuint uint_types_scale[4] = {
+ PIPE_FORMAT_R32_USCALED,
+ PIPE_FORMAT_R32G32_USCALED,
+ PIPE_FORMAT_R32G32B32_USCALED,
+ PIPE_FORMAT_R32G32B32A32_USCALED
+};
+
+static GLuint int_types_norm[4] = {
+ PIPE_FORMAT_R32_SNORM,
+ PIPE_FORMAT_R32G32_SNORM,
+ PIPE_FORMAT_R32G32B32_SNORM,
+ PIPE_FORMAT_R32G32B32A32_SNORM
+};
+
+static GLuint int_types_scale[4] = {
+ PIPE_FORMAT_R32_SSCALED,
+ PIPE_FORMAT_R32G32_SSCALED,
+ PIPE_FORMAT_R32G32B32_SSCALED,
+ PIPE_FORMAT_R32G32B32A32_SSCALED
+};
+
+static GLuint ushort_types_norm[4] = {
+ PIPE_FORMAT_R16_UNORM,
+ PIPE_FORMAT_R16G16_UNORM,
+ PIPE_FORMAT_R16G16B16_UNORM,
+ PIPE_FORMAT_R16G16B16A16_UNORM
+};
+
+static GLuint ushort_types_scale[4] = {
+ PIPE_FORMAT_R16_USCALED,
+ PIPE_FORMAT_R16G16_USCALED,
+ PIPE_FORMAT_R16G16B16_USCALED,
+ PIPE_FORMAT_R16G16B16A16_USCALED
+};
+
+static GLuint short_types_norm[4] = {
+ PIPE_FORMAT_R16_SNORM,
+ PIPE_FORMAT_R16G16_SNORM,
+ PIPE_FORMAT_R16G16B16_SNORM,
+ PIPE_FORMAT_R16G16B16A16_SNORM
+};
+
+static GLuint short_types_scale[4] = {
+ PIPE_FORMAT_R16_SSCALED,
+ PIPE_FORMAT_R16G16_SSCALED,
+ PIPE_FORMAT_R16G16B16_SSCALED,
+ PIPE_FORMAT_R16G16B16A16_SSCALED
+};
+
+static GLuint ubyte_types_norm[4] = {
+ PIPE_FORMAT_R8_UNORM,
+ PIPE_FORMAT_R8G8_UNORM,
+ PIPE_FORMAT_R8G8B8_UNORM,
+ PIPE_FORMAT_R8G8B8A8_UNORM
+};
+
+static GLuint ubyte_types_scale[4] = {
+ PIPE_FORMAT_R8_USCALED,
+ PIPE_FORMAT_R8G8_USCALED,
+ PIPE_FORMAT_R8G8B8_USCALED,
+ PIPE_FORMAT_R8G8B8A8_USCALED
+};
+
+static GLuint byte_types_norm[4] = {
+ PIPE_FORMAT_R8_SNORM,
+ PIPE_FORMAT_R8G8_SNORM,
+ PIPE_FORMAT_R8G8B8_SNORM,
+ PIPE_FORMAT_R8G8B8A8_SNORM
+};
+
+static GLuint byte_types_scale[4] = {
+ PIPE_FORMAT_R8_SSCALED,
+ PIPE_FORMAT_R8G8_SSCALED,
+ PIPE_FORMAT_R8G8B8_SSCALED,
+ PIPE_FORMAT_R8G8B8A8_SSCALED
+};
+
+static GLuint fixed_types[4] = {
+ PIPE_FORMAT_R32_FIXED,
+ PIPE_FORMAT_R32G32_FIXED,
+ PIPE_FORMAT_R32G32B32_FIXED,
+ PIPE_FORMAT_R32G32B32A32_FIXED
+};
+
+
+
+/**
+ * Return a PIPE_FORMAT_x for the given GL datatype and size.
+ */
+GLuint
+st_pipe_vertex_format(GLenum type, GLuint size, GLenum format,
+ GLboolean normalized)
+{
+ assert((type >= GL_BYTE && type <= GL_DOUBLE) ||
+ type == GL_FIXED || type == GL_HALF_FLOAT);
+ assert(size >= 1);
+ assert(size <= 4);
+ assert(format == GL_RGBA || format == GL_BGRA);
+
+ if (format == GL_BGRA) {
+ /* this is an odd-ball case */
+ assert(type == GL_UNSIGNED_BYTE);
+ assert(normalized);
+ return PIPE_FORMAT_B8G8R8A8_UNORM;
+ }
+
+ if (normalized) {
+ switch (type) {
+ case GL_DOUBLE: return double_types[size-1];
+ case GL_FLOAT: return float_types[size-1];
+ case GL_HALF_FLOAT: return half_float_types[size-1];
+ case GL_INT: return int_types_norm[size-1];
+ case GL_SHORT: return short_types_norm[size-1];
+ case GL_BYTE: return byte_types_norm[size-1];
+ case GL_UNSIGNED_INT: return uint_types_norm[size-1];
+ case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1];
+ case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1];
+ case GL_FIXED: return fixed_types[size-1];
+ default: assert(0); return 0;
+ }
+ }
+ else {
+ switch (type) {
+ case GL_DOUBLE: return double_types[size-1];
+ case GL_FLOAT: return float_types[size-1];
+ case GL_HALF_FLOAT: return half_float_types[size-1];
+ case GL_INT: return int_types_scale[size-1];
+ case GL_SHORT: return short_types_scale[size-1];
+ case GL_BYTE: return byte_types_scale[size-1];
+ case GL_UNSIGNED_INT: return uint_types_scale[size-1];
+ case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1];
+ case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1];
+ case GL_FIXED: return fixed_types[size-1];
+ default: assert(0); return 0;
+ }
+ }
+ return 0; /* silence compiler warning */
+}
+
+
+
+
+
+/**
+ * Examine the active arrays to determine if we have interleaved
+ * vertex arrays all living in one VBO, or all living in user space.
+ * \param userSpace returns whether the arrays are in user space.
+ */
+static GLboolean
+is_interleaved_arrays(const struct st_vertex_program *vp,
+ const struct st_vp_variant *vpv,
+ const struct gl_client_array **arrays)
+{
+ GLuint attr;
+ const struct gl_buffer_object *firstBufObj = NULL;
+ GLint firstStride = -1;
+ const GLubyte *client_addr = NULL;
+ GLboolean user_memory;
+
+ for (attr = 0; attr < vpv->num_inputs; attr++) {
+ const GLuint mesaAttr = vp->index_to_input[attr];
+ const struct gl_buffer_object *bufObj = arrays[mesaAttr]->BufferObj;
+ const GLsizei stride = arrays[mesaAttr]->StrideB; /* in bytes */
+
+ if (firstStride < 0) {
+ firstStride = stride;
+ user_memory = !bufObj || !bufObj->Name;
+ }
+ else if (firstStride != stride) {
+ return GL_FALSE;
+ }
+
+ if (!bufObj || !bufObj->Name) {
+ /* Try to detect if the client-space arrays are
+ * "close" to each other.
+ */
+ if (!user_memory) {
+ return GL_FALSE;
+ }
+ if (!client_addr) {
+ client_addr = arrays[mesaAttr]->Ptr;
+ }
+ else if (abs(arrays[mesaAttr]->Ptr - client_addr) > firstStride) {
+ /* arrays start too far apart */
+ return GL_FALSE;
+ }
+ }
+ else if (!firstBufObj) {
+ if (user_memory) {
+ return GL_FALSE;
+ }
+ firstBufObj = bufObj;
+ }
+ else if (bufObj != firstBufObj) {
+ return GL_FALSE;
+ }
+ }
+
+ return GL_TRUE;
+}
+
+
+/**
+ * Set up for drawing interleaved arrays that all live in one VBO
+ * or all live in user space.
+ * \param vbuffer returns vertex buffer info
+ * \param velements returns vertex element info
+ */
+static void
+setup_interleaved_attribs(struct gl_context *ctx,
+ const struct st_vertex_program *vp,
+ const struct st_vp_variant *vpv,
+ const struct gl_client_array **arrays,
+ struct pipe_vertex_buffer *vbuffer,
+ struct pipe_vertex_element velements[],
+ unsigned max_index,
+ unsigned num_instances)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ GLuint attr;
+ const GLubyte *low_addr = NULL;
+
+ /* Find the lowest address. */
+ if(vpv->num_inputs) {
+ low_addr = arrays[vp->index_to_input[0]]->Ptr;
+
+ for (attr = 1; attr < vpv->num_inputs; attr++) {
+ const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
+ low_addr = MIN2(low_addr, start);
+ }
+ }
+
+ for (attr = 0; attr < vpv->num_inputs; attr++) {
+ const GLuint mesaAttr = vp->index_to_input[attr];
+ struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
+ struct st_buffer_object *stobj = st_buffer_object(bufobj);
+ GLsizei stride = arrays[mesaAttr]->StrideB;
+
+ if (attr == 0) {
+ if (bufobj && bufobj->Name) {
+ vbuffer->buffer = NULL;
+ pipe_resource_reference(&vbuffer->buffer, stobj->buffer);
+ vbuffer->buffer_offset = pointer_to_offset(low_addr);
+ } else {
+ uint divisor = arrays[mesaAttr]->InstanceDivisor;
+ uint length = (divisor ? num_instances / divisor : max_index) + 1;
+ vbuffer->buffer =
+ pipe_user_buffer_create(pipe->screen, (void*)low_addr,
+ stride * length,
+ PIPE_BIND_VERTEX_BUFFER);
+ vbuffer->buffer_offset = 0;
+
+ /* Track user vertex buffers. */
+ pipe_resource_reference(&st->user_vb[0], vbuffer->buffer);
+ st->user_vb_stride[0] = stride;
+ st->num_user_vbs = 1;
+ }
+ vbuffer->stride = stride; /* in bytes */
+ }
+
+ velements[attr].src_offset =
+ (unsigned) (arrays[mesaAttr]->Ptr - low_addr);
+ velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor;
+ velements[attr].vertex_buffer_index = 0;
+ velements[attr].src_format =
+ st_pipe_vertex_format(arrays[mesaAttr]->Type,
+ arrays[mesaAttr]->Size,
+ arrays[mesaAttr]->Format,
+ arrays[mesaAttr]->Normalized);
+ assert(velements[attr].src_format);
+ }
+}
+
+
+/**
+ * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
+ * vertex attribute.
+ * \param vbuffer returns vertex buffer info
+ * \param velements returns vertex element info
+ */
+static void
+setup_non_interleaved_attribs(struct gl_context *ctx,
+ const struct st_vertex_program *vp,
+ const struct st_vp_variant *vpv,
+ const struct gl_client_array **arrays,
+ struct pipe_vertex_buffer vbuffer[],
+ struct pipe_vertex_element velements[],
+ unsigned max_index,
+ unsigned num_instances)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ GLuint attr;
+
+ for (attr = 0; attr < vpv->num_inputs; attr++) {
+ const GLuint mesaAttr = vp->index_to_input[attr];
+ struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
+ GLsizei stride = arrays[mesaAttr]->StrideB;
+
+ if (bufobj && bufobj->Name) {
+ /* Attribute data is in a VBO.
+ * Recall that for VBOs, the gl_client_array->Ptr field is
+ * really an offset from the start of the VBO, not a pointer.
+ */
+ struct st_buffer_object *stobj = st_buffer_object(bufobj);
+ assert(stobj->buffer);
+
+ vbuffer[attr].buffer = NULL;
+ pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer);
+ vbuffer[attr].buffer_offset = pointer_to_offset(arrays[mesaAttr]->Ptr);
+ }
+ else {
+ /* wrap user data */
+ if (arrays[mesaAttr]->Ptr) {
+ uint divisor = arrays[mesaAttr]->InstanceDivisor;
+ uint length = (divisor ? num_instances / divisor : max_index) + 1;
+ vbuffer[attr].buffer =
+ pipe_user_buffer_create(pipe->screen,
+ (void *) arrays[mesaAttr]->Ptr,
+ stride * length,
+ PIPE_BIND_VERTEX_BUFFER);
+ }
+ else {
+ /* no array, use ctx->Current.Attrib[] value */
+ uint bytes = sizeof(ctx->Current.Attrib[0]);
+ vbuffer[attr].buffer =
+ pipe_user_buffer_create(pipe->screen,
+ (void *) ctx->Current.Attrib[mesaAttr],
+ bytes,
+ PIPE_BIND_VERTEX_BUFFER);
+ stride = 0;
+ }
+
+ vbuffer[attr].buffer_offset = 0;
+
+ /* Track user vertex buffers. */
+ pipe_resource_reference(&st->user_vb[attr], vbuffer[attr].buffer);
+ st->user_vb_stride[attr] = stride;
+ st->num_user_vbs = MAX2(st->num_user_vbs, attr+1);
+ }
+
+ /* common-case setup */
+ vbuffer[attr].stride = stride; /* in bytes */
+
+ velements[attr].src_offset = 0;
+ velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor;
+ velements[attr].vertex_buffer_index = attr;
+ velements[attr].src_format
+ = st_pipe_vertex_format(arrays[mesaAttr]->Type,
+ arrays[mesaAttr]->Size,
+ arrays[mesaAttr]->Format,
+ arrays[mesaAttr]->Normalized);
+ assert(velements[attr].src_format);
+ }
+}
+
+
+static void
+setup_index_buffer(struct gl_context *ctx,
+ const struct _mesa_index_buffer *ib,
+ struct pipe_index_buffer *ibuffer)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+
+ memset(ibuffer, 0, sizeof(*ibuffer));
+ if (ib) {
+ struct gl_buffer_object *bufobj = ib->obj;
+
+ switch (ib->type) {
+ case GL_UNSIGNED_INT:
+ ibuffer->index_size = 4;
+ break;
+ case GL_UNSIGNED_SHORT:
+ ibuffer->index_size = 2;
+ break;
+ case GL_UNSIGNED_BYTE:
+ ibuffer->index_size = 1;
+ break;
+ default:
+ assert(0);
+ return;
+ }
+
+ /* get/create the index buffer object */
+ if (bufobj && bufobj->Name) {
+ /* elements/indexes are in a real VBO */
+ struct st_buffer_object *stobj = st_buffer_object(bufobj);
+ pipe_resource_reference(&ibuffer->buffer, stobj->buffer);
+ ibuffer->offset = pointer_to_offset(ib->ptr);
+ }
+ else {
+ /* element/indicies are in user space memory */
+ ibuffer->buffer =
+ pipe_user_buffer_create(pipe->screen, (void *) ib->ptr,
+ ib->count * ibuffer->index_size,
+ PIPE_BIND_INDEX_BUFFER);
+ }
+ }
+}
+
+/**
+ * Prior to drawing, check that any uniforms referenced by the
+ * current shader have been set. If a uniform has not been set,
+ * issue a warning.
+ */
+static void
+check_uniforms(struct gl_context *ctx)
+{
+ struct gl_shader_program *shProg[3] = {
+ ctx->Shader.CurrentVertexProgram,
+ ctx->Shader.CurrentGeometryProgram,
+ ctx->Shader.CurrentFragmentProgram,
+ };
+ unsigned j;
+
+ for (j = 0; j < 3; j++) {
+ unsigned i;
+
+ if (shProg[j] == NULL || !shProg[j]->LinkStatus)
+ continue;
+
+ for (i = 0; i < shProg[j]->Uniforms->NumUniforms; i++) {
+ const struct gl_uniform *u = &shProg[j]->Uniforms->Uniforms[i];
+ if (!u->Initialized) {
+ _mesa_warning(ctx,
+ "Using shader with uninitialized uniform: %s",
+ u->Name);
+ }
+ }
+ }
+}
+
+
+/**
+ * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
+ * the corresponding Gallium type.
+ */
+static unsigned
+translate_prim(const struct gl_context *ctx, unsigned prim)
+{
+ /* GL prims should match Gallium prims, spot-check a few */
+ assert(GL_POINTS == PIPE_PRIM_POINTS);
+ assert(GL_QUADS == PIPE_PRIM_QUADS);
+ assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
+
+ /* Avoid quadstrips if it's easy to do so:
+ * Note: it's imporant to do the correct trimming if we change the prim type!
+ * We do that wherever this function is called.
+ */
+ if (prim == GL_QUAD_STRIP &&
+ ctx->Light.ShadeModel != GL_FLAT &&
+ ctx->Polygon.FrontMode == GL_FILL &&
+ ctx->Polygon.BackMode == GL_FILL)
+ prim = GL_TRIANGLE_STRIP;
+
+ return prim;
+}
+
+
+static void
+st_validate_varrays(struct gl_context *ctx,
+ const struct gl_client_array **arrays,
+ unsigned max_index,
+ unsigned num_instances)
+{
+ struct st_context *st = st_context(ctx);
+ const struct st_vertex_program *vp;
+ const struct st_vp_variant *vpv;
+ struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
+ struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
+ unsigned num_vbuffers, num_velements;
+ GLuint attr;
+ unsigned i;
+
+ /* must get these after state validation! */
+ vp = st->vp;
+ vpv = st->vp_variant;
+
+ memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs);
+
+ /* Unreference any user vertex buffers. */
+ for (i = 0; i < st->num_user_vbs; i++) {
+ pipe_resource_reference(&st->user_vb[i], NULL);
+ }
+ st->num_user_vbs = 0;
+
+ /*
+ * Setup the vbuffer[] and velements[] arrays.
+ */
+ if (is_interleaved_arrays(vp, vpv, arrays)) {
+ setup_interleaved_attribs(ctx, vp, vpv, arrays, vbuffer, velements,
+ max_index, num_instances);
+
+ num_vbuffers = 1;
+ num_velements = vpv->num_inputs;
+ if (num_velements == 0)
+ num_vbuffers = 0;
+ }
+ else {
+ setup_non_interleaved_attribs(ctx, vp, vpv, arrays,
+ vbuffer, velements, max_index, num_instances);
+ num_vbuffers = vpv->num_inputs;
+ num_velements = vpv->num_inputs;
+ }
+
+ cso_set_vertex_buffers(st->cso_context, num_vbuffers, vbuffer);
+ cso_set_vertex_elements(st->cso_context, num_velements, velements);
+
+ /* unreference buffers (frees wrapped user-space buffer objects)
+ * This is OK, because the pipe driver should reference buffers by itself
+ * in set_vertex_buffers. */
+ for (attr = 0; attr < num_vbuffers; attr++) {
+ pipe_resource_reference(&vbuffer[attr].buffer, NULL);
+ assert(!vbuffer[attr].buffer);
+ }
+}
+
+
+/**
+ * This function gets plugged into the VBO module and is called when
+ * we have something to render.
+ * Basically, translate the information into the format expected by gallium.
+ */
+void
+st_draw_vbo(struct gl_context *ctx,
+ const struct gl_client_array **arrays,
+ const struct _mesa_prim *prims,
+ GLuint nr_prims,
+ const struct _mesa_index_buffer *ib,
+ GLboolean index_bounds_valid,
+ GLuint min_index,
+ GLuint max_index)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct pipe_index_buffer ibuffer;
+ struct pipe_draw_info info;
+ unsigned i, num_instances = 1;
+ GLboolean new_array =
+ st->dirty.st && (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM)) != 0;
+
+ /* Mesa core state should have been validated already */
+ assert(ctx->NewState == 0x0);
+
+ if (ib) {
+ /* Gallium probably doesn't want this in some cases. */
+ if (!index_bounds_valid)
+ if (!vbo_all_varyings_in_vbos(arrays))
+ vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
+
+ for (i = 0; i < nr_prims; i++) {
+ num_instances = MAX2(num_instances, prims[i].num_instances);
+ }
+ } else {
+ /* Get min/max index for non-indexed drawing. */
+ min_index = ~0;
+ max_index = 0;
+
+ for (i = 0; i < nr_prims; i++) {
+ min_index = MIN2(min_index, prims[i].start);
+ max_index = MAX2(max_index, prims[i].start + prims[i].count - 1);
+ num_instances = MAX2(num_instances, prims[i].num_instances);
+ }
+ }
+
+ /* Validate state. */
+ if (st->dirty.st) {
+ GLboolean vertDataEdgeFlags;
+
+ /* sanity check for pointer arithmetic below */
+ assert(sizeof(arrays[0]->Ptr[0]) == 1);
+
+ vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj &&
+ arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name;
+ if (vertDataEdgeFlags != st->vertdata_edgeflags) {
+ st->vertdata_edgeflags = vertDataEdgeFlags;
+ st->dirty.st |= ST_NEW_EDGEFLAGS_DATA;
+ }
+
+ st_validate_state(st);
+
+ if (new_array) {
+ st_validate_varrays(ctx, arrays, max_index, num_instances);
+ }
+
+#if 0
+ if (MESA_VERBOSE & VERBOSE_GLSL) {
+ check_uniforms(ctx);
+ }
+#else
+ (void) check_uniforms;
+#endif
+ }
+
+ /* Notify the driver that the content of user buffers may have been
+ * changed. */
+ if (!new_array && st->num_user_vbs) {
+ for (i = 0; i < st->num_user_vbs; i++) {
+ if (st->user_vb[i]) {
+ unsigned stride = st->user_vb_stride[i];
+
+ if (stride) {
+ pipe->redefine_user_buffer(pipe, st->user_vb[i],
+ min_index * stride,
+ (max_index + 1 - min_index) * stride);
+ } else {
+ /* stride == 0 */
+ pipe->redefine_user_buffer(pipe, st->user_vb[i],
+ 0, st->user_vb[i]->width0);
+ }
+ }
+ }
+ }
+
+ setup_index_buffer(ctx, ib, &ibuffer);
+ pipe->set_index_buffer(pipe, &ibuffer);
+
+ util_draw_init_info(&info);
+ if (ib) {
+ info.indexed = TRUE;
+ if (min_index != ~0 && max_index != ~0) {
+ info.min_index = min_index;
+ info.max_index = max_index;
+ }
+ }
+
+ info.primitive_restart = st->ctx->Array.PrimitiveRestart;
+ info.restart_index = st->ctx->Array.RestartIndex;
+
+ /* do actual drawing */
+ for (i = 0; i < nr_prims; i++) {
+ info.mode = translate_prim( ctx, prims[i].mode );
+ info.start = prims[i].start;
+ info.count = prims[i].count;
+ info.instance_count = prims[i].num_instances;
+ info.index_bias = prims[i].basevertex;
+ if (!ib) {
+ info.min_index = info.start;
+ info.max_index = info.start + info.count - 1;
+ }
+
+ if (u_trim_pipe_prim(info.mode, &info.count))
+ pipe->draw_vbo(pipe, &info);
+ }
+
+ pipe_resource_reference(&ibuffer.buffer, NULL);
+}
+
+
+void st_init_draw( struct st_context *st )
+{
+ struct gl_context *ctx = st->ctx;
+
+ vbo_set_draw_func(ctx, st_draw_vbo);
+
+#if FEATURE_feedback || FEATURE_rastpos
+ st->draw = draw_create(st->pipe); /* for selection/feedback */
+
+ /* Disable draw options that might convert points/lines to tris, etc.
+ * as that would foul-up feedback/selection mode.
+ */
+ draw_wide_line_threshold(st->draw, 1000.0f);
+ draw_wide_point_threshold(st->draw, 1000.0f);
+ draw_enable_line_stipple(st->draw, FALSE);
+ draw_enable_point_sprites(st->draw, FALSE);
+#endif
+}
+
+
+void st_destroy_draw( struct st_context *st )
+{
+#if FEATURE_feedback || FEATURE_rastpos
+ draw_destroy(st->draw);
+#endif
+}
+
+
diff --git a/mesalib/src/mesa/state_tracker/st_draw_feedback.c b/mesalib/src/mesa/state_tracker/st_draw_feedback.c index 1e1220bfe..f0734eae0 100644 --- a/mesalib/src/mesa/state_tracker/st_draw_feedback.c +++ b/mesalib/src/mesa/state_tracker/st_draw_feedback.c @@ -1,277 +1,277 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - -#include "main/imports.h" -#include "main/image.h" -#include "main/macros.h" -#include "main/mfeatures.h" - -#include "vbo/vbo.h" - -#include "st_context.h" -#include "st_atom.h" -#include "st_cb_bufferobjects.h" -#include "st_draw.h" -#include "st_program.h" - -#include "pipe/p_context.h" -#include "pipe/p_defines.h" -#include "util/u_inlines.h" - -#include "draw/draw_private.h" -#include "draw/draw_context.h" - - -#if FEATURE_feedback || FEATURE_rastpos - -/** - * Set the (private) draw module's post-transformed vertex format when in - * GL_SELECT or GL_FEEDBACK mode or for glRasterPos. - */ -static void -set_feedback_vertex_format(struct gl_context *ctx) -{ -#if 0 - struct st_context *st = st_context(ctx); - struct vertex_info vinfo; - GLuint i; - - memset(&vinfo, 0, sizeof(vinfo)); - - if (ctx->RenderMode == GL_SELECT) { - assert(ctx->RenderMode == GL_SELECT); - vinfo.num_attribs = 1; - vinfo.format[0] = FORMAT_4F; - vinfo.interp_mode[0] = INTERP_LINEAR; - } - else { - /* GL_FEEDBACK, or glRasterPos */ - /* emit all attribs (pos, color, texcoord) as GLfloat[4] */ - vinfo.num_attribs = st->state.vs->cso->state.num_outputs; - for (i = 0; i < vinfo.num_attribs; i++) { - vinfo.format[i] = FORMAT_4F; - vinfo.interp_mode[i] = INTERP_LINEAR; - } - } - - draw_set_vertex_info(st->draw, &vinfo); -#endif -} - - -/** - * Called by VBO to draw arrays when in selection or feedback mode and - * to implement glRasterPos. - * This is very much like the normal draw_vbo() function above. - * Look at code refactoring some day. - * Might move this into the failover module some day. - */ -void -st_feedback_draw_vbo(struct gl_context *ctx, - const struct gl_client_array **arrays, - const struct _mesa_prim *prims, - GLuint nr_prims, - const struct _mesa_index_buffer *ib, - GLboolean index_bounds_valid, - GLuint min_index, - GLuint max_index) -{ - struct st_context *st = st_context(ctx); - struct pipe_context *pipe = st->pipe; - struct draw_context *draw = st->draw; - const struct st_vertex_program *vp; - const struct pipe_shader_state *vs; - struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS]; - struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS]; - struct pipe_index_buffer ibuffer; - struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS]; - struct pipe_transfer *ib_transfer = NULL; - GLuint attr, i; - const void *mapped_indices = NULL; - - assert(draw); - - st_validate_state(st); - - if (!index_bounds_valid) - vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index); - - /* must get these after state validation! */ - vp = st->vp; - vs = &st->vp_variant->tgsi; - - if (!st->vp_variant->draw_shader) { - st->vp_variant->draw_shader = draw_create_vertex_shader(draw, vs); - } - - /* - * Set up the draw module's state. - * - * We'd like to do this less frequently, but the normal state-update - * code sends state updates to the pipe, not to our private draw module. - */ - assert(draw); - draw_set_viewport_state(draw, &st->state.viewport); - draw_set_clip_state(draw, &st->state.clip); - draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL); - draw_bind_vertex_shader(draw, st->vp_variant->draw_shader); - set_feedback_vertex_format(ctx); - - /* loop over TGSI shader inputs to determine vertex buffer - * and attribute info - */ - for (attr = 0; attr < vp->num_inputs; attr++) { - const GLuint mesaAttr = vp->index_to_input[attr]; - struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj; - void *map; - - if (bufobj && bufobj->Name) { - /* Attribute data is in a VBO. - * Recall that for VBOs, the gl_client_array->Ptr field is - * really an offset from the start of the VBO, not a pointer. - */ - struct st_buffer_object *stobj = st_buffer_object(bufobj); - assert(stobj->buffer); - - vbuffers[attr].buffer = NULL; - pipe_resource_reference(&vbuffers[attr].buffer, stobj->buffer); - vbuffers[attr].buffer_offset = pointer_to_offset(arrays[0]->Ptr); - velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr; - } - else { - /* attribute data is in user-space memory, not a VBO */ - uint bytes = (arrays[mesaAttr]->Size - * _mesa_sizeof_type(arrays[mesaAttr]->Type) - * (max_index + 1)); - - /* wrap user data */ - vbuffers[attr].buffer - = pipe_user_buffer_create(pipe->screen, (void *) arrays[mesaAttr]->Ptr, - bytes, - PIPE_BIND_VERTEX_BUFFER); - vbuffers[attr].buffer_offset = 0; - velements[attr].src_offset = 0; - } - - /* common-case setup */ - vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */ - velements[attr].instance_divisor = 0; - velements[attr].vertex_buffer_index = attr; - velements[attr].src_format = - st_pipe_vertex_format(arrays[mesaAttr]->Type, - arrays[mesaAttr]->Size, - arrays[mesaAttr]->Format, - arrays[mesaAttr]->Normalized); - assert(velements[attr].src_format); - - /* tell draw about this attribute */ -#if 0 - draw_set_vertex_buffer(draw, attr, &vbuffer[attr]); -#endif - - /* map the attrib buffer */ - map = pipe_buffer_map(pipe, vbuffers[attr].buffer, - PIPE_TRANSFER_READ, - &vb_transfer[attr]); - draw_set_mapped_vertex_buffer(draw, attr, map); - } - - draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers); - draw_set_vertex_elements(draw, vp->num_inputs, velements); - - memset(&ibuffer, 0, sizeof(ibuffer)); - if (ib) { - struct gl_buffer_object *bufobj = ib->obj; - - switch (ib->type) { - case GL_UNSIGNED_INT: - ibuffer.index_size = 4; - break; - case GL_UNSIGNED_SHORT: - ibuffer.index_size = 2; - break; - case GL_UNSIGNED_BYTE: - ibuffer.index_size = 1; - break; - default: - assert(0); - goto out_unref_vertex; - } - - if (bufobj && bufobj->Name) { - struct st_buffer_object *stobj = st_buffer_object(bufobj); - - pipe_resource_reference(&ibuffer.buffer, stobj->buffer); - ibuffer.offset = pointer_to_offset(ib->ptr); - - mapped_indices = pipe_buffer_map(pipe, stobj->buffer, - PIPE_TRANSFER_READ, &ib_transfer); - } - else { - /* skip setting ibuffer.buffer as the draw module does not use it */ - mapped_indices = ib->ptr; - } - - draw_set_index_buffer(draw, &ibuffer); - draw_set_mapped_index_buffer(draw, mapped_indices); - } - - /* set the constant buffer */ - draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0, - st->state.constants[PIPE_SHADER_VERTEX].ptr, - st->state.constants[PIPE_SHADER_VERTEX].size); - - - /* draw here */ - for (i = 0; i < nr_prims; i++) { - draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count); - } - - - /* - * unmap vertex/index buffers - */ - if (ib) { - draw_set_mapped_index_buffer(draw, NULL); - draw_set_index_buffer(draw, NULL); - - if (ib_transfer) - pipe_buffer_unmap(pipe, ib_transfer); - pipe_resource_reference(&ibuffer.buffer, NULL); - } - - out_unref_vertex: - for (attr = 0; attr < vp->num_inputs; attr++) { - pipe_buffer_unmap(pipe, vb_transfer[attr]); - draw_set_mapped_vertex_buffer(draw, attr, NULL); - pipe_resource_reference(&vbuffers[attr].buffer, NULL); - } - draw_set_vertex_buffers(draw, 0, NULL); -} - -#endif /* FEATURE_feedback || FEATURE_rastpos */ - +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#include "main/imports.h"
+#include "main/image.h"
+#include "main/macros.h"
+#include "main/mfeatures.h"
+
+#include "vbo/vbo.h"
+
+#include "st_context.h"
+#include "st_atom.h"
+#include "st_cb_bufferobjects.h"
+#include "st_draw.h"
+#include "st_program.h"
+
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "util/u_inlines.h"
+
+#include "draw/draw_private.h"
+#include "draw/draw_context.h"
+
+
+#if FEATURE_feedback || FEATURE_rastpos
+
+/**
+ * Set the (private) draw module's post-transformed vertex format when in
+ * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
+ */
+static void
+set_feedback_vertex_format(struct gl_context *ctx)
+{
+#if 0
+ struct st_context *st = st_context(ctx);
+ struct vertex_info vinfo;
+ GLuint i;
+
+ memset(&vinfo, 0, sizeof(vinfo));
+
+ if (ctx->RenderMode == GL_SELECT) {
+ assert(ctx->RenderMode == GL_SELECT);
+ vinfo.num_attribs = 1;
+ vinfo.format[0] = FORMAT_4F;
+ vinfo.interp_mode[0] = INTERP_LINEAR;
+ }
+ else {
+ /* GL_FEEDBACK, or glRasterPos */
+ /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
+ vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
+ for (i = 0; i < vinfo.num_attribs; i++) {
+ vinfo.format[i] = FORMAT_4F;
+ vinfo.interp_mode[i] = INTERP_LINEAR;
+ }
+ }
+
+ draw_set_vertex_info(st->draw, &vinfo);
+#endif
+}
+
+
+/**
+ * Called by VBO to draw arrays when in selection or feedback mode and
+ * to implement glRasterPos.
+ * This is very much like the normal draw_vbo() function above.
+ * Look at code refactoring some day.
+ * Might move this into the failover module some day.
+ */
+void
+st_feedback_draw_vbo(struct gl_context *ctx,
+ const struct gl_client_array **arrays,
+ const struct _mesa_prim *prims,
+ GLuint nr_prims,
+ const struct _mesa_index_buffer *ib,
+ GLboolean index_bounds_valid,
+ GLuint min_index,
+ GLuint max_index)
+{
+ struct st_context *st = st_context(ctx);
+ struct pipe_context *pipe = st->pipe;
+ struct draw_context *draw = st->draw;
+ const struct st_vertex_program *vp;
+ const struct pipe_shader_state *vs;
+ struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
+ struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
+ struct pipe_index_buffer ibuffer;
+ struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
+ struct pipe_transfer *ib_transfer = NULL;
+ GLuint attr, i;
+ const void *mapped_indices = NULL;
+
+ assert(draw);
+
+ st_validate_state(st);
+
+ if (!index_bounds_valid)
+ vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
+
+ /* must get these after state validation! */
+ vp = st->vp;
+ vs = &st->vp_variant->tgsi;
+
+ if (!st->vp_variant->draw_shader) {
+ st->vp_variant->draw_shader = draw_create_vertex_shader(draw, vs);
+ }
+
+ /*
+ * Set up the draw module's state.
+ *
+ * We'd like to do this less frequently, but the normal state-update
+ * code sends state updates to the pipe, not to our private draw module.
+ */
+ assert(draw);
+ draw_set_viewport_state(draw, &st->state.viewport);
+ draw_set_clip_state(draw, &st->state.clip);
+ draw_set_rasterizer_state(draw, &st->state.rasterizer, NULL);
+ draw_bind_vertex_shader(draw, st->vp_variant->draw_shader);
+ set_feedback_vertex_format(ctx);
+
+ /* loop over TGSI shader inputs to determine vertex buffer
+ * and attribute info
+ */
+ for (attr = 0; attr < vp->num_inputs; attr++) {
+ const GLuint mesaAttr = vp->index_to_input[attr];
+ struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
+ void *map;
+
+ if (bufobj && bufobj->Name) {
+ /* Attribute data is in a VBO.
+ * Recall that for VBOs, the gl_client_array->Ptr field is
+ * really an offset from the start of the VBO, not a pointer.
+ */
+ struct st_buffer_object *stobj = st_buffer_object(bufobj);
+ assert(stobj->buffer);
+
+ vbuffers[attr].buffer = NULL;
+ pipe_resource_reference(&vbuffers[attr].buffer, stobj->buffer);
+ vbuffers[attr].buffer_offset = pointer_to_offset(arrays[0]->Ptr);
+ velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr;
+ }
+ else {
+ /* attribute data is in user-space memory, not a VBO */
+ uint bytes = (arrays[mesaAttr]->Size
+ * _mesa_sizeof_type(arrays[mesaAttr]->Type)
+ * (max_index + 1));
+
+ /* wrap user data */
+ vbuffers[attr].buffer
+ = pipe_user_buffer_create(pipe->screen, (void *) arrays[mesaAttr]->Ptr,
+ bytes,
+ PIPE_BIND_VERTEX_BUFFER);
+ vbuffers[attr].buffer_offset = 0;
+ velements[attr].src_offset = 0;
+ }
+
+ /* common-case setup */
+ vbuffers[attr].stride = arrays[mesaAttr]->StrideB; /* in bytes */
+ velements[attr].instance_divisor = 0;
+ velements[attr].vertex_buffer_index = attr;
+ velements[attr].src_format =
+ st_pipe_vertex_format(arrays[mesaAttr]->Type,
+ arrays[mesaAttr]->Size,
+ arrays[mesaAttr]->Format,
+ arrays[mesaAttr]->Normalized);
+ assert(velements[attr].src_format);
+
+ /* tell draw about this attribute */
+#if 0
+ draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
+#endif
+
+ /* map the attrib buffer */
+ map = pipe_buffer_map(pipe, vbuffers[attr].buffer,
+ PIPE_TRANSFER_READ,
+ &vb_transfer[attr]);
+ draw_set_mapped_vertex_buffer(draw, attr, map);
+ }
+
+ draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers);
+ draw_set_vertex_elements(draw, vp->num_inputs, velements);
+
+ memset(&ibuffer, 0, sizeof(ibuffer));
+ if (ib) {
+ struct gl_buffer_object *bufobj = ib->obj;
+
+ switch (ib->type) {
+ case GL_UNSIGNED_INT:
+ ibuffer.index_size = 4;
+ break;
+ case GL_UNSIGNED_SHORT:
+ ibuffer.index_size = 2;
+ break;
+ case GL_UNSIGNED_BYTE:
+ ibuffer.index_size = 1;
+ break;
+ default:
+ assert(0);
+ goto out_unref_vertex;
+ }
+
+ if (bufobj && bufobj->Name) {
+ struct st_buffer_object *stobj = st_buffer_object(bufobj);
+
+ pipe_resource_reference(&ibuffer.buffer, stobj->buffer);
+ ibuffer.offset = pointer_to_offset(ib->ptr);
+
+ mapped_indices = pipe_buffer_map(pipe, stobj->buffer,
+ PIPE_TRANSFER_READ, &ib_transfer);
+ }
+ else {
+ /* skip setting ibuffer.buffer as the draw module does not use it */
+ mapped_indices = ib->ptr;
+ }
+
+ draw_set_index_buffer(draw, &ibuffer);
+ draw_set_mapped_index_buffer(draw, mapped_indices);
+ }
+
+ /* set the constant buffer */
+ draw_set_mapped_constant_buffer(st->draw, PIPE_SHADER_VERTEX, 0,
+ st->state.constants[PIPE_SHADER_VERTEX].ptr,
+ st->state.constants[PIPE_SHADER_VERTEX].size);
+
+
+ /* draw here */
+ for (i = 0; i < nr_prims; i++) {
+ draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
+ }
+
+
+ /*
+ * unmap vertex/index buffers
+ */
+ if (ib) {
+ draw_set_mapped_index_buffer(draw, NULL);
+ draw_set_index_buffer(draw, NULL);
+
+ if (ib_transfer)
+ pipe_buffer_unmap(pipe, ib_transfer);
+ pipe_resource_reference(&ibuffer.buffer, NULL);
+ }
+
+ out_unref_vertex:
+ for (attr = 0; attr < vp->num_inputs; attr++) {
+ pipe_buffer_unmap(pipe, vb_transfer[attr]);
+ draw_set_mapped_vertex_buffer(draw, attr, NULL);
+ pipe_resource_reference(&vbuffers[attr].buffer, NULL);
+ }
+ draw_set_vertex_buffers(draw, 0, NULL);
+}
+
+#endif /* FEATURE_feedback || FEATURE_rastpos */
+
diff --git a/mesalib/src/mesa/state_tracker/st_format.h b/mesalib/src/mesa/state_tracker/st_format.h index 0fb570f6e..d31e60711 100644 --- a/mesalib/src/mesa/state_tracker/st_format.h +++ b/mesalib/src/mesa/state_tracker/st_format.h @@ -1,86 +1,86 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * Copyright (c) 2010 VMware, Inc. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - - -#ifndef ST_FORMAT_H -#define ST_FORMAT_H - -#include "main/formats.h" -#include "main/glheader.h" - -#include "pipe/p_defines.h" -#include "pipe/p_format.h" - -struct gl_context; -struct pipe_screen; - -extern GLenum -st_format_datatype(enum pipe_format format); - - -extern enum pipe_format -st_mesa_format_to_pipe_format(gl_format mesaFormat); - -extern gl_format -st_pipe_format_to_mesa_format(enum pipe_format pipeFormat); - - -extern enum pipe_format -st_choose_format(struct pipe_screen *screen, GLenum internalFormat, - enum pipe_texture_target target, unsigned sample_count, - unsigned tex_usage); - -extern enum pipe_format -st_choose_renderbuffer_format(struct pipe_screen *screen, - GLenum internalFormat, unsigned sample_count); - - -gl_format -st_ChooseTextureFormat_renderable(struct gl_context *ctx, GLint internalFormat, - GLenum format, GLenum type, GLboolean renderable); - -extern gl_format -st_ChooseTextureFormat(struct gl_context * ctx, GLint internalFormat, - GLenum format, GLenum type); - - -extern GLboolean -st_equal_formats(enum pipe_format pFormat, GLenum format, GLenum type); - -/* can we use a sampler view to translate these formats - only used to make TFP so far */ -extern GLboolean -st_sampler_compat_formats(enum pipe_format format1, enum pipe_format format2); - - -extern void -st_translate_color(const GLfloat colorIn[4], GLenum baseFormat, - GLfloat colorOut[4]); - - -#endif /* ST_FORMAT_H */ +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * Copyright (c) 2010 VMware, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#ifndef ST_FORMAT_H
+#define ST_FORMAT_H
+
+#include "main/formats.h"
+#include "main/glheader.h"
+
+#include "pipe/p_defines.h"
+#include "pipe/p_format.h"
+
+struct gl_context;
+struct pipe_screen;
+
+extern GLenum
+st_format_datatype(enum pipe_format format);
+
+
+extern enum pipe_format
+st_mesa_format_to_pipe_format(gl_format mesaFormat);
+
+extern gl_format
+st_pipe_format_to_mesa_format(enum pipe_format pipeFormat);
+
+
+extern enum pipe_format
+st_choose_format(struct pipe_screen *screen, GLenum internalFormat,
+ enum pipe_texture_target target, unsigned sample_count,
+ unsigned tex_usage);
+
+extern enum pipe_format
+st_choose_renderbuffer_format(struct pipe_screen *screen,
+ GLenum internalFormat, unsigned sample_count);
+
+
+gl_format
+st_ChooseTextureFormat_renderable(struct gl_context *ctx, GLint internalFormat,
+ GLenum format, GLenum type, GLboolean renderable);
+
+extern gl_format
+st_ChooseTextureFormat(struct gl_context * ctx, GLint internalFormat,
+ GLenum format, GLenum type);
+
+
+extern GLboolean
+st_equal_formats(enum pipe_format pFormat, GLenum format, GLenum type);
+
+/* can we use a sampler view to translate these formats
+ only used to make TFP so far */
+extern GLboolean
+st_sampler_compat_formats(enum pipe_format format1, enum pipe_format format2);
+
+
+extern void
+st_translate_color(const GLfloat colorIn[4], GLenum baseFormat,
+ GLfloat colorOut[4]);
+
+
+#endif /* ST_FORMAT_H */
diff --git a/mesalib/src/mesa/state_tracker/st_mesa_to_tgsi.c b/mesalib/src/mesa/state_tracker/st_mesa_to_tgsi.c index c07739f9d..bf0bfd6df 100644 --- a/mesalib/src/mesa/state_tracker/st_mesa_to_tgsi.c +++ b/mesalib/src/mesa/state_tracker/st_mesa_to_tgsi.c @@ -1,1249 +1,1249 @@ -/************************************************************************** - * - * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - -/* - * \author - * Michal Krol, - * Keith Whitwell - */ - -#include "pipe/p_compiler.h" -#include "pipe/p_context.h" -#include "pipe/p_screen.h" -#include "pipe/p_shader_tokens.h" -#include "pipe/p_state.h" -#include "tgsi/tgsi_ureg.h" -#include "st_mesa_to_tgsi.h" -#include "st_context.h" -#include "program/prog_instruction.h" -#include "program/prog_parameter.h" -#include "util/u_debug.h" -#include "util/u_math.h" -#include "util/u_memory.h" - - -#define PROGRAM_ANY_CONST ((1 << PROGRAM_LOCAL_PARAM) | \ - (1 << PROGRAM_ENV_PARAM) | \ - (1 << PROGRAM_STATE_VAR) | \ - (1 << PROGRAM_NAMED_PARAM) | \ - (1 << PROGRAM_CONSTANT) | \ - (1 << PROGRAM_UNIFORM)) - - -struct label { - unsigned branch_target; - unsigned token; -}; - - -/** - * Intermediate state used during shader translation. - */ -struct st_translate { - struct ureg_program *ureg; - - struct ureg_dst temps[MAX_PROGRAM_TEMPS]; - struct ureg_src *constants; - struct ureg_dst outputs[PIPE_MAX_SHADER_OUTPUTS]; - struct ureg_src inputs[PIPE_MAX_SHADER_INPUTS]; - struct ureg_dst address[1]; - struct ureg_src samplers[PIPE_MAX_SAMPLERS]; - struct ureg_src systemValues[SYSTEM_VALUE_MAX]; - - /* Extra info for handling point size clamping in vertex shader */ - struct ureg_dst pointSizeResult; /**< Actual point size output register */ - struct ureg_src pointSizeConst; /**< Point size range constant register */ - GLint pointSizeOutIndex; /**< Temp point size output register */ - GLboolean prevInstWrotePointSize; - - const GLuint *inputMapping; - const GLuint *outputMapping; - - /* For every instruction that contains a label (eg CALL), keep - * details so that we can go back afterwards and emit the correct - * tgsi instruction number for each label. - */ - struct label *labels; - unsigned labels_size; - unsigned labels_count; - - /* Keep a record of the tgsi instruction number that each mesa - * instruction starts at, will be used to fix up labels after - * translation. - */ - unsigned *insn; - unsigned insn_size; - unsigned insn_count; - - unsigned procType; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */ - - boolean error; -}; - - -/** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */ -static unsigned mesa_sysval_to_semantic[SYSTEM_VALUE_MAX] = { - TGSI_SEMANTIC_FACE, - TGSI_SEMANTIC_INSTANCEID -}; - - -/** - * Make note of a branch to a label in the TGSI code. - * After we've emitted all instructions, we'll go over the list - * of labels built here and patch the TGSI code with the actual - * location of each label. - */ -static unsigned *get_label( struct st_translate *t, - unsigned branch_target ) -{ - unsigned i; - - if (t->labels_count + 1 >= t->labels_size) { - unsigned old_size = t->labels_size; - t->labels_size = 1 << (util_logbase2(t->labels_size) + 1); - t->labels = REALLOC( t->labels, - old_size * sizeof t->labels[0], - t->labels_size * sizeof t->labels[0] ); - if (t->labels == NULL) { - static unsigned dummy; - t->error = TRUE; - return &dummy; - } - } - - i = t->labels_count++; - t->labels[i].branch_target = branch_target; - return &t->labels[i].token; -} - - -/** - * Called prior to emitting the TGSI code for each Mesa instruction. - * Allocate additional space for instructions if needed. - * Update the insn[] array so the next Mesa instruction points to - * the next TGSI instruction. - */ -static void set_insn_start( struct st_translate *t, - unsigned start ) -{ - if (t->insn_count + 1 >= t->insn_size) { - unsigned old_size = t->insn_size; - t->insn_size = 1 << (util_logbase2(t->insn_size) + 1); - t->insn = REALLOC( t->insn, - old_size * sizeof t->insn[0], - t->insn_size * sizeof t->insn[0] ); - if (t->insn == NULL) { - t->error = TRUE; - return; - } - } - - t->insn[t->insn_count++] = start; -} - - -/** - * Map a Mesa dst register to a TGSI ureg_dst register. - */ -static struct ureg_dst -dst_register( struct st_translate *t, - gl_register_file file, - GLuint index ) -{ - switch( file ) { - case PROGRAM_UNDEFINED: - return ureg_dst_undef(); - - case PROGRAM_TEMPORARY: - if (ureg_dst_is_undef(t->temps[index])) - t->temps[index] = ureg_DECL_temporary( t->ureg ); - - return t->temps[index]; - - case PROGRAM_OUTPUT: - if (t->procType == TGSI_PROCESSOR_VERTEX && index == VERT_RESULT_PSIZ) - t->prevInstWrotePointSize = GL_TRUE; - - if (t->procType == TGSI_PROCESSOR_VERTEX) - assert(index < VERT_RESULT_MAX); - else if (t->procType == TGSI_PROCESSOR_FRAGMENT) - assert(index < FRAG_RESULT_MAX); - else - assert(index < GEOM_RESULT_MAX); - - assert(t->outputMapping[index] < Elements(t->outputs)); - - return t->outputs[t->outputMapping[index]]; - - case PROGRAM_ADDRESS: - return t->address[index]; - - default: - debug_assert( 0 ); - return ureg_dst_undef(); - } -} - - -/** - * Map a Mesa src register to a TGSI ureg_src register. - */ -static struct ureg_src -src_register( struct st_translate *t, - gl_register_file file, - GLint index ) -{ - switch( file ) { - case PROGRAM_UNDEFINED: - return ureg_src_undef(); - - case PROGRAM_TEMPORARY: - assert(index >= 0); - assert(index < Elements(t->temps)); - if (ureg_dst_is_undef(t->temps[index])) - t->temps[index] = ureg_DECL_temporary( t->ureg ); - return ureg_src(t->temps[index]); - - case PROGRAM_NAMED_PARAM: - case PROGRAM_ENV_PARAM: - case PROGRAM_LOCAL_PARAM: - case PROGRAM_UNIFORM: - assert(index >= 0); - return t->constants[index]; - case PROGRAM_STATE_VAR: - case PROGRAM_CONSTANT: /* ie, immediate */ - if (index < 0) - return ureg_DECL_constant( t->ureg, 0 ); - else - return t->constants[index]; - - case PROGRAM_INPUT: - assert(t->inputMapping[index] < Elements(t->inputs)); - return t->inputs[t->inputMapping[index]]; - - case PROGRAM_OUTPUT: - assert(t->outputMapping[index] < Elements(t->outputs)); - return ureg_src(t->outputs[t->outputMapping[index]]); /* not needed? */ - - case PROGRAM_ADDRESS: - return ureg_src(t->address[index]); - - case PROGRAM_SYSTEM_VALUE: - assert(index < Elements(t->systemValues)); - return t->systemValues[index]; - - default: - debug_assert( 0 ); - return ureg_src_undef(); - } -} - - -/** - * Map mesa texture target to TGSI texture target. - */ -static unsigned -translate_texture_target( GLuint textarget, - GLboolean shadow ) -{ - if (shadow) { - switch( textarget ) { - case TEXTURE_1D_INDEX: return TGSI_TEXTURE_SHADOW1D; - case TEXTURE_2D_INDEX: return TGSI_TEXTURE_SHADOW2D; - case TEXTURE_RECT_INDEX: return TGSI_TEXTURE_SHADOWRECT; - default: break; - } - } - - switch( textarget ) { - case TEXTURE_1D_INDEX: return TGSI_TEXTURE_1D; - case TEXTURE_2D_INDEX: return TGSI_TEXTURE_2D; - case TEXTURE_3D_INDEX: return TGSI_TEXTURE_3D; - case TEXTURE_CUBE_INDEX: return TGSI_TEXTURE_CUBE; - case TEXTURE_RECT_INDEX: return TGSI_TEXTURE_RECT; - case TEXTURE_1D_ARRAY_INDEX: return TGSI_TEXTURE_1D_ARRAY; - case TEXTURE_2D_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY; - default: - debug_assert( 0 ); - return TGSI_TEXTURE_1D; - } -} - - -/** - * Create a TGSI ureg_dst register from a Mesa dest register. - */ -static struct ureg_dst -translate_dst( struct st_translate *t, - const struct prog_dst_register *DstReg, - boolean saturate ) -{ - struct ureg_dst dst = dst_register( t, - DstReg->File, - DstReg->Index ); - - dst = ureg_writemask( dst, - DstReg->WriteMask ); - - if (saturate) - dst = ureg_saturate( dst ); - - if (DstReg->RelAddr) - dst = ureg_dst_indirect( dst, ureg_src(t->address[0]) ); - - return dst; -} - - -/** - * Create a TGSI ureg_src register from a Mesa src register. - */ -static struct ureg_src -translate_src( struct st_translate *t, - const struct prog_src_register *SrcReg ) -{ - struct ureg_src src = src_register( t, SrcReg->File, SrcReg->Index ); - - if (t->procType == TGSI_PROCESSOR_GEOMETRY && SrcReg->HasIndex2) { - src = src_register( t, SrcReg->File, SrcReg->Index2 ); - if (SrcReg->RelAddr2) - src = ureg_src_dimension_indirect( src, ureg_src(t->address[0]), - SrcReg->Index); - else - src = ureg_src_dimension( src, SrcReg->Index); - } - - src = ureg_swizzle( src, - GET_SWZ( SrcReg->Swizzle, 0 ) & 0x3, - GET_SWZ( SrcReg->Swizzle, 1 ) & 0x3, - GET_SWZ( SrcReg->Swizzle, 2 ) & 0x3, - GET_SWZ( SrcReg->Swizzle, 3 ) & 0x3); - - if (SrcReg->Negate == NEGATE_XYZW) - src = ureg_negate(src); - - if (SrcReg->Abs) - src = ureg_abs(src); - - if (SrcReg->RelAddr) { - src = ureg_src_indirect( src, ureg_src(t->address[0])); - if (SrcReg->File != PROGRAM_INPUT && - SrcReg->File != PROGRAM_OUTPUT) { - /* If SrcReg->Index was negative, it was set to zero in - * src_register(). Reassign it now. But don't do this - * for input/output regs since they get remapped while - * const buffers don't. - */ - src.Index = SrcReg->Index; - } - } - - return src; -} - - -static struct ureg_src swizzle_4v( struct ureg_src src, - const unsigned *swz ) -{ - return ureg_swizzle( src, swz[0], swz[1], swz[2], swz[3] ); -} - - -/** - * Translate a SWZ instruction into a MOV, MUL or MAD instruction. EG: - * - * SWZ dst, src.x-y10 - * - * becomes: - * - * MAD dst {1,-1,0,0}, src.xyxx, {0,0,1,0} - */ -static void emit_swz( struct st_translate *t, - struct ureg_dst dst, - const struct prog_src_register *SrcReg ) -{ - struct ureg_program *ureg = t->ureg; - struct ureg_src src = src_register( t, SrcReg->File, SrcReg->Index ); - - unsigned negate_mask = SrcReg->Negate; - - unsigned one_mask = ((GET_SWZ(SrcReg->Swizzle, 0) == SWIZZLE_ONE) << 0 | - (GET_SWZ(SrcReg->Swizzle, 1) == SWIZZLE_ONE) << 1 | - (GET_SWZ(SrcReg->Swizzle, 2) == SWIZZLE_ONE) << 2 | - (GET_SWZ(SrcReg->Swizzle, 3) == SWIZZLE_ONE) << 3); - - unsigned zero_mask = ((GET_SWZ(SrcReg->Swizzle, 0) == SWIZZLE_ZERO) << 0 | - (GET_SWZ(SrcReg->Swizzle, 1) == SWIZZLE_ZERO) << 1 | - (GET_SWZ(SrcReg->Swizzle, 2) == SWIZZLE_ZERO) << 2 | - (GET_SWZ(SrcReg->Swizzle, 3) == SWIZZLE_ZERO) << 3); - - unsigned negative_one_mask = one_mask & negate_mask; - unsigned positive_one_mask = one_mask & ~negate_mask; - - struct ureg_src imm; - unsigned i; - unsigned mul_swizzle[4] = {0,0,0,0}; - unsigned add_swizzle[4] = {0,0,0,0}; - unsigned src_swizzle[4] = {0,0,0,0}; - boolean need_add = FALSE; - boolean need_mul = FALSE; - - if (dst.WriteMask == 0) - return; - - /* Is this just a MOV? - */ - if (zero_mask == 0 && - one_mask == 0 && - (negate_mask == 0 || negate_mask == TGSI_WRITEMASK_XYZW)) - { - ureg_MOV( ureg, dst, translate_src( t, SrcReg )); - return; - } - -#define IMM_ZERO 0 -#define IMM_ONE 1 -#define IMM_NEG_ONE 2 - - imm = ureg_imm3f( ureg, 0, 1, -1 ); - - for (i = 0; i < 4; i++) { - unsigned bit = 1 << i; - - if (dst.WriteMask & bit) { - if (positive_one_mask & bit) { - mul_swizzle[i] = IMM_ZERO; - add_swizzle[i] = IMM_ONE; - need_add = TRUE; - } - else if (negative_one_mask & bit) { - mul_swizzle[i] = IMM_ZERO; - add_swizzle[i] = IMM_NEG_ONE; - need_add = TRUE; - } - else if (zero_mask & bit) { - mul_swizzle[i] = IMM_ZERO; - add_swizzle[i] = IMM_ZERO; - need_add = TRUE; - } - else { - add_swizzle[i] = IMM_ZERO; - src_swizzle[i] = GET_SWZ(SrcReg->Swizzle, i); - need_mul = TRUE; - if (negate_mask & bit) { - mul_swizzle[i] = IMM_NEG_ONE; - } - else { - mul_swizzle[i] = IMM_ONE; - } - } - } - } - - if (need_mul && need_add) { - ureg_MAD( ureg, - dst, - swizzle_4v( src, src_swizzle ), - swizzle_4v( imm, mul_swizzle ), - swizzle_4v( imm, add_swizzle ) ); - } - else if (need_mul) { - ureg_MUL( ureg, - dst, - swizzle_4v( src, src_swizzle ), - swizzle_4v( imm, mul_swizzle ) ); - } - else if (need_add) { - ureg_MOV( ureg, - dst, - swizzle_4v( imm, add_swizzle ) ); - } - else { - debug_assert(0); - } - -#undef IMM_ZERO -#undef IMM_ONE -#undef IMM_NEG_ONE -} - - -/** - * Negate the value of DDY to match GL semantics where (0,0) is the - * lower-left corner of the window. - * Note that the GL_ARB_fragment_coord_conventions extension will - * effect this someday. - */ -static void emit_ddy( struct st_translate *t, - struct ureg_dst dst, - const struct prog_src_register *SrcReg ) -{ - struct ureg_program *ureg = t->ureg; - struct ureg_src src = translate_src( t, SrcReg ); - src = ureg_negate( src ); - ureg_DDY( ureg, dst, src ); -} - - - -static unsigned -translate_opcode( unsigned op ) -{ - switch( op ) { - case OPCODE_ARL: - return TGSI_OPCODE_ARL; - case OPCODE_ABS: - return TGSI_OPCODE_ABS; - case OPCODE_ADD: - return TGSI_OPCODE_ADD; - case OPCODE_BGNLOOP: - return TGSI_OPCODE_BGNLOOP; - case OPCODE_BGNSUB: - return TGSI_OPCODE_BGNSUB; - case OPCODE_BRA: - return TGSI_OPCODE_BRA; - case OPCODE_BRK: - return TGSI_OPCODE_BRK; - case OPCODE_CAL: - return TGSI_OPCODE_CAL; - case OPCODE_CMP: - return TGSI_OPCODE_CMP; - case OPCODE_CONT: - return TGSI_OPCODE_CONT; - case OPCODE_COS: - return TGSI_OPCODE_COS; - case OPCODE_DDX: - return TGSI_OPCODE_DDX; - case OPCODE_DDY: - return TGSI_OPCODE_DDY; - case OPCODE_DP2: - return TGSI_OPCODE_DP2; - case OPCODE_DP2A: - return TGSI_OPCODE_DP2A; - case OPCODE_DP3: - return TGSI_OPCODE_DP3; - case OPCODE_DP4: - return TGSI_OPCODE_DP4; - case OPCODE_DPH: - return TGSI_OPCODE_DPH; - case OPCODE_DST: - return TGSI_OPCODE_DST; - case OPCODE_ELSE: - return TGSI_OPCODE_ELSE; - case OPCODE_EMIT_VERTEX: - return TGSI_OPCODE_EMIT; - case OPCODE_END_PRIMITIVE: - return TGSI_OPCODE_ENDPRIM; - case OPCODE_ENDIF: - return TGSI_OPCODE_ENDIF; - case OPCODE_ENDLOOP: - return TGSI_OPCODE_ENDLOOP; - case OPCODE_ENDSUB: - return TGSI_OPCODE_ENDSUB; - case OPCODE_EX2: - return TGSI_OPCODE_EX2; - case OPCODE_EXP: - return TGSI_OPCODE_EXP; - case OPCODE_FLR: - return TGSI_OPCODE_FLR; - case OPCODE_FRC: - return TGSI_OPCODE_FRC; - case OPCODE_IF: - return TGSI_OPCODE_IF; - case OPCODE_TRUNC: - return TGSI_OPCODE_TRUNC; - case OPCODE_KIL: - return TGSI_OPCODE_KIL; - case OPCODE_KIL_NV: - return TGSI_OPCODE_KILP; - case OPCODE_LG2: - return TGSI_OPCODE_LG2; - case OPCODE_LOG: - return TGSI_OPCODE_LOG; - case OPCODE_LIT: - return TGSI_OPCODE_LIT; - case OPCODE_LRP: - return TGSI_OPCODE_LRP; - case OPCODE_MAD: - return TGSI_OPCODE_MAD; - case OPCODE_MAX: - return TGSI_OPCODE_MAX; - case OPCODE_MIN: - return TGSI_OPCODE_MIN; - case OPCODE_MOV: - return TGSI_OPCODE_MOV; - case OPCODE_MUL: - return TGSI_OPCODE_MUL; - case OPCODE_NOP: - return TGSI_OPCODE_NOP; - case OPCODE_NRM3: - return TGSI_OPCODE_NRM; - case OPCODE_NRM4: - return TGSI_OPCODE_NRM4; - case OPCODE_POW: - return TGSI_OPCODE_POW; - case OPCODE_RCP: - return TGSI_OPCODE_RCP; - case OPCODE_RET: - return TGSI_OPCODE_RET; - case OPCODE_RSQ: - return TGSI_OPCODE_RSQ; - case OPCODE_SCS: - return TGSI_OPCODE_SCS; - case OPCODE_SEQ: - return TGSI_OPCODE_SEQ; - case OPCODE_SGE: - return TGSI_OPCODE_SGE; - case OPCODE_SGT: - return TGSI_OPCODE_SGT; - case OPCODE_SIN: - return TGSI_OPCODE_SIN; - case OPCODE_SLE: - return TGSI_OPCODE_SLE; - case OPCODE_SLT: - return TGSI_OPCODE_SLT; - case OPCODE_SNE: - return TGSI_OPCODE_SNE; - case OPCODE_SSG: - return TGSI_OPCODE_SSG; - case OPCODE_SUB: - return TGSI_OPCODE_SUB; - case OPCODE_TEX: - return TGSI_OPCODE_TEX; - case OPCODE_TXB: - return TGSI_OPCODE_TXB; - case OPCODE_TXD: - return TGSI_OPCODE_TXD; - case OPCODE_TXL: - return TGSI_OPCODE_TXL; - case OPCODE_TXP: - return TGSI_OPCODE_TXP; - case OPCODE_XPD: - return TGSI_OPCODE_XPD; - case OPCODE_END: - return TGSI_OPCODE_END; - default: - debug_assert( 0 ); - return TGSI_OPCODE_NOP; - } -} - - -static void -compile_instruction( - struct st_translate *t, - const struct prog_instruction *inst ) -{ - struct ureg_program *ureg = t->ureg; - GLuint i; - struct ureg_dst dst[1]; - struct ureg_src src[4]; - unsigned num_dst; - unsigned num_src; - - num_dst = _mesa_num_inst_dst_regs( inst->Opcode ); - num_src = _mesa_num_inst_src_regs( inst->Opcode ); - - if (num_dst) - dst[0] = translate_dst( t, - &inst->DstReg, - inst->SaturateMode ); - - for (i = 0; i < num_src; i++) - src[i] = translate_src( t, &inst->SrcReg[i] ); - - switch( inst->Opcode ) { - case OPCODE_SWZ: - emit_swz( t, dst[0], &inst->SrcReg[0] ); - return; - - case OPCODE_BGNLOOP: - case OPCODE_CAL: - case OPCODE_ELSE: - case OPCODE_ENDLOOP: - case OPCODE_IF: - debug_assert(num_dst == 0); - ureg_label_insn( ureg, - translate_opcode( inst->Opcode ), - src, num_src, - get_label( t, inst->BranchTarget )); - return; - - case OPCODE_TEX: - case OPCODE_TXB: - case OPCODE_TXD: - case OPCODE_TXL: - case OPCODE_TXP: - src[num_src++] = t->samplers[inst->TexSrcUnit]; - ureg_tex_insn( ureg, - translate_opcode( inst->Opcode ), - dst, num_dst, - translate_texture_target( inst->TexSrcTarget, - inst->TexShadow ), - src, num_src ); - return; - - case OPCODE_SCS: - dst[0] = ureg_writemask(dst[0], TGSI_WRITEMASK_XY ); - ureg_insn( ureg, - translate_opcode( inst->Opcode ), - dst, num_dst, - src, num_src ); - break; - - case OPCODE_XPD: - dst[0] = ureg_writemask(dst[0], TGSI_WRITEMASK_XYZ ); - ureg_insn( ureg, - translate_opcode( inst->Opcode ), - dst, num_dst, - src, num_src ); - break; - - case OPCODE_NOISE1: - case OPCODE_NOISE2: - case OPCODE_NOISE3: - case OPCODE_NOISE4: - /* At some point, a motivated person could add a better - * implementation of noise. Currently not even the nvidia - * binary drivers do anything more than this. In any case, the - * place to do this is in the GL state tracker, not the poor - * driver. - */ - ureg_MOV( ureg, dst[0], ureg_imm1f(ureg, 0.5) ); - break; - - case OPCODE_DDY: - emit_ddy( t, dst[0], &inst->SrcReg[0] ); - break; - - default: - ureg_insn( ureg, - translate_opcode( inst->Opcode ), - dst, num_dst, - src, num_src ); - break; - } -} - - -/** - * Emit the TGSI instructions to adjust the WPOS pixel center convention - * Basically, add (adjX, adjY) to the fragment position. - */ -static void -emit_adjusted_wpos( struct st_translate *t, - const struct gl_program *program, - GLfloat adjX, GLfloat adjY) -{ - struct ureg_program *ureg = t->ureg; - struct ureg_dst wpos_temp = ureg_DECL_temporary(ureg); - struct ureg_src wpos_input = t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]]; - - /* Note that we bias X and Y and pass Z and W through unchanged. - * The shader might also use gl_FragCoord.w and .z. - */ - ureg_ADD(ureg, wpos_temp, wpos_input, - ureg_imm4f(ureg, adjX, adjY, 0.0f, 0.0f)); - - t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]] = ureg_src(wpos_temp); -} - - -/** - * Emit the TGSI instructions for inverting the WPOS y coordinate. - * This code is unavoidable because it also depends on whether - * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM). - */ -static void -emit_wpos_inversion( struct st_translate *t, - const struct gl_program *program, - boolean invert) -{ - struct ureg_program *ureg = t->ureg; - - /* Fragment program uses fragment position input. - * Need to replace instances of INPUT[WPOS] with temp T - * where T = INPUT[WPOS] by y is inverted. - */ - static const gl_state_index wposTransformState[STATE_LENGTH] - = { STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM, 0, 0, 0 }; - - /* XXX: note we are modifying the incoming shader here! Need to - * do this before emitting the constant decls below, or this - * will be missed: - */ - unsigned wposTransConst = _mesa_add_state_reference(program->Parameters, - wposTransformState); - - struct ureg_src wpostrans = ureg_DECL_constant( ureg, wposTransConst ); - struct ureg_dst wpos_temp; - struct ureg_src wpos_input = t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]]; - - /* MOV wpos_temp, input[wpos] - */ - if (wpos_input.File == TGSI_FILE_TEMPORARY) - wpos_temp = ureg_dst(wpos_input); - else { - wpos_temp = ureg_DECL_temporary( ureg ); - ureg_MOV( ureg, wpos_temp, wpos_input ); - } - - if (invert) { - /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy - */ - ureg_MAD( ureg, - ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y ), - wpos_input, - ureg_scalar(wpostrans, 0), - ureg_scalar(wpostrans, 1)); - } else { - /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww - */ - ureg_MAD( ureg, - ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y ), - wpos_input, - ureg_scalar(wpostrans, 2), - ureg_scalar(wpostrans, 3)); - } - - /* Use wpos_temp as position input from here on: - */ - t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]] = ureg_src(wpos_temp); -} - - -/** - * Emit fragment position/ooordinate code. - */ -static void -emit_wpos(struct st_context *st, - struct st_translate *t, - const struct gl_program *program, - struct ureg_program *ureg) -{ - const struct gl_fragment_program *fp = - (const struct gl_fragment_program *) program; - struct pipe_screen *pscreen = st->pipe->screen; - boolean invert = FALSE; - - if (fp->OriginUpperLeft) { - /* Fragment shader wants origin in upper-left */ - if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT)) { - /* the driver supports upper-left origin */ - } - else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT)) { - /* the driver supports lower-left origin, need to invert Y */ - ureg_property_fs_coord_origin(ureg, TGSI_FS_COORD_ORIGIN_LOWER_LEFT); - invert = TRUE; - } - else - assert(0); - } - else { - /* Fragment shader wants origin in lower-left */ - if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT)) - /* the driver supports lower-left origin */ - ureg_property_fs_coord_origin(ureg, TGSI_FS_COORD_ORIGIN_LOWER_LEFT); - else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT)) - /* the driver supports upper-left origin, need to invert Y */ - invert = TRUE; - else - assert(0); - } - - if (fp->PixelCenterInteger) { - /* Fragment shader wants pixel center integer */ - if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER)) - /* the driver supports pixel center integer */ - ureg_property_fs_coord_pixel_center(ureg, TGSI_FS_COORD_PIXEL_CENTER_INTEGER); - else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER)) - /* the driver supports pixel center half integer, need to bias X,Y */ - emit_adjusted_wpos(t, program, 0.5f, invert ? 0.5f : -0.5f); - else - assert(0); - } - else { - /* Fragment shader wants pixel center half integer */ - if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER)) { - /* the driver supports pixel center half integer */ - } - else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER)) { - /* the driver supports pixel center integer, need to bias X,Y */ - ureg_property_fs_coord_pixel_center(ureg, TGSI_FS_COORD_PIXEL_CENTER_INTEGER); - emit_adjusted_wpos(t, program, 0.5f, invert ? -0.5f : 0.5f); - } - else - assert(0); - } - - /* we invert after adjustment so that we avoid the MOV to temporary, - * and reuse the adjustment ADD instead */ - emit_wpos_inversion(t, program, invert); -} - - -/** - * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back. - * TGSI uses +1 for front, -1 for back. - * This function converts the TGSI value to the GL value. Simply clamping/ - * saturating the value to [0,1] does the job. - */ -static void -emit_face_var( struct st_translate *t, - const struct gl_program *program ) -{ - struct ureg_program *ureg = t->ureg; - struct ureg_dst face_temp = ureg_DECL_temporary( ureg ); - struct ureg_src face_input = t->inputs[t->inputMapping[FRAG_ATTRIB_FACE]]; - - /* MOV_SAT face_temp, input[face] - */ - face_temp = ureg_saturate( face_temp ); - ureg_MOV( ureg, face_temp, face_input ); - - /* Use face_temp as face input from here on: - */ - t->inputs[t->inputMapping[FRAG_ATTRIB_FACE]] = ureg_src(face_temp); -} - - -static void -emit_edgeflags( struct st_translate *t, - const struct gl_program *program ) -{ - struct ureg_program *ureg = t->ureg; - struct ureg_dst edge_dst = t->outputs[t->outputMapping[VERT_RESULT_EDGE]]; - struct ureg_src edge_src = t->inputs[t->inputMapping[VERT_ATTRIB_EDGEFLAG]]; - - ureg_MOV( ureg, edge_dst, edge_src ); -} - - -/** - * Translate Mesa program to TGSI format. - * \param program the program to translate - * \param numInputs number of input registers used - * \param inputMapping maps Mesa fragment program inputs to TGSI generic - * input indexes - * \param inputSemanticName the TGSI_SEMANTIC flag for each input - * \param inputSemanticIndex the semantic index (ex: which texcoord) for - * each input - * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input - * \param numOutputs number of output registers used - * \param outputMapping maps Mesa fragment program outputs to TGSI - * generic outputs - * \param outputSemanticName the TGSI_SEMANTIC flag for each output - * \param outputSemanticIndex the semantic index (ex: which texcoord) for - * each output - * - * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY - */ -enum pipe_error -st_translate_mesa_program( - struct gl_context *ctx, - uint procType, - struct ureg_program *ureg, - const struct gl_program *program, - GLuint numInputs, - const GLuint inputMapping[], - const ubyte inputSemanticName[], - const ubyte inputSemanticIndex[], - const GLuint interpMode[], - GLuint numOutputs, - const GLuint outputMapping[], - const ubyte outputSemanticName[], - const ubyte outputSemanticIndex[], - boolean passthrough_edgeflags ) -{ - struct st_translate translate, *t; - unsigned i; - enum pipe_error ret = PIPE_OK; - - assert(numInputs <= Elements(t->inputs)); - assert(numOutputs <= Elements(t->outputs)); - - t = &translate; - memset(t, 0, sizeof *t); - - t->procType = procType; - t->inputMapping = inputMapping; - t->outputMapping = outputMapping; - t->ureg = ureg; - t->pointSizeOutIndex = -1; - t->prevInstWrotePointSize = GL_FALSE; - - /*_mesa_print_program(program);*/ - - /* - * Declare input attributes. - */ - if (procType == TGSI_PROCESSOR_FRAGMENT) { - for (i = 0; i < numInputs; i++) { - if (program->InputFlags[0] & PROG_PARAM_BIT_CYL_WRAP) { - t->inputs[i] = ureg_DECL_fs_input_cyl(ureg, - inputSemanticName[i], - inputSemanticIndex[i], - interpMode[i], - TGSI_CYLINDRICAL_WRAP_X); - } - else { - t->inputs[i] = ureg_DECL_fs_input(ureg, - inputSemanticName[i], - inputSemanticIndex[i], - interpMode[i]); - } - } - - if (program->InputsRead & FRAG_BIT_WPOS) { - /* Must do this after setting up t->inputs, and before - * emitting constant references, below: - */ - emit_wpos(st_context(ctx), t, program, ureg); - } - - if (program->InputsRead & FRAG_BIT_FACE) { - emit_face_var( t, program ); - } - - /* - * Declare output attributes. - */ - for (i = 0; i < numOutputs; i++) { - switch (outputSemanticName[i]) { - case TGSI_SEMANTIC_POSITION: - t->outputs[i] = ureg_DECL_output( ureg, - TGSI_SEMANTIC_POSITION, /* Z / Depth */ - outputSemanticIndex[i] ); - - t->outputs[i] = ureg_writemask( t->outputs[i], - TGSI_WRITEMASK_Z ); - break; - case TGSI_SEMANTIC_STENCIL: - t->outputs[i] = ureg_DECL_output( ureg, - TGSI_SEMANTIC_STENCIL, /* Stencil */ - outputSemanticIndex[i] ); - t->outputs[i] = ureg_writemask( t->outputs[i], - TGSI_WRITEMASK_Y ); - break; - case TGSI_SEMANTIC_COLOR: - t->outputs[i] = ureg_DECL_output( ureg, - TGSI_SEMANTIC_COLOR, - outputSemanticIndex[i] ); - break; - default: - debug_assert(0); - return 0; - } - } - } - else if (procType == TGSI_PROCESSOR_GEOMETRY) { - for (i = 0; i < numInputs; i++) { - t->inputs[i] = ureg_DECL_gs_input(ureg, - i, - inputSemanticName[i], - inputSemanticIndex[i]); - } - - for (i = 0; i < numOutputs; i++) { - t->outputs[i] = ureg_DECL_output( ureg, - outputSemanticName[i], - outputSemanticIndex[i] ); - } - } - else { - assert(procType == TGSI_PROCESSOR_VERTEX); - - for (i = 0; i < numInputs; i++) { - t->inputs[i] = ureg_DECL_vs_input(ureg, i); - } - - for (i = 0; i < numOutputs; i++) { - t->outputs[i] = ureg_DECL_output( ureg, - outputSemanticName[i], - outputSemanticIndex[i] ); - if ((outputSemanticName[i] == TGSI_SEMANTIC_PSIZE) && program->Id) { - /* Writing to the point size result register requires special - * handling to implement clamping. - */ - static const gl_state_index pointSizeClampState[STATE_LENGTH] - = { STATE_INTERNAL, STATE_POINT_SIZE_IMPL_CLAMP, 0, 0, 0 }; - /* XXX: note we are modifying the incoming shader here! Need to - * do this before emitting the constant decls below, or this - * will be missed: - */ - unsigned pointSizeClampConst = - _mesa_add_state_reference(program->Parameters, - pointSizeClampState); - struct ureg_dst psizregtemp = ureg_DECL_temporary( ureg ); - t->pointSizeConst = ureg_DECL_constant( ureg, pointSizeClampConst ); - t->pointSizeResult = t->outputs[i]; - t->pointSizeOutIndex = i; - t->outputs[i] = psizregtemp; - } - } - if (passthrough_edgeflags) - emit_edgeflags( t, program ); - } - - /* Declare address register. - */ - if (program->NumAddressRegs > 0) { - debug_assert( program->NumAddressRegs == 1 ); - t->address[0] = ureg_DECL_address( ureg ); - } - - /* Declare misc input registers - */ - { - GLbitfield sysInputs = program->SystemValuesRead; - unsigned numSys = 0; - for (i = 0; sysInputs; i++) { - if (sysInputs & (1 << i)) { - unsigned semName = mesa_sysval_to_semantic[i]; - t->systemValues[i] = ureg_DECL_system_value(ureg, numSys, semName, 0); - numSys++; - sysInputs &= ~(1 << i); - } - } - } - - if (program->IndirectRegisterFiles & (1 << PROGRAM_TEMPORARY)) { - /* If temps are accessed with indirect addressing, declare temporaries - * in sequential order. Else, we declare them on demand elsewhere. - */ - for (i = 0; i < program->NumTemporaries; i++) { - /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */ - t->temps[i] = ureg_DECL_temporary( t->ureg ); - } - } - - /* Emit constants and immediates. Mesa uses a single index space - * for these, so we put all the translated regs in t->constants. - */ - if (program->Parameters) { - t->constants = CALLOC( program->Parameters->NumParameters, - sizeof t->constants[0] ); - if (t->constants == NULL) { - ret = PIPE_ERROR_OUT_OF_MEMORY; - goto out; - } - - for (i = 0; i < program->Parameters->NumParameters; i++) { - switch (program->Parameters->Parameters[i].Type) { - case PROGRAM_ENV_PARAM: - case PROGRAM_LOCAL_PARAM: - case PROGRAM_STATE_VAR: - case PROGRAM_NAMED_PARAM: - case PROGRAM_UNIFORM: - t->constants[i] = ureg_DECL_constant( ureg, i ); - break; - - /* Emit immediates only when there's no indirect addressing of - * the const buffer. - * FIXME: Be smarter and recognize param arrays: - * indirect addressing is only valid within the referenced - * array. - */ - case PROGRAM_CONSTANT: - if (program->IndirectRegisterFiles & PROGRAM_ANY_CONST) - t->constants[i] = ureg_DECL_constant( ureg, i ); - else - t->constants[i] = - ureg_DECL_immediate( ureg, - program->Parameters->ParameterValues[i], - 4 ); - break; - default: - break; - } - } - } - - /* texture samplers */ - for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) { - if (program->SamplersUsed & (1 << i)) { - t->samplers[i] = ureg_DECL_sampler( ureg, i ); - } - } - - /* Emit each instruction in turn: - */ - for (i = 0; i < program->NumInstructions; i++) { - set_insn_start( t, ureg_get_instruction_number( ureg )); - compile_instruction( t, &program->Instructions[i] ); - - if (t->prevInstWrotePointSize && program->Id) { - /* The previous instruction wrote to the (fake) vertex point size - * result register. Now we need to clamp that value to the min/max - * point size range, putting the result into the real point size - * register. - * Note that we can't do this easily at the end of program due to - * possible early return. - */ - set_insn_start( t, ureg_get_instruction_number( ureg )); - ureg_MAX( t->ureg, - ureg_writemask(t->outputs[t->pointSizeOutIndex], WRITEMASK_X), - ureg_src(t->outputs[t->pointSizeOutIndex]), - ureg_swizzle(t->pointSizeConst, 1,1,1,1)); - ureg_MIN( t->ureg, ureg_writemask(t->pointSizeResult, WRITEMASK_X), - ureg_src(t->outputs[t->pointSizeOutIndex]), - ureg_swizzle(t->pointSizeConst, 2,2,2,2)); - } - t->prevInstWrotePointSize = GL_FALSE; - } - - /* Fix up all emitted labels: - */ - for (i = 0; i < t->labels_count; i++) { - ureg_fixup_label( ureg, - t->labels[i].token, - t->insn[t->labels[i].branch_target] ); - } - -out: - FREE(t->insn); - FREE(t->labels); - FREE(t->constants); - - if (t->error) { - debug_printf("%s: translate error flag set\n", __FUNCTION__); - } - - return ret; -} - - -/** - * Tokens cannot be free with free otherwise the builtin gallium - * malloc debugging will get confused. - */ -void -st_free_tokens(const struct tgsi_token *tokens) -{ - FREE((void *)tokens); -} +/**************************************************************************
+ *
+ * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+/*
+ * \author
+ * Michal Krol,
+ * Keith Whitwell
+ */
+
+#include "pipe/p_compiler.h"
+#include "pipe/p_context.h"
+#include "pipe/p_screen.h"
+#include "pipe/p_shader_tokens.h"
+#include "pipe/p_state.h"
+#include "tgsi/tgsi_ureg.h"
+#include "st_mesa_to_tgsi.h"
+#include "st_context.h"
+#include "program/prog_instruction.h"
+#include "program/prog_parameter.h"
+#include "util/u_debug.h"
+#include "util/u_math.h"
+#include "util/u_memory.h"
+
+
+#define PROGRAM_ANY_CONST ((1 << PROGRAM_LOCAL_PARAM) | \
+ (1 << PROGRAM_ENV_PARAM) | \
+ (1 << PROGRAM_STATE_VAR) | \
+ (1 << PROGRAM_NAMED_PARAM) | \
+ (1 << PROGRAM_CONSTANT) | \
+ (1 << PROGRAM_UNIFORM))
+
+
+struct label {
+ unsigned branch_target;
+ unsigned token;
+};
+
+
+/**
+ * Intermediate state used during shader translation.
+ */
+struct st_translate {
+ struct ureg_program *ureg;
+
+ struct ureg_dst temps[MAX_PROGRAM_TEMPS];
+ struct ureg_src *constants;
+ struct ureg_dst outputs[PIPE_MAX_SHADER_OUTPUTS];
+ struct ureg_src inputs[PIPE_MAX_SHADER_INPUTS];
+ struct ureg_dst address[1];
+ struct ureg_src samplers[PIPE_MAX_SAMPLERS];
+ struct ureg_src systemValues[SYSTEM_VALUE_MAX];
+
+ /* Extra info for handling point size clamping in vertex shader */
+ struct ureg_dst pointSizeResult; /**< Actual point size output register */
+ struct ureg_src pointSizeConst; /**< Point size range constant register */
+ GLint pointSizeOutIndex; /**< Temp point size output register */
+ GLboolean prevInstWrotePointSize;
+
+ const GLuint *inputMapping;
+ const GLuint *outputMapping;
+
+ /* For every instruction that contains a label (eg CALL), keep
+ * details so that we can go back afterwards and emit the correct
+ * tgsi instruction number for each label.
+ */
+ struct label *labels;
+ unsigned labels_size;
+ unsigned labels_count;
+
+ /* Keep a record of the tgsi instruction number that each mesa
+ * instruction starts at, will be used to fix up labels after
+ * translation.
+ */
+ unsigned *insn;
+ unsigned insn_size;
+ unsigned insn_count;
+
+ unsigned procType; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
+
+ boolean error;
+};
+
+
+/** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
+static unsigned mesa_sysval_to_semantic[SYSTEM_VALUE_MAX] = {
+ TGSI_SEMANTIC_FACE,
+ TGSI_SEMANTIC_INSTANCEID
+};
+
+
+/**
+ * Make note of a branch to a label in the TGSI code.
+ * After we've emitted all instructions, we'll go over the list
+ * of labels built here and patch the TGSI code with the actual
+ * location of each label.
+ */
+static unsigned *get_label( struct st_translate *t,
+ unsigned branch_target )
+{
+ unsigned i;
+
+ if (t->labels_count + 1 >= t->labels_size) {
+ unsigned old_size = t->labels_size;
+ t->labels_size = 1 << (util_logbase2(t->labels_size) + 1);
+ t->labels = REALLOC( t->labels,
+ old_size * sizeof t->labels[0],
+ t->labels_size * sizeof t->labels[0] );
+ if (t->labels == NULL) {
+ static unsigned dummy;
+ t->error = TRUE;
+ return &dummy;
+ }
+ }
+
+ i = t->labels_count++;
+ t->labels[i].branch_target = branch_target;
+ return &t->labels[i].token;
+}
+
+
+/**
+ * Called prior to emitting the TGSI code for each Mesa instruction.
+ * Allocate additional space for instructions if needed.
+ * Update the insn[] array so the next Mesa instruction points to
+ * the next TGSI instruction.
+ */
+static void set_insn_start( struct st_translate *t,
+ unsigned start )
+{
+ if (t->insn_count + 1 >= t->insn_size) {
+ unsigned old_size = t->insn_size;
+ t->insn_size = 1 << (util_logbase2(t->insn_size) + 1);
+ t->insn = REALLOC( t->insn,
+ old_size * sizeof t->insn[0],
+ t->insn_size * sizeof t->insn[0] );
+ if (t->insn == NULL) {
+ t->error = TRUE;
+ return;
+ }
+ }
+
+ t->insn[t->insn_count++] = start;
+}
+
+
+/**
+ * Map a Mesa dst register to a TGSI ureg_dst register.
+ */
+static struct ureg_dst
+dst_register( struct st_translate *t,
+ gl_register_file file,
+ GLuint index )
+{
+ switch( file ) {
+ case PROGRAM_UNDEFINED:
+ return ureg_dst_undef();
+
+ case PROGRAM_TEMPORARY:
+ if (ureg_dst_is_undef(t->temps[index]))
+ t->temps[index] = ureg_DECL_temporary( t->ureg );
+
+ return t->temps[index];
+
+ case PROGRAM_OUTPUT:
+ if (t->procType == TGSI_PROCESSOR_VERTEX && index == VERT_RESULT_PSIZ)
+ t->prevInstWrotePointSize = GL_TRUE;
+
+ if (t->procType == TGSI_PROCESSOR_VERTEX)
+ assert(index < VERT_RESULT_MAX);
+ else if (t->procType == TGSI_PROCESSOR_FRAGMENT)
+ assert(index < FRAG_RESULT_MAX);
+ else
+ assert(index < GEOM_RESULT_MAX);
+
+ assert(t->outputMapping[index] < Elements(t->outputs));
+
+ return t->outputs[t->outputMapping[index]];
+
+ case PROGRAM_ADDRESS:
+ return t->address[index];
+
+ default:
+ debug_assert( 0 );
+ return ureg_dst_undef();
+ }
+}
+
+
+/**
+ * Map a Mesa src register to a TGSI ureg_src register.
+ */
+static struct ureg_src
+src_register( struct st_translate *t,
+ gl_register_file file,
+ GLint index )
+{
+ switch( file ) {
+ case PROGRAM_UNDEFINED:
+ return ureg_src_undef();
+
+ case PROGRAM_TEMPORARY:
+ assert(index >= 0);
+ assert(index < Elements(t->temps));
+ if (ureg_dst_is_undef(t->temps[index]))
+ t->temps[index] = ureg_DECL_temporary( t->ureg );
+ return ureg_src(t->temps[index]);
+
+ case PROGRAM_NAMED_PARAM:
+ case PROGRAM_ENV_PARAM:
+ case PROGRAM_LOCAL_PARAM:
+ case PROGRAM_UNIFORM:
+ assert(index >= 0);
+ return t->constants[index];
+ case PROGRAM_STATE_VAR:
+ case PROGRAM_CONSTANT: /* ie, immediate */
+ if (index < 0)
+ return ureg_DECL_constant( t->ureg, 0 );
+ else
+ return t->constants[index];
+
+ case PROGRAM_INPUT:
+ assert(t->inputMapping[index] < Elements(t->inputs));
+ return t->inputs[t->inputMapping[index]];
+
+ case PROGRAM_OUTPUT:
+ assert(t->outputMapping[index] < Elements(t->outputs));
+ return ureg_src(t->outputs[t->outputMapping[index]]); /* not needed? */
+
+ case PROGRAM_ADDRESS:
+ return ureg_src(t->address[index]);
+
+ case PROGRAM_SYSTEM_VALUE:
+ assert(index < Elements(t->systemValues));
+ return t->systemValues[index];
+
+ default:
+ debug_assert( 0 );
+ return ureg_src_undef();
+ }
+}
+
+
+/**
+ * Map mesa texture target to TGSI texture target.
+ */
+static unsigned
+translate_texture_target( GLuint textarget,
+ GLboolean shadow )
+{
+ if (shadow) {
+ switch( textarget ) {
+ case TEXTURE_1D_INDEX: return TGSI_TEXTURE_SHADOW1D;
+ case TEXTURE_2D_INDEX: return TGSI_TEXTURE_SHADOW2D;
+ case TEXTURE_RECT_INDEX: return TGSI_TEXTURE_SHADOWRECT;
+ default: break;
+ }
+ }
+
+ switch( textarget ) {
+ case TEXTURE_1D_INDEX: return TGSI_TEXTURE_1D;
+ case TEXTURE_2D_INDEX: return TGSI_TEXTURE_2D;
+ case TEXTURE_3D_INDEX: return TGSI_TEXTURE_3D;
+ case TEXTURE_CUBE_INDEX: return TGSI_TEXTURE_CUBE;
+ case TEXTURE_RECT_INDEX: return TGSI_TEXTURE_RECT;
+ case TEXTURE_1D_ARRAY_INDEX: return TGSI_TEXTURE_1D_ARRAY;
+ case TEXTURE_2D_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY;
+ default:
+ debug_assert( 0 );
+ return TGSI_TEXTURE_1D;
+ }
+}
+
+
+/**
+ * Create a TGSI ureg_dst register from a Mesa dest register.
+ */
+static struct ureg_dst
+translate_dst( struct st_translate *t,
+ const struct prog_dst_register *DstReg,
+ boolean saturate )
+{
+ struct ureg_dst dst = dst_register( t,
+ DstReg->File,
+ DstReg->Index );
+
+ dst = ureg_writemask( dst,
+ DstReg->WriteMask );
+
+ if (saturate)
+ dst = ureg_saturate( dst );
+
+ if (DstReg->RelAddr)
+ dst = ureg_dst_indirect( dst, ureg_src(t->address[0]) );
+
+ return dst;
+}
+
+
+/**
+ * Create a TGSI ureg_src register from a Mesa src register.
+ */
+static struct ureg_src
+translate_src( struct st_translate *t,
+ const struct prog_src_register *SrcReg )
+{
+ struct ureg_src src = src_register( t, SrcReg->File, SrcReg->Index );
+
+ if (t->procType == TGSI_PROCESSOR_GEOMETRY && SrcReg->HasIndex2) {
+ src = src_register( t, SrcReg->File, SrcReg->Index2 );
+ if (SrcReg->RelAddr2)
+ src = ureg_src_dimension_indirect( src, ureg_src(t->address[0]),
+ SrcReg->Index);
+ else
+ src = ureg_src_dimension( src, SrcReg->Index);
+ }
+
+ src = ureg_swizzle( src,
+ GET_SWZ( SrcReg->Swizzle, 0 ) & 0x3,
+ GET_SWZ( SrcReg->Swizzle, 1 ) & 0x3,
+ GET_SWZ( SrcReg->Swizzle, 2 ) & 0x3,
+ GET_SWZ( SrcReg->Swizzle, 3 ) & 0x3);
+
+ if (SrcReg->Negate == NEGATE_XYZW)
+ src = ureg_negate(src);
+
+ if (SrcReg->Abs)
+ src = ureg_abs(src);
+
+ if (SrcReg->RelAddr) {
+ src = ureg_src_indirect( src, ureg_src(t->address[0]));
+ if (SrcReg->File != PROGRAM_INPUT &&
+ SrcReg->File != PROGRAM_OUTPUT) {
+ /* If SrcReg->Index was negative, it was set to zero in
+ * src_register(). Reassign it now. But don't do this
+ * for input/output regs since they get remapped while
+ * const buffers don't.
+ */
+ src.Index = SrcReg->Index;
+ }
+ }
+
+ return src;
+}
+
+
+static struct ureg_src swizzle_4v( struct ureg_src src,
+ const unsigned *swz )
+{
+ return ureg_swizzle( src, swz[0], swz[1], swz[2], swz[3] );
+}
+
+
+/**
+ * Translate a SWZ instruction into a MOV, MUL or MAD instruction. EG:
+ *
+ * SWZ dst, src.x-y10
+ *
+ * becomes:
+ *
+ * MAD dst {1,-1,0,0}, src.xyxx, {0,0,1,0}
+ */
+static void emit_swz( struct st_translate *t,
+ struct ureg_dst dst,
+ const struct prog_src_register *SrcReg )
+{
+ struct ureg_program *ureg = t->ureg;
+ struct ureg_src src = src_register( t, SrcReg->File, SrcReg->Index );
+
+ unsigned negate_mask = SrcReg->Negate;
+
+ unsigned one_mask = ((GET_SWZ(SrcReg->Swizzle, 0) == SWIZZLE_ONE) << 0 |
+ (GET_SWZ(SrcReg->Swizzle, 1) == SWIZZLE_ONE) << 1 |
+ (GET_SWZ(SrcReg->Swizzle, 2) == SWIZZLE_ONE) << 2 |
+ (GET_SWZ(SrcReg->Swizzle, 3) == SWIZZLE_ONE) << 3);
+
+ unsigned zero_mask = ((GET_SWZ(SrcReg->Swizzle, 0) == SWIZZLE_ZERO) << 0 |
+ (GET_SWZ(SrcReg->Swizzle, 1) == SWIZZLE_ZERO) << 1 |
+ (GET_SWZ(SrcReg->Swizzle, 2) == SWIZZLE_ZERO) << 2 |
+ (GET_SWZ(SrcReg->Swizzle, 3) == SWIZZLE_ZERO) << 3);
+
+ unsigned negative_one_mask = one_mask & negate_mask;
+ unsigned positive_one_mask = one_mask & ~negate_mask;
+
+ struct ureg_src imm;
+ unsigned i;
+ unsigned mul_swizzle[4] = {0,0,0,0};
+ unsigned add_swizzle[4] = {0,0,0,0};
+ unsigned src_swizzle[4] = {0,0,0,0};
+ boolean need_add = FALSE;
+ boolean need_mul = FALSE;
+
+ if (dst.WriteMask == 0)
+ return;
+
+ /* Is this just a MOV?
+ */
+ if (zero_mask == 0 &&
+ one_mask == 0 &&
+ (negate_mask == 0 || negate_mask == TGSI_WRITEMASK_XYZW))
+ {
+ ureg_MOV( ureg, dst, translate_src( t, SrcReg ));
+ return;
+ }
+
+#define IMM_ZERO 0
+#define IMM_ONE 1
+#define IMM_NEG_ONE 2
+
+ imm = ureg_imm3f( ureg, 0, 1, -1 );
+
+ for (i = 0; i < 4; i++) {
+ unsigned bit = 1 << i;
+
+ if (dst.WriteMask & bit) {
+ if (positive_one_mask & bit) {
+ mul_swizzle[i] = IMM_ZERO;
+ add_swizzle[i] = IMM_ONE;
+ need_add = TRUE;
+ }
+ else if (negative_one_mask & bit) {
+ mul_swizzle[i] = IMM_ZERO;
+ add_swizzle[i] = IMM_NEG_ONE;
+ need_add = TRUE;
+ }
+ else if (zero_mask & bit) {
+ mul_swizzle[i] = IMM_ZERO;
+ add_swizzle[i] = IMM_ZERO;
+ need_add = TRUE;
+ }
+ else {
+ add_swizzle[i] = IMM_ZERO;
+ src_swizzle[i] = GET_SWZ(SrcReg->Swizzle, i);
+ need_mul = TRUE;
+ if (negate_mask & bit) {
+ mul_swizzle[i] = IMM_NEG_ONE;
+ }
+ else {
+ mul_swizzle[i] = IMM_ONE;
+ }
+ }
+ }
+ }
+
+ if (need_mul && need_add) {
+ ureg_MAD( ureg,
+ dst,
+ swizzle_4v( src, src_swizzle ),
+ swizzle_4v( imm, mul_swizzle ),
+ swizzle_4v( imm, add_swizzle ) );
+ }
+ else if (need_mul) {
+ ureg_MUL( ureg,
+ dst,
+ swizzle_4v( src, src_swizzle ),
+ swizzle_4v( imm, mul_swizzle ) );
+ }
+ else if (need_add) {
+ ureg_MOV( ureg,
+ dst,
+ swizzle_4v( imm, add_swizzle ) );
+ }
+ else {
+ debug_assert(0);
+ }
+
+#undef IMM_ZERO
+#undef IMM_ONE
+#undef IMM_NEG_ONE
+}
+
+
+/**
+ * Negate the value of DDY to match GL semantics where (0,0) is the
+ * lower-left corner of the window.
+ * Note that the GL_ARB_fragment_coord_conventions extension will
+ * effect this someday.
+ */
+static void emit_ddy( struct st_translate *t,
+ struct ureg_dst dst,
+ const struct prog_src_register *SrcReg )
+{
+ struct ureg_program *ureg = t->ureg;
+ struct ureg_src src = translate_src( t, SrcReg );
+ src = ureg_negate( src );
+ ureg_DDY( ureg, dst, src );
+}
+
+
+
+static unsigned
+translate_opcode( unsigned op )
+{
+ switch( op ) {
+ case OPCODE_ARL:
+ return TGSI_OPCODE_ARL;
+ case OPCODE_ABS:
+ return TGSI_OPCODE_ABS;
+ case OPCODE_ADD:
+ return TGSI_OPCODE_ADD;
+ case OPCODE_BGNLOOP:
+ return TGSI_OPCODE_BGNLOOP;
+ case OPCODE_BGNSUB:
+ return TGSI_OPCODE_BGNSUB;
+ case OPCODE_BRA:
+ return TGSI_OPCODE_BRA;
+ case OPCODE_BRK:
+ return TGSI_OPCODE_BRK;
+ case OPCODE_CAL:
+ return TGSI_OPCODE_CAL;
+ case OPCODE_CMP:
+ return TGSI_OPCODE_CMP;
+ case OPCODE_CONT:
+ return TGSI_OPCODE_CONT;
+ case OPCODE_COS:
+ return TGSI_OPCODE_COS;
+ case OPCODE_DDX:
+ return TGSI_OPCODE_DDX;
+ case OPCODE_DDY:
+ return TGSI_OPCODE_DDY;
+ case OPCODE_DP2:
+ return TGSI_OPCODE_DP2;
+ case OPCODE_DP2A:
+ return TGSI_OPCODE_DP2A;
+ case OPCODE_DP3:
+ return TGSI_OPCODE_DP3;
+ case OPCODE_DP4:
+ return TGSI_OPCODE_DP4;
+ case OPCODE_DPH:
+ return TGSI_OPCODE_DPH;
+ case OPCODE_DST:
+ return TGSI_OPCODE_DST;
+ case OPCODE_ELSE:
+ return TGSI_OPCODE_ELSE;
+ case OPCODE_EMIT_VERTEX:
+ return TGSI_OPCODE_EMIT;
+ case OPCODE_END_PRIMITIVE:
+ return TGSI_OPCODE_ENDPRIM;
+ case OPCODE_ENDIF:
+ return TGSI_OPCODE_ENDIF;
+ case OPCODE_ENDLOOP:
+ return TGSI_OPCODE_ENDLOOP;
+ case OPCODE_ENDSUB:
+ return TGSI_OPCODE_ENDSUB;
+ case OPCODE_EX2:
+ return TGSI_OPCODE_EX2;
+ case OPCODE_EXP:
+ return TGSI_OPCODE_EXP;
+ case OPCODE_FLR:
+ return TGSI_OPCODE_FLR;
+ case OPCODE_FRC:
+ return TGSI_OPCODE_FRC;
+ case OPCODE_IF:
+ return TGSI_OPCODE_IF;
+ case OPCODE_TRUNC:
+ return TGSI_OPCODE_TRUNC;
+ case OPCODE_KIL:
+ return TGSI_OPCODE_KIL;
+ case OPCODE_KIL_NV:
+ return TGSI_OPCODE_KILP;
+ case OPCODE_LG2:
+ return TGSI_OPCODE_LG2;
+ case OPCODE_LOG:
+ return TGSI_OPCODE_LOG;
+ case OPCODE_LIT:
+ return TGSI_OPCODE_LIT;
+ case OPCODE_LRP:
+ return TGSI_OPCODE_LRP;
+ case OPCODE_MAD:
+ return TGSI_OPCODE_MAD;
+ case OPCODE_MAX:
+ return TGSI_OPCODE_MAX;
+ case OPCODE_MIN:
+ return TGSI_OPCODE_MIN;
+ case OPCODE_MOV:
+ return TGSI_OPCODE_MOV;
+ case OPCODE_MUL:
+ return TGSI_OPCODE_MUL;
+ case OPCODE_NOP:
+ return TGSI_OPCODE_NOP;
+ case OPCODE_NRM3:
+ return TGSI_OPCODE_NRM;
+ case OPCODE_NRM4:
+ return TGSI_OPCODE_NRM4;
+ case OPCODE_POW:
+ return TGSI_OPCODE_POW;
+ case OPCODE_RCP:
+ return TGSI_OPCODE_RCP;
+ case OPCODE_RET:
+ return TGSI_OPCODE_RET;
+ case OPCODE_RSQ:
+ return TGSI_OPCODE_RSQ;
+ case OPCODE_SCS:
+ return TGSI_OPCODE_SCS;
+ case OPCODE_SEQ:
+ return TGSI_OPCODE_SEQ;
+ case OPCODE_SGE:
+ return TGSI_OPCODE_SGE;
+ case OPCODE_SGT:
+ return TGSI_OPCODE_SGT;
+ case OPCODE_SIN:
+ return TGSI_OPCODE_SIN;
+ case OPCODE_SLE:
+ return TGSI_OPCODE_SLE;
+ case OPCODE_SLT:
+ return TGSI_OPCODE_SLT;
+ case OPCODE_SNE:
+ return TGSI_OPCODE_SNE;
+ case OPCODE_SSG:
+ return TGSI_OPCODE_SSG;
+ case OPCODE_SUB:
+ return TGSI_OPCODE_SUB;
+ case OPCODE_TEX:
+ return TGSI_OPCODE_TEX;
+ case OPCODE_TXB:
+ return TGSI_OPCODE_TXB;
+ case OPCODE_TXD:
+ return TGSI_OPCODE_TXD;
+ case OPCODE_TXL:
+ return TGSI_OPCODE_TXL;
+ case OPCODE_TXP:
+ return TGSI_OPCODE_TXP;
+ case OPCODE_XPD:
+ return TGSI_OPCODE_XPD;
+ case OPCODE_END:
+ return TGSI_OPCODE_END;
+ default:
+ debug_assert( 0 );
+ return TGSI_OPCODE_NOP;
+ }
+}
+
+
+static void
+compile_instruction(
+ struct st_translate *t,
+ const struct prog_instruction *inst )
+{
+ struct ureg_program *ureg = t->ureg;
+ GLuint i;
+ struct ureg_dst dst[1];
+ struct ureg_src src[4];
+ unsigned num_dst;
+ unsigned num_src;
+
+ num_dst = _mesa_num_inst_dst_regs( inst->Opcode );
+ num_src = _mesa_num_inst_src_regs( inst->Opcode );
+
+ if (num_dst)
+ dst[0] = translate_dst( t,
+ &inst->DstReg,
+ inst->SaturateMode );
+
+ for (i = 0; i < num_src; i++)
+ src[i] = translate_src( t, &inst->SrcReg[i] );
+
+ switch( inst->Opcode ) {
+ case OPCODE_SWZ:
+ emit_swz( t, dst[0], &inst->SrcReg[0] );
+ return;
+
+ case OPCODE_BGNLOOP:
+ case OPCODE_CAL:
+ case OPCODE_ELSE:
+ case OPCODE_ENDLOOP:
+ case OPCODE_IF:
+ debug_assert(num_dst == 0);
+ ureg_label_insn( ureg,
+ translate_opcode( inst->Opcode ),
+ src, num_src,
+ get_label( t, inst->BranchTarget ));
+ return;
+
+ case OPCODE_TEX:
+ case OPCODE_TXB:
+ case OPCODE_TXD:
+ case OPCODE_TXL:
+ case OPCODE_TXP:
+ src[num_src++] = t->samplers[inst->TexSrcUnit];
+ ureg_tex_insn( ureg,
+ translate_opcode( inst->Opcode ),
+ dst, num_dst,
+ translate_texture_target( inst->TexSrcTarget,
+ inst->TexShadow ),
+ src, num_src );
+ return;
+
+ case OPCODE_SCS:
+ dst[0] = ureg_writemask(dst[0], TGSI_WRITEMASK_XY );
+ ureg_insn( ureg,
+ translate_opcode( inst->Opcode ),
+ dst, num_dst,
+ src, num_src );
+ break;
+
+ case OPCODE_XPD:
+ dst[0] = ureg_writemask(dst[0], TGSI_WRITEMASK_XYZ );
+ ureg_insn( ureg,
+ translate_opcode( inst->Opcode ),
+ dst, num_dst,
+ src, num_src );
+ break;
+
+ case OPCODE_NOISE1:
+ case OPCODE_NOISE2:
+ case OPCODE_NOISE3:
+ case OPCODE_NOISE4:
+ /* At some point, a motivated person could add a better
+ * implementation of noise. Currently not even the nvidia
+ * binary drivers do anything more than this. In any case, the
+ * place to do this is in the GL state tracker, not the poor
+ * driver.
+ */
+ ureg_MOV( ureg, dst[0], ureg_imm1f(ureg, 0.5) );
+ break;
+
+ case OPCODE_DDY:
+ emit_ddy( t, dst[0], &inst->SrcReg[0] );
+ break;
+
+ default:
+ ureg_insn( ureg,
+ translate_opcode( inst->Opcode ),
+ dst, num_dst,
+ src, num_src );
+ break;
+ }
+}
+
+
+/**
+ * Emit the TGSI instructions to adjust the WPOS pixel center convention
+ * Basically, add (adjX, adjY) to the fragment position.
+ */
+static void
+emit_adjusted_wpos( struct st_translate *t,
+ const struct gl_program *program,
+ GLfloat adjX, GLfloat adjY)
+{
+ struct ureg_program *ureg = t->ureg;
+ struct ureg_dst wpos_temp = ureg_DECL_temporary(ureg);
+ struct ureg_src wpos_input = t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]];
+
+ /* Note that we bias X and Y and pass Z and W through unchanged.
+ * The shader might also use gl_FragCoord.w and .z.
+ */
+ ureg_ADD(ureg, wpos_temp, wpos_input,
+ ureg_imm4f(ureg, adjX, adjY, 0.0f, 0.0f));
+
+ t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]] = ureg_src(wpos_temp);
+}
+
+
+/**
+ * Emit the TGSI instructions for inverting the WPOS y coordinate.
+ * This code is unavoidable because it also depends on whether
+ * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
+ */
+static void
+emit_wpos_inversion( struct st_translate *t,
+ const struct gl_program *program,
+ boolean invert)
+{
+ struct ureg_program *ureg = t->ureg;
+
+ /* Fragment program uses fragment position input.
+ * Need to replace instances of INPUT[WPOS] with temp T
+ * where T = INPUT[WPOS] by y is inverted.
+ */
+ static const gl_state_index wposTransformState[STATE_LENGTH]
+ = { STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM, 0, 0, 0 };
+
+ /* XXX: note we are modifying the incoming shader here! Need to
+ * do this before emitting the constant decls below, or this
+ * will be missed:
+ */
+ unsigned wposTransConst = _mesa_add_state_reference(program->Parameters,
+ wposTransformState);
+
+ struct ureg_src wpostrans = ureg_DECL_constant( ureg, wposTransConst );
+ struct ureg_dst wpos_temp;
+ struct ureg_src wpos_input = t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]];
+
+ /* MOV wpos_temp, input[wpos]
+ */
+ if (wpos_input.File == TGSI_FILE_TEMPORARY)
+ wpos_temp = ureg_dst(wpos_input);
+ else {
+ wpos_temp = ureg_DECL_temporary( ureg );
+ ureg_MOV( ureg, wpos_temp, wpos_input );
+ }
+
+ if (invert) {
+ /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
+ */
+ ureg_MAD( ureg,
+ ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y ),
+ wpos_input,
+ ureg_scalar(wpostrans, 0),
+ ureg_scalar(wpostrans, 1));
+ } else {
+ /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
+ */
+ ureg_MAD( ureg,
+ ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y ),
+ wpos_input,
+ ureg_scalar(wpostrans, 2),
+ ureg_scalar(wpostrans, 3));
+ }
+
+ /* Use wpos_temp as position input from here on:
+ */
+ t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]] = ureg_src(wpos_temp);
+}
+
+
+/**
+ * Emit fragment position/ooordinate code.
+ */
+static void
+emit_wpos(struct st_context *st,
+ struct st_translate *t,
+ const struct gl_program *program,
+ struct ureg_program *ureg)
+{
+ const struct gl_fragment_program *fp =
+ (const struct gl_fragment_program *) program;
+ struct pipe_screen *pscreen = st->pipe->screen;
+ boolean invert = FALSE;
+
+ if (fp->OriginUpperLeft) {
+ /* Fragment shader wants origin in upper-left */
+ if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT)) {
+ /* the driver supports upper-left origin */
+ }
+ else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT)) {
+ /* the driver supports lower-left origin, need to invert Y */
+ ureg_property_fs_coord_origin(ureg, TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
+ invert = TRUE;
+ }
+ else
+ assert(0);
+ }
+ else {
+ /* Fragment shader wants origin in lower-left */
+ if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT))
+ /* the driver supports lower-left origin */
+ ureg_property_fs_coord_origin(ureg, TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
+ else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT))
+ /* the driver supports upper-left origin, need to invert Y */
+ invert = TRUE;
+ else
+ assert(0);
+ }
+
+ if (fp->PixelCenterInteger) {
+ /* Fragment shader wants pixel center integer */
+ if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER))
+ /* the driver supports pixel center integer */
+ ureg_property_fs_coord_pixel_center(ureg, TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
+ else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER))
+ /* the driver supports pixel center half integer, need to bias X,Y */
+ emit_adjusted_wpos(t, program, 0.5f, invert ? 0.5f : -0.5f);
+ else
+ assert(0);
+ }
+ else {
+ /* Fragment shader wants pixel center half integer */
+ if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER)) {
+ /* the driver supports pixel center half integer */
+ }
+ else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER)) {
+ /* the driver supports pixel center integer, need to bias X,Y */
+ ureg_property_fs_coord_pixel_center(ureg, TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
+ emit_adjusted_wpos(t, program, 0.5f, invert ? -0.5f : 0.5f);
+ }
+ else
+ assert(0);
+ }
+
+ /* we invert after adjustment so that we avoid the MOV to temporary,
+ * and reuse the adjustment ADD instead */
+ emit_wpos_inversion(t, program, invert);
+}
+
+
+/**
+ * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
+ * TGSI uses +1 for front, -1 for back.
+ * This function converts the TGSI value to the GL value. Simply clamping/
+ * saturating the value to [0,1] does the job.
+ */
+static void
+emit_face_var( struct st_translate *t,
+ const struct gl_program *program )
+{
+ struct ureg_program *ureg = t->ureg;
+ struct ureg_dst face_temp = ureg_DECL_temporary( ureg );
+ struct ureg_src face_input = t->inputs[t->inputMapping[FRAG_ATTRIB_FACE]];
+
+ /* MOV_SAT face_temp, input[face]
+ */
+ face_temp = ureg_saturate( face_temp );
+ ureg_MOV( ureg, face_temp, face_input );
+
+ /* Use face_temp as face input from here on:
+ */
+ t->inputs[t->inputMapping[FRAG_ATTRIB_FACE]] = ureg_src(face_temp);
+}
+
+
+static void
+emit_edgeflags( struct st_translate *t,
+ const struct gl_program *program )
+{
+ struct ureg_program *ureg = t->ureg;
+ struct ureg_dst edge_dst = t->outputs[t->outputMapping[VERT_RESULT_EDGE]];
+ struct ureg_src edge_src = t->inputs[t->inputMapping[VERT_ATTRIB_EDGEFLAG]];
+
+ ureg_MOV( ureg, edge_dst, edge_src );
+}
+
+
+/**
+ * Translate Mesa program to TGSI format.
+ * \param program the program to translate
+ * \param numInputs number of input registers used
+ * \param inputMapping maps Mesa fragment program inputs to TGSI generic
+ * input indexes
+ * \param inputSemanticName the TGSI_SEMANTIC flag for each input
+ * \param inputSemanticIndex the semantic index (ex: which texcoord) for
+ * each input
+ * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
+ * \param numOutputs number of output registers used
+ * \param outputMapping maps Mesa fragment program outputs to TGSI
+ * generic outputs
+ * \param outputSemanticName the TGSI_SEMANTIC flag for each output
+ * \param outputSemanticIndex the semantic index (ex: which texcoord) for
+ * each output
+ *
+ * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
+ */
+enum pipe_error
+st_translate_mesa_program(
+ struct gl_context *ctx,
+ uint procType,
+ struct ureg_program *ureg,
+ const struct gl_program *program,
+ GLuint numInputs,
+ const GLuint inputMapping[],
+ const ubyte inputSemanticName[],
+ const ubyte inputSemanticIndex[],
+ const GLuint interpMode[],
+ GLuint numOutputs,
+ const GLuint outputMapping[],
+ const ubyte outputSemanticName[],
+ const ubyte outputSemanticIndex[],
+ boolean passthrough_edgeflags )
+{
+ struct st_translate translate, *t;
+ unsigned i;
+ enum pipe_error ret = PIPE_OK;
+
+ assert(numInputs <= Elements(t->inputs));
+ assert(numOutputs <= Elements(t->outputs));
+
+ t = &translate;
+ memset(t, 0, sizeof *t);
+
+ t->procType = procType;
+ t->inputMapping = inputMapping;
+ t->outputMapping = outputMapping;
+ t->ureg = ureg;
+ t->pointSizeOutIndex = -1;
+ t->prevInstWrotePointSize = GL_FALSE;
+
+ /*_mesa_print_program(program);*/
+
+ /*
+ * Declare input attributes.
+ */
+ if (procType == TGSI_PROCESSOR_FRAGMENT) {
+ for (i = 0; i < numInputs; i++) {
+ if (program->InputFlags[0] & PROG_PARAM_BIT_CYL_WRAP) {
+ t->inputs[i] = ureg_DECL_fs_input_cyl(ureg,
+ inputSemanticName[i],
+ inputSemanticIndex[i],
+ interpMode[i],
+ TGSI_CYLINDRICAL_WRAP_X);
+ }
+ else {
+ t->inputs[i] = ureg_DECL_fs_input(ureg,
+ inputSemanticName[i],
+ inputSemanticIndex[i],
+ interpMode[i]);
+ }
+ }
+
+ if (program->InputsRead & FRAG_BIT_WPOS) {
+ /* Must do this after setting up t->inputs, and before
+ * emitting constant references, below:
+ */
+ emit_wpos(st_context(ctx), t, program, ureg);
+ }
+
+ if (program->InputsRead & FRAG_BIT_FACE) {
+ emit_face_var( t, program );
+ }
+
+ /*
+ * Declare output attributes.
+ */
+ for (i = 0; i < numOutputs; i++) {
+ switch (outputSemanticName[i]) {
+ case TGSI_SEMANTIC_POSITION:
+ t->outputs[i] = ureg_DECL_output( ureg,
+ TGSI_SEMANTIC_POSITION, /* Z / Depth */
+ outputSemanticIndex[i] );
+
+ t->outputs[i] = ureg_writemask( t->outputs[i],
+ TGSI_WRITEMASK_Z );
+ break;
+ case TGSI_SEMANTIC_STENCIL:
+ t->outputs[i] = ureg_DECL_output( ureg,
+ TGSI_SEMANTIC_STENCIL, /* Stencil */
+ outputSemanticIndex[i] );
+ t->outputs[i] = ureg_writemask( t->outputs[i],
+ TGSI_WRITEMASK_Y );
+ break;
+ case TGSI_SEMANTIC_COLOR:
+ t->outputs[i] = ureg_DECL_output( ureg,
+ TGSI_SEMANTIC_COLOR,
+ outputSemanticIndex[i] );
+ break;
+ default:
+ debug_assert(0);
+ return 0;
+ }
+ }
+ }
+ else if (procType == TGSI_PROCESSOR_GEOMETRY) {
+ for (i = 0; i < numInputs; i++) {
+ t->inputs[i] = ureg_DECL_gs_input(ureg,
+ i,
+ inputSemanticName[i],
+ inputSemanticIndex[i]);
+ }
+
+ for (i = 0; i < numOutputs; i++) {
+ t->outputs[i] = ureg_DECL_output( ureg,
+ outputSemanticName[i],
+ outputSemanticIndex[i] );
+ }
+ }
+ else {
+ assert(procType == TGSI_PROCESSOR_VERTEX);
+
+ for (i = 0; i < numInputs; i++) {
+ t->inputs[i] = ureg_DECL_vs_input(ureg, i);
+ }
+
+ for (i = 0; i < numOutputs; i++) {
+ t->outputs[i] = ureg_DECL_output( ureg,
+ outputSemanticName[i],
+ outputSemanticIndex[i] );
+ if ((outputSemanticName[i] == TGSI_SEMANTIC_PSIZE) && program->Id) {
+ /* Writing to the point size result register requires special
+ * handling to implement clamping.
+ */
+ static const gl_state_index pointSizeClampState[STATE_LENGTH]
+ = { STATE_INTERNAL, STATE_POINT_SIZE_IMPL_CLAMP, 0, 0, 0 };
+ /* XXX: note we are modifying the incoming shader here! Need to
+ * do this before emitting the constant decls below, or this
+ * will be missed:
+ */
+ unsigned pointSizeClampConst =
+ _mesa_add_state_reference(program->Parameters,
+ pointSizeClampState);
+ struct ureg_dst psizregtemp = ureg_DECL_temporary( ureg );
+ t->pointSizeConst = ureg_DECL_constant( ureg, pointSizeClampConst );
+ t->pointSizeResult = t->outputs[i];
+ t->pointSizeOutIndex = i;
+ t->outputs[i] = psizregtemp;
+ }
+ }
+ if (passthrough_edgeflags)
+ emit_edgeflags( t, program );
+ }
+
+ /* Declare address register.
+ */
+ if (program->NumAddressRegs > 0) {
+ debug_assert( program->NumAddressRegs == 1 );
+ t->address[0] = ureg_DECL_address( ureg );
+ }
+
+ /* Declare misc input registers
+ */
+ {
+ GLbitfield sysInputs = program->SystemValuesRead;
+ unsigned numSys = 0;
+ for (i = 0; sysInputs; i++) {
+ if (sysInputs & (1 << i)) {
+ unsigned semName = mesa_sysval_to_semantic[i];
+ t->systemValues[i] = ureg_DECL_system_value(ureg, numSys, semName, 0);
+ numSys++;
+ sysInputs &= ~(1 << i);
+ }
+ }
+ }
+
+ if (program->IndirectRegisterFiles & (1 << PROGRAM_TEMPORARY)) {
+ /* If temps are accessed with indirect addressing, declare temporaries
+ * in sequential order. Else, we declare them on demand elsewhere.
+ */
+ for (i = 0; i < program->NumTemporaries; i++) {
+ /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
+ t->temps[i] = ureg_DECL_temporary( t->ureg );
+ }
+ }
+
+ /* Emit constants and immediates. Mesa uses a single index space
+ * for these, so we put all the translated regs in t->constants.
+ */
+ if (program->Parameters) {
+ t->constants = CALLOC( program->Parameters->NumParameters,
+ sizeof t->constants[0] );
+ if (t->constants == NULL) {
+ ret = PIPE_ERROR_OUT_OF_MEMORY;
+ goto out;
+ }
+
+ for (i = 0; i < program->Parameters->NumParameters; i++) {
+ switch (program->Parameters->Parameters[i].Type) {
+ case PROGRAM_ENV_PARAM:
+ case PROGRAM_LOCAL_PARAM:
+ case PROGRAM_STATE_VAR:
+ case PROGRAM_NAMED_PARAM:
+ case PROGRAM_UNIFORM:
+ t->constants[i] = ureg_DECL_constant( ureg, i );
+ break;
+
+ /* Emit immediates only when there's no indirect addressing of
+ * the const buffer.
+ * FIXME: Be smarter and recognize param arrays:
+ * indirect addressing is only valid within the referenced
+ * array.
+ */
+ case PROGRAM_CONSTANT:
+ if (program->IndirectRegisterFiles & PROGRAM_ANY_CONST)
+ t->constants[i] = ureg_DECL_constant( ureg, i );
+ else
+ t->constants[i] =
+ ureg_DECL_immediate( ureg,
+ program->Parameters->ParameterValues[i],
+ 4 );
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ /* texture samplers */
+ for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
+ if (program->SamplersUsed & (1 << i)) {
+ t->samplers[i] = ureg_DECL_sampler( ureg, i );
+ }
+ }
+
+ /* Emit each instruction in turn:
+ */
+ for (i = 0; i < program->NumInstructions; i++) {
+ set_insn_start( t, ureg_get_instruction_number( ureg ));
+ compile_instruction( t, &program->Instructions[i] );
+
+ if (t->prevInstWrotePointSize && program->Id) {
+ /* The previous instruction wrote to the (fake) vertex point size
+ * result register. Now we need to clamp that value to the min/max
+ * point size range, putting the result into the real point size
+ * register.
+ * Note that we can't do this easily at the end of program due to
+ * possible early return.
+ */
+ set_insn_start( t, ureg_get_instruction_number( ureg ));
+ ureg_MAX( t->ureg,
+ ureg_writemask(t->outputs[t->pointSizeOutIndex], WRITEMASK_X),
+ ureg_src(t->outputs[t->pointSizeOutIndex]),
+ ureg_swizzle(t->pointSizeConst, 1,1,1,1));
+ ureg_MIN( t->ureg, ureg_writemask(t->pointSizeResult, WRITEMASK_X),
+ ureg_src(t->outputs[t->pointSizeOutIndex]),
+ ureg_swizzle(t->pointSizeConst, 2,2,2,2));
+ }
+ t->prevInstWrotePointSize = GL_FALSE;
+ }
+
+ /* Fix up all emitted labels:
+ */
+ for (i = 0; i < t->labels_count; i++) {
+ ureg_fixup_label( ureg,
+ t->labels[i].token,
+ t->insn[t->labels[i].branch_target] );
+ }
+
+out:
+ FREE(t->insn);
+ FREE(t->labels);
+ FREE(t->constants);
+
+ if (t->error) {
+ debug_printf("%s: translate error flag set\n", __FUNCTION__);
+ }
+
+ return ret;
+}
+
+
+/**
+ * Tokens cannot be free with free otherwise the builtin gallium
+ * malloc debugging will get confused.
+ */
+void
+st_free_tokens(const struct tgsi_token *tokens)
+{
+ FREE((void *)tokens);
+}
diff --git a/mesalib/src/mesa/state_tracker/st_program.c b/mesalib/src/mesa/state_tracker/st_program.c index 0b1ad63af..1efc58799 100644 --- a/mesalib/src/mesa/state_tracker/st_program.c +++ b/mesalib/src/mesa/state_tracker/st_program.c @@ -1,1165 +1,1165 @@ -/************************************************************************** - * - * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - /* - * Authors: - * Keith Whitwell <keith@tungstengraphics.com> - * Brian Paul - */ - - -#include "main/imports.h" -#include "main/hash.h" -#include "main/mfeatures.h" -#include "main/mtypes.h" -#include "program/prog_parameter.h" -#include "program/prog_print.h" -#include "program/programopt.h" - -#include "pipe/p_context.h" -#include "pipe/p_defines.h" -#include "pipe/p_shader_tokens.h" -#include "draw/draw_context.h" -#include "tgsi/tgsi_dump.h" -#include "tgsi/tgsi_ureg.h" - -#include "st_debug.h" -#include "st_cb_bitmap.h" -#include "st_cb_drawpixels.h" -#include "st_context.h" -#include "st_program.h" -#include "st_mesa_to_tgsi.h" -#include "cso_cache/cso_context.h" - - - -/** - * Delete a vertex program variant. Note the caller must unlink - * the variant from the linked list. - */ -static void -delete_vp_variant(struct st_context *st, struct st_vp_variant *vpv) -{ - if (vpv->driver_shader) - cso_delete_vertex_shader(st->cso_context, vpv->driver_shader); - -#if FEATURE_feedback || FEATURE_rastpos - if (vpv->draw_shader) - draw_delete_vertex_shader( st->draw, vpv->draw_shader ); -#endif - - if (vpv->tgsi.tokens) - st_free_tokens(vpv->tgsi.tokens); - - FREE( vpv ); -} - - - -/** - * Clean out any old compilations: - */ -void -st_release_vp_variants( struct st_context *st, - struct st_vertex_program *stvp ) -{ - struct st_vp_variant *vpv; - - for (vpv = stvp->variants; vpv; ) { - struct st_vp_variant *next = vpv->next; - delete_vp_variant(st, vpv); - vpv = next; - } - - stvp->variants = NULL; -} - - - -/** - * Delete a fragment program variant. Note the caller must unlink - * the variant from the linked list. - */ -static void -delete_fp_variant(struct st_context *st, struct st_fp_variant *fpv) -{ - if (fpv->driver_shader) - cso_delete_fragment_shader(st->cso_context, fpv->driver_shader); - if (fpv->parameters) - _mesa_free_parameter_list(fpv->parameters); - - FREE(fpv); -} - - -/** - * Free all variants of a fragment program. - */ -void -st_release_fp_variants(struct st_context *st, struct st_fragment_program *stfp) -{ - struct st_fp_variant *fpv; - - for (fpv = stfp->variants; fpv; ) { - struct st_fp_variant *next = fpv->next; - delete_fp_variant(st, fpv); - fpv = next; - } - - stfp->variants = NULL; -} - - -/** - * Delete a geometry program variant. Note the caller must unlink - * the variant from the linked list. - */ -static void -delete_gp_variant(struct st_context *st, struct st_gp_variant *gpv) -{ - if (gpv->driver_shader) - cso_delete_geometry_shader(st->cso_context, gpv->driver_shader); - - FREE(gpv); -} - - -/** - * Free all variants of a geometry program. - */ -void -st_release_gp_variants(struct st_context *st, struct st_geometry_program *stgp) -{ - struct st_gp_variant *gpv; - - for (gpv = stgp->variants; gpv; ) { - struct st_gp_variant *next = gpv->next; - delete_gp_variant(st, gpv); - gpv = next; - } - - stgp->variants = NULL; -} - - - - -/** - * Translate a Mesa vertex shader into a TGSI shader. - * \param outputMapping to map vertex program output registers (VERT_RESULT_x) - * to TGSI output slots - * \param tokensOut destination for TGSI tokens - * \return pointer to cached pipe_shader object. - */ -static void -st_prepare_vertex_program(struct st_context *st, - struct st_vertex_program *stvp) -{ - GLuint attr; - - stvp->num_inputs = 0; - stvp->num_outputs = 0; - - if (stvp->Base.IsPositionInvariant) - _mesa_insert_mvp_code(st->ctx, &stvp->Base); - - assert(stvp->Base.Base.NumInstructions > 1); - - /* - * Determine number of inputs, the mappings between VERT_ATTRIB_x - * and TGSI generic input indexes, plus input attrib semantic info. - */ - for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) { - if (stvp->Base.Base.InputsRead & (1 << attr)) { - stvp->input_to_index[attr] = stvp->num_inputs; - stvp->index_to_input[stvp->num_inputs] = attr; - stvp->num_inputs++; - } - } - /* bit of a hack, presetup potentially unused edgeflag input */ - stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs; - stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG; - - /* Compute mapping of vertex program outputs to slots. - */ - for (attr = 0; attr < VERT_RESULT_MAX; attr++) { - if ((stvp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) == 0) { - stvp->result_to_output[attr] = ~0; - } - else { - unsigned slot = stvp->num_outputs++; - - stvp->result_to_output[attr] = slot; - - switch (attr) { - case VERT_RESULT_HPOS: - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION; - stvp->output_semantic_index[slot] = 0; - break; - case VERT_RESULT_COL0: - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR; - stvp->output_semantic_index[slot] = 0; - break; - case VERT_RESULT_COL1: - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR; - stvp->output_semantic_index[slot] = 1; - break; - case VERT_RESULT_BFC0: - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR; - stvp->output_semantic_index[slot] = 0; - break; - case VERT_RESULT_BFC1: - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR; - stvp->output_semantic_index[slot] = 1; - break; - case VERT_RESULT_FOGC: - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG; - stvp->output_semantic_index[slot] = 0; - break; - case VERT_RESULT_PSIZ: - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE; - stvp->output_semantic_index[slot] = 0; - break; - case VERT_RESULT_EDGE: - assert(0); - break; - - case VERT_RESULT_TEX0: - case VERT_RESULT_TEX1: - case VERT_RESULT_TEX2: - case VERT_RESULT_TEX3: - case VERT_RESULT_TEX4: - case VERT_RESULT_TEX5: - case VERT_RESULT_TEX6: - case VERT_RESULT_TEX7: - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; - stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0; - break; - - case VERT_RESULT_VAR0: - default: - assert(attr < VERT_RESULT_MAX); - stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; - stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 - - FRAG_ATTRIB_TEX0 + - attr - - VERT_RESULT_VAR0); - break; - } - } - } - /* similar hack to above, presetup potentially unused edgeflag output */ - stvp->result_to_output[VERT_RESULT_EDGE] = stvp->num_outputs; - stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG; - stvp->output_semantic_index[stvp->num_outputs] = 0; -} - - -/** - * Translate a vertex program to create a new variant. - */ -static struct st_vp_variant * -st_translate_vertex_program(struct st_context *st, - struct st_vertex_program *stvp, - const struct st_vp_variant_key *key) -{ - struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant); - struct pipe_context *pipe = st->pipe; - struct ureg_program *ureg; - enum pipe_error error; - unsigned num_outputs; - - st_prepare_vertex_program( st, stvp ); - - _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_OUTPUT); - _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_VARYING); - - ureg = ureg_create( TGSI_PROCESSOR_VERTEX ); - if (ureg == NULL) { - FREE(vpv); - return NULL; - } - - vpv->key = *key; - - vpv->num_inputs = stvp->num_inputs; - num_outputs = stvp->num_outputs; - if (key->passthrough_edgeflags) { - vpv->num_inputs++; - num_outputs++; - } - - if (ST_DEBUG & DEBUG_MESA) { - _mesa_print_program(&stvp->Base.Base); - _mesa_print_program_parameters(st->ctx, &stvp->Base.Base); - debug_printf("\n"); - } - - error = st_translate_mesa_program(st->ctx, - TGSI_PROCESSOR_VERTEX, - ureg, - &stvp->Base.Base, - /* inputs */ - vpv->num_inputs, - stvp->input_to_index, - NULL, /* input semantic name */ - NULL, /* input semantic index */ - NULL, - /* outputs */ - num_outputs, - stvp->result_to_output, - stvp->output_semantic_name, - stvp->output_semantic_index, - key->passthrough_edgeflags ); - - if (error) - goto fail; - - vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL ); - if (!vpv->tgsi.tokens) - goto fail; - - ureg_destroy( ureg ); - - vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi); - - if (ST_DEBUG & DEBUG_TGSI) { - tgsi_dump( vpv->tgsi.tokens, 0 ); - debug_printf("\n"); - } - - return vpv; - -fail: - debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__); - _mesa_print_program(&stvp->Base.Base); - debug_assert(0); - - ureg_destroy( ureg ); - return NULL; -} - - -/** - * Find/create a vertex program variant. - */ -struct st_vp_variant * -st_get_vp_variant(struct st_context *st, - struct st_vertex_program *stvp, - const struct st_vp_variant_key *key) -{ - struct st_vp_variant *vpv; - - /* Search for existing variant */ - for (vpv = stvp->variants; vpv; vpv = vpv->next) { - if (memcmp(&vpv->key, key, sizeof(*key)) == 0) { - break; - } - } - - if (!vpv) { - /* create now */ - vpv = st_translate_vertex_program(st, stvp, key); - if (vpv) { - /* insert into list */ - vpv->next = stvp->variants; - stvp->variants = vpv; - } - } - - return vpv; -} - - -/** - * Translate a Mesa fragment shader into a TGSI shader using extra info in - * the key. - * \return new fragment program variant - */ -static struct st_fp_variant * -st_translate_fragment_program(struct st_context *st, - struct st_fragment_program *stfp, - const struct st_fp_variant_key *key) -{ - struct pipe_context *pipe = st->pipe; - struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant); - GLboolean deleteFP = GL_FALSE; - - if (!variant) - return NULL; - - assert(!(key->bitmap && key->drawpixels)); - -#if FEATURE_drawpix - if (key->bitmap) { - /* glBitmap drawing */ - struct gl_fragment_program *fp; /* we free this temp program below */ - - st_make_bitmap_fragment_program(st, &stfp->Base, - &fp, &variant->bitmap_sampler); - - variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters); - stfp = st_fragment_program(fp); - deleteFP = GL_TRUE; - } - else if (key->drawpixels) { - /* glDrawPixels drawing */ - struct gl_fragment_program *fp; /* we free this temp program below */ - - if (key->drawpixels_z || key->drawpixels_stencil) { - fp = st_make_drawpix_z_stencil_program(st, key->drawpixels_z, - key->drawpixels_stencil); - } - else { - /* RGBA */ - st_make_drawpix_fragment_program(st, &stfp->Base, &fp); - variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters); - deleteFP = GL_TRUE; - } - stfp = st_fragment_program(fp); - } -#endif - - if (!stfp->tgsi.tokens) { - /* need to translate Mesa instructions to TGSI now */ - GLuint outputMapping[FRAG_RESULT_MAX]; - GLuint inputMapping[FRAG_ATTRIB_MAX]; - GLuint interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */ - GLuint attr; - enum pipe_error error; - const GLbitfield inputsRead = stfp->Base.Base.InputsRead; - struct ureg_program *ureg; - GLboolean write_all = GL_FALSE; - - ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS]; - ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS]; - uint fs_num_inputs = 0; - - ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS]; - ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS]; - uint fs_num_outputs = 0; - - - _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT); - - /* - * Convert Mesa program inputs to TGSI input register semantics. - */ - for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) { - if (inputsRead & (1 << attr)) { - const GLuint slot = fs_num_inputs++; - - inputMapping[attr] = slot; - - switch (attr) { - case FRAG_ATTRIB_WPOS: - input_semantic_name[slot] = TGSI_SEMANTIC_POSITION; - input_semantic_index[slot] = 0; - interpMode[slot] = TGSI_INTERPOLATE_LINEAR; - break; - case FRAG_ATTRIB_COL0: - input_semantic_name[slot] = TGSI_SEMANTIC_COLOR; - input_semantic_index[slot] = 0; - interpMode[slot] = TGSI_INTERPOLATE_LINEAR; - break; - case FRAG_ATTRIB_COL1: - input_semantic_name[slot] = TGSI_SEMANTIC_COLOR; - input_semantic_index[slot] = 1; - interpMode[slot] = TGSI_INTERPOLATE_LINEAR; - break; - case FRAG_ATTRIB_FOGC: - input_semantic_name[slot] = TGSI_SEMANTIC_FOG; - input_semantic_index[slot] = 0; - interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE; - break; - case FRAG_ATTRIB_FACE: - input_semantic_name[slot] = TGSI_SEMANTIC_FACE; - input_semantic_index[slot] = 0; - interpMode[slot] = TGSI_INTERPOLATE_CONSTANT; - break; - /* In most cases, there is nothing special about these - * inputs, so adopt a convention to use the generic - * semantic name and the mesa FRAG_ATTRIB_ number as the - * index. - * - * All that is required is that the vertex shader labels - * its own outputs similarly, and that the vertex shader - * generates at least every output required by the - * fragment shader plus fixed-function hardware (such as - * BFC). - * - * There is no requirement that semantic indexes start at - * zero or be restricted to a particular range -- nobody - * should be building tables based on semantic index. - */ - case FRAG_ATTRIB_PNTC: - case FRAG_ATTRIB_TEX0: - case FRAG_ATTRIB_TEX1: - case FRAG_ATTRIB_TEX2: - case FRAG_ATTRIB_TEX3: - case FRAG_ATTRIB_TEX4: - case FRAG_ATTRIB_TEX5: - case FRAG_ATTRIB_TEX6: - case FRAG_ATTRIB_TEX7: - case FRAG_ATTRIB_VAR0: - default: - /* Actually, let's try and zero-base this just for - * readability of the generated TGSI. - */ - assert(attr >= FRAG_ATTRIB_TEX0); - input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0); - input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; - if (attr == FRAG_ATTRIB_PNTC) - interpMode[slot] = TGSI_INTERPOLATE_LINEAR; - else - interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE; - break; - } - } - else { - inputMapping[attr] = -1; - } - } - - /* - * Semantics and mapping for outputs - */ - { - uint numColors = 0; - GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten; - - /* if z is written, emit that first */ - if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) { - fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION; - fs_output_semantic_index[fs_num_outputs] = 0; - outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs; - fs_num_outputs++; - outputsWritten &= ~(1 << FRAG_RESULT_DEPTH); - } - - if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) { - fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL; - fs_output_semantic_index[fs_num_outputs] = 0; - outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs; - fs_num_outputs++; - outputsWritten &= ~(1 << FRAG_RESULT_STENCIL); - } - - /* handle remaning outputs (color) */ - for (attr = 0; attr < FRAG_RESULT_MAX; attr++) { - if (outputsWritten & BITFIELD64_BIT(attr)) { - switch (attr) { - case FRAG_RESULT_DEPTH: - case FRAG_RESULT_STENCIL: - /* handled above */ - assert(0); - break; - case FRAG_RESULT_COLOR: - write_all = GL_TRUE; /* fallthrough */ - default: - assert(attr == FRAG_RESULT_COLOR || - (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX)); - fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR; - fs_output_semantic_index[fs_num_outputs] = numColors; - outputMapping[attr] = fs_num_outputs; - numColors++; - break; - } - - fs_num_outputs++; - } - } - } - - ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT ); - if (ureg == NULL) - return NULL; - - if (ST_DEBUG & DEBUG_MESA) { - _mesa_print_program(&stfp->Base.Base); - _mesa_print_program_parameters(st->ctx, &stfp->Base.Base); - debug_printf("\n"); - } - if (write_all == GL_TRUE) - ureg_property_fs_color0_writes_all_cbufs(ureg, 1); - - error = st_translate_mesa_program(st->ctx, - TGSI_PROCESSOR_FRAGMENT, - ureg, - &stfp->Base.Base, - /* inputs */ - fs_num_inputs, - inputMapping, - input_semantic_name, - input_semantic_index, - interpMode, - /* outputs */ - fs_num_outputs, - outputMapping, - fs_output_semantic_name, - fs_output_semantic_index, FALSE ); - - stfp->tgsi.tokens = ureg_get_tokens( ureg, NULL ); - ureg_destroy( ureg ); - } - - /* fill in variant */ - variant->driver_shader = pipe->create_fs_state(pipe, &stfp->tgsi); - variant->key = *key; - - if (ST_DEBUG & DEBUG_TGSI) { - tgsi_dump( stfp->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/ ); - debug_printf("\n"); - } - - if (deleteFP) { - /* Free the temporary program made above */ - struct gl_fragment_program *fp = &stfp->Base; - _mesa_reference_fragprog(st->ctx, &fp, NULL); - } - - return variant; -} - - -/** - * Translate fragment program if needed. - */ -struct st_fp_variant * -st_get_fp_variant(struct st_context *st, - struct st_fragment_program *stfp, - const struct st_fp_variant_key *key) -{ - struct st_fp_variant *fpv; - - /* Search for existing variant */ - for (fpv = stfp->variants; fpv; fpv = fpv->next) { - if (memcmp(&fpv->key, key, sizeof(*key)) == 0) { - break; - } - } - - if (!fpv) { - /* create new */ - fpv = st_translate_fragment_program(st, stfp, key); - if (fpv) { - /* insert into list */ - fpv->next = stfp->variants; - stfp->variants = fpv; - } - } - - return fpv; -} - - -/** - * Translate a geometry program to create a new variant. - */ -static struct st_gp_variant * -st_translate_geometry_program(struct st_context *st, - struct st_geometry_program *stgp, - const struct st_gp_variant_key *key) -{ - GLuint inputMapping[GEOM_ATTRIB_MAX]; - GLuint outputMapping[GEOM_RESULT_MAX]; - struct pipe_context *pipe = st->pipe; - enum pipe_error error; - GLuint attr; - const GLbitfield inputsRead = stgp->Base.Base.InputsRead; - GLuint vslot = 0; - GLuint num_generic = 0; - - uint gs_num_inputs = 0; - uint gs_builtin_inputs = 0; - uint gs_array_offset = 0; - - ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS]; - ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS]; - uint gs_num_outputs = 0; - - GLint i; - GLuint maxSlot = 0; - struct ureg_program *ureg; - - struct st_gp_variant *gpv; - - gpv = CALLOC_STRUCT(st_gp_variant); - if (!gpv) - return NULL; - - _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_OUTPUT); - _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_VARYING); - - ureg = ureg_create( TGSI_PROCESSOR_GEOMETRY ); - if (ureg == NULL) { - FREE(gpv); - return NULL; - } - - /* which vertex output goes to the first geometry input */ - vslot = 0; - - memset(inputMapping, 0, sizeof(inputMapping)); - memset(outputMapping, 0, sizeof(outputMapping)); - - /* - * Convert Mesa program inputs to TGSI input register semantics. - */ - for (attr = 0; attr < GEOM_ATTRIB_MAX; attr++) { - if (inputsRead & (1 << attr)) { - const GLuint slot = gs_num_inputs; - - gs_num_inputs++; - - inputMapping[attr] = slot; - - stgp->input_map[slot + gs_array_offset] = vslot - gs_builtin_inputs; - stgp->input_to_index[attr] = vslot; - stgp->index_to_input[vslot] = attr; - ++vslot; - - if (attr != GEOM_ATTRIB_PRIMITIVE_ID) { - gs_array_offset += 2; - } else - ++gs_builtin_inputs; - -#if 0 - debug_printf("input map at %d = %d\n", - slot + gs_array_offset, stgp->input_map[slot + gs_array_offset]); -#endif - - switch (attr) { - case GEOM_ATTRIB_PRIMITIVE_ID: - stgp->input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID; - stgp->input_semantic_index[slot] = 0; - break; - case GEOM_ATTRIB_POSITION: - stgp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION; - stgp->input_semantic_index[slot] = 0; - break; - case GEOM_ATTRIB_COLOR0: - stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR; - stgp->input_semantic_index[slot] = 0; - break; - case GEOM_ATTRIB_COLOR1: - stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR; - stgp->input_semantic_index[slot] = 1; - break; - case GEOM_ATTRIB_FOG_FRAG_COORD: - stgp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG; - stgp->input_semantic_index[slot] = 0; - break; - case GEOM_ATTRIB_TEX_COORD: - stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; - stgp->input_semantic_index[slot] = num_generic++; - break; - case GEOM_ATTRIB_VAR0: - /* fall-through */ - default: - stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; - stgp->input_semantic_index[slot] = num_generic++; - } - } - } - - /* initialize output semantics to defaults */ - for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) { - gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC; - gs_output_semantic_index[i] = 0; - } - - num_generic = 0; - /* - * Determine number of outputs, the (default) output register - * mapping and the semantic information for each output. - */ - for (attr = 0; attr < GEOM_RESULT_MAX; attr++) { - if (stgp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) { - GLuint slot; - - slot = gs_num_outputs; - gs_num_outputs++; - outputMapping[attr] = slot; - - switch (attr) { - case GEOM_RESULT_POS: - assert(slot == 0); - gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION; - gs_output_semantic_index[slot] = 0; - break; - case GEOM_RESULT_COL0: - gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR; - gs_output_semantic_index[slot] = 0; - break; - case GEOM_RESULT_COL1: - gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR; - gs_output_semantic_index[slot] = 1; - break; - case GEOM_RESULT_SCOL0: - gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR; - gs_output_semantic_index[slot] = 0; - break; - case GEOM_RESULT_SCOL1: - gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR; - gs_output_semantic_index[slot] = 1; - break; - case GEOM_RESULT_FOGC: - gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG; - gs_output_semantic_index[slot] = 0; - break; - case GEOM_RESULT_PSIZ: - gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE; - gs_output_semantic_index[slot] = 0; - break; - case GEOM_RESULT_TEX0: - case GEOM_RESULT_TEX1: - case GEOM_RESULT_TEX2: - case GEOM_RESULT_TEX3: - case GEOM_RESULT_TEX4: - case GEOM_RESULT_TEX5: - case GEOM_RESULT_TEX6: - case GEOM_RESULT_TEX7: - /* fall-through */ - case GEOM_RESULT_VAR0: - /* fall-through */ - default: - assert(slot < Elements(gs_output_semantic_name)); - /* use default semantic info */ - gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC; - gs_output_semantic_index[slot] = num_generic++; - } - } - } - - assert(gs_output_semantic_name[0] == TGSI_SEMANTIC_POSITION); - - /* find max output slot referenced to compute gs_num_outputs */ - for (attr = 0; attr < GEOM_RESULT_MAX; attr++) { - if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot) - maxSlot = outputMapping[attr]; - } - gs_num_outputs = maxSlot + 1; - -#if 0 /* debug */ - { - GLuint i; - printf("outputMapping? %d\n", outputMapping ? 1 : 0); - if (outputMapping) { - printf("attr -> slot\n"); - for (i = 0; i < 16; i++) { - printf(" %2d %3d\n", i, outputMapping[i]); - } - } - printf("slot sem_name sem_index\n"); - for (i = 0; i < gs_num_outputs; i++) { - printf(" %2d %d %d\n", - i, - gs_output_semantic_name[i], - gs_output_semantic_index[i]); - } - } -#endif - - /* free old shader state, if any */ - if (stgp->tgsi.tokens) { - st_free_tokens(stgp->tgsi.tokens); - stgp->tgsi.tokens = NULL; - } - - ureg_property_gs_input_prim(ureg, stgp->Base.InputType); - ureg_property_gs_output_prim(ureg, stgp->Base.OutputType); - ureg_property_gs_max_vertices(ureg, stgp->Base.VerticesOut); - - error = st_translate_mesa_program(st->ctx, - TGSI_PROCESSOR_GEOMETRY, - ureg, - &stgp->Base.Base, - /* inputs */ - gs_num_inputs, - inputMapping, - stgp->input_semantic_name, - stgp->input_semantic_index, - NULL, - /* outputs */ - gs_num_outputs, - outputMapping, - gs_output_semantic_name, - gs_output_semantic_index, - FALSE); - - stgp->num_inputs = gs_num_inputs; - stgp->tgsi.tokens = ureg_get_tokens( ureg, NULL ); - ureg_destroy( ureg ); - - /* fill in new variant */ - gpv->driver_shader = pipe->create_gs_state(pipe, &stgp->tgsi); - gpv->key = *key; - - if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) { - _mesa_print_program(&stgp->Base.Base); - debug_printf("\n"); - } - - if (ST_DEBUG & DEBUG_TGSI) { - tgsi_dump(stgp->tgsi.tokens, 0); - debug_printf("\n"); - } - - return gpv; -} - - -/** - * Get/create geometry program variant. - */ -struct st_gp_variant * -st_get_gp_variant(struct st_context *st, - struct st_geometry_program *stgp, - const struct st_gp_variant_key *key) -{ - struct st_gp_variant *gpv; - - /* Search for existing variant */ - for (gpv = stgp->variants; gpv; gpv = gpv->next) { - if (memcmp(&gpv->key, key, sizeof(*key)) == 0) { - break; - } - } - - if (!gpv) { - /* create new */ - gpv = st_translate_geometry_program(st, stgp, key); - if (gpv) { - /* insert into list */ - gpv->next = stgp->variants; - stgp->variants = gpv; - } - } - - return gpv; -} - - - - -/** - * Debug- print current shader text - */ -void -st_print_shaders(struct gl_context *ctx) -{ - struct gl_shader_program *shProg[3] = { - ctx->Shader.CurrentVertexProgram, - ctx->Shader.CurrentGeometryProgram, - ctx->Shader.CurrentFragmentProgram, - }; - unsigned j; - - for (j = 0; j < 3; j++) { - unsigned i; - - if (shProg[j] == NULL) - continue; - - for (i = 0; i < shProg[j]->NumShaders; i++) { - struct gl_shader *sh; - - switch (shProg[j]->Shaders[i]->Type) { - case GL_VERTEX_SHADER: - sh = (i != 0) ? NULL : shProg[j]->Shaders[i]; - break; - case GL_GEOMETRY_SHADER_ARB: - sh = (i != 1) ? NULL : shProg[j]->Shaders[i]; - break; - case GL_FRAGMENT_SHADER: - sh = (i != 2) ? NULL : shProg[j]->Shaders[i]; - break; - default: - assert(0); - sh = NULL; - break; - } - - if (sh != NULL) { - printf("GLSL shader %u of %u:\n", i, shProg[j]->NumShaders); - printf("%s\n", sh->Source); - } - } - } -} - - -/** - * Vert/Geom/Frag programs have per-context variants. Free all the - * variants attached to the given program which match the given context. - */ -static void -destroy_program_variants(struct st_context *st, struct gl_program *program) -{ - if (!program) - return; - - switch (program->Target) { - case GL_VERTEX_PROGRAM_ARB: - { - struct st_vertex_program *stvp = (struct st_vertex_program *) program; - struct st_vp_variant *vpv, **prevPtr = &stvp->variants; - - for (vpv = stvp->variants; vpv; ) { - struct st_vp_variant *next = vpv->next; - if (vpv->key.st == st) { - /* unlink from list */ - *prevPtr = next; - /* destroy this variant */ - delete_vp_variant(st, vpv); - } - else { - prevPtr = &vpv->next; - } - vpv = next; - } - } - break; - case GL_FRAGMENT_PROGRAM_ARB: - { - struct st_fragment_program *stfp = - (struct st_fragment_program *) program; - struct st_fp_variant *fpv, **prevPtr = &stfp->variants; - - for (fpv = stfp->variants; fpv; ) { - struct st_fp_variant *next = fpv->next; - if (fpv->key.st == st) { - /* unlink from list */ - *prevPtr = next; - /* destroy this variant */ - delete_fp_variant(st, fpv); - } - else { - prevPtr = &fpv->next; - } - fpv = next; - } - } - break; - case MESA_GEOMETRY_PROGRAM: - { - struct st_geometry_program *stgp = - (struct st_geometry_program *) program; - struct st_gp_variant *gpv, **prevPtr = &stgp->variants; - - for (gpv = stgp->variants; gpv; ) { - struct st_gp_variant *next = gpv->next; - if (gpv->key.st == st) { - /* unlink from list */ - *prevPtr = next; - /* destroy this variant */ - delete_gp_variant(st, gpv); - } - else { - prevPtr = &gpv->next; - } - gpv = next; - } - } - break; - default: - _mesa_problem(NULL, "Unexpected program target in " - "destroy_program_variants_cb()"); - } -} - - -/** - * Callback for _mesa_HashWalk. Free all the shader's program variants - * which match the given context. - */ -static void -destroy_shader_program_variants_cb(GLuint key, void *data, void *userData) -{ - struct st_context *st = (struct st_context *) userData; - struct gl_shader *shader = (struct gl_shader *) data; - - switch (shader->Type) { - case GL_SHADER_PROGRAM_MESA: - { - struct gl_shader_program *shProg = (struct gl_shader_program *) data; - GLuint i; - - for (i = 0; i < shProg->NumShaders; i++) { - destroy_program_variants(st, shProg->Shaders[i]->Program); - } - - destroy_program_variants(st, (struct gl_program *) - shProg->VertexProgram); - destroy_program_variants(st, (struct gl_program *) - shProg->FragmentProgram); - destroy_program_variants(st, (struct gl_program *) - shProg->GeometryProgram); - } - break; - case GL_VERTEX_SHADER: - case GL_FRAGMENT_SHADER: - case GL_GEOMETRY_SHADER: - { - destroy_program_variants(st, shader->Program); - } - break; - default: - assert(0); - } -} - - -/** - * Callback for _mesa_HashWalk. Free all the program variants which match - * the given context. - */ -static void -destroy_program_variants_cb(GLuint key, void *data, void *userData) -{ - struct st_context *st = (struct st_context *) userData; - struct gl_program *program = (struct gl_program *) data; - destroy_program_variants(st, program); -} - - -/** - * Walk over all shaders and programs to delete any variants which - * belong to the given context. - * This is called during context tear-down. - */ -void -st_destroy_program_variants(struct st_context *st) -{ - /* ARB vert/frag program */ - _mesa_HashWalk(st->ctx->Shared->Programs, - destroy_program_variants_cb, st); - - /* GLSL vert/frag/geom shaders */ - _mesa_HashWalk(st->ctx->Shared->ShaderObjects, - destroy_shader_program_variants_cb, st); -} +/**************************************************************************
+ *
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+ /*
+ * Authors:
+ * Keith Whitwell <keith@tungstengraphics.com>
+ * Brian Paul
+ */
+
+
+#include "main/imports.h"
+#include "main/hash.h"
+#include "main/mfeatures.h"
+#include "main/mtypes.h"
+#include "program/prog_parameter.h"
+#include "program/prog_print.h"
+#include "program/programopt.h"
+
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "pipe/p_shader_tokens.h"
+#include "draw/draw_context.h"
+#include "tgsi/tgsi_dump.h"
+#include "tgsi/tgsi_ureg.h"
+
+#include "st_debug.h"
+#include "st_cb_bitmap.h"
+#include "st_cb_drawpixels.h"
+#include "st_context.h"
+#include "st_program.h"
+#include "st_mesa_to_tgsi.h"
+#include "cso_cache/cso_context.h"
+
+
+
+/**
+ * Delete a vertex program variant. Note the caller must unlink
+ * the variant from the linked list.
+ */
+static void
+delete_vp_variant(struct st_context *st, struct st_vp_variant *vpv)
+{
+ if (vpv->driver_shader)
+ cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
+
+#if FEATURE_feedback || FEATURE_rastpos
+ if (vpv->draw_shader)
+ draw_delete_vertex_shader( st->draw, vpv->draw_shader );
+#endif
+
+ if (vpv->tgsi.tokens)
+ st_free_tokens(vpv->tgsi.tokens);
+
+ FREE( vpv );
+}
+
+
+
+/**
+ * Clean out any old compilations:
+ */
+void
+st_release_vp_variants( struct st_context *st,
+ struct st_vertex_program *stvp )
+{
+ struct st_vp_variant *vpv;
+
+ for (vpv = stvp->variants; vpv; ) {
+ struct st_vp_variant *next = vpv->next;
+ delete_vp_variant(st, vpv);
+ vpv = next;
+ }
+
+ stvp->variants = NULL;
+}
+
+
+
+/**
+ * Delete a fragment program variant. Note the caller must unlink
+ * the variant from the linked list.
+ */
+static void
+delete_fp_variant(struct st_context *st, struct st_fp_variant *fpv)
+{
+ if (fpv->driver_shader)
+ cso_delete_fragment_shader(st->cso_context, fpv->driver_shader);
+ if (fpv->parameters)
+ _mesa_free_parameter_list(fpv->parameters);
+
+ FREE(fpv);
+}
+
+
+/**
+ * Free all variants of a fragment program.
+ */
+void
+st_release_fp_variants(struct st_context *st, struct st_fragment_program *stfp)
+{
+ struct st_fp_variant *fpv;
+
+ for (fpv = stfp->variants; fpv; ) {
+ struct st_fp_variant *next = fpv->next;
+ delete_fp_variant(st, fpv);
+ fpv = next;
+ }
+
+ stfp->variants = NULL;
+}
+
+
+/**
+ * Delete a geometry program variant. Note the caller must unlink
+ * the variant from the linked list.
+ */
+static void
+delete_gp_variant(struct st_context *st, struct st_gp_variant *gpv)
+{
+ if (gpv->driver_shader)
+ cso_delete_geometry_shader(st->cso_context, gpv->driver_shader);
+
+ FREE(gpv);
+}
+
+
+/**
+ * Free all variants of a geometry program.
+ */
+void
+st_release_gp_variants(struct st_context *st, struct st_geometry_program *stgp)
+{
+ struct st_gp_variant *gpv;
+
+ for (gpv = stgp->variants; gpv; ) {
+ struct st_gp_variant *next = gpv->next;
+ delete_gp_variant(st, gpv);
+ gpv = next;
+ }
+
+ stgp->variants = NULL;
+}
+
+
+
+
+/**
+ * Translate a Mesa vertex shader into a TGSI shader.
+ * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
+ * to TGSI output slots
+ * \param tokensOut destination for TGSI tokens
+ * \return pointer to cached pipe_shader object.
+ */
+static void
+st_prepare_vertex_program(struct st_context *st,
+ struct st_vertex_program *stvp)
+{
+ GLuint attr;
+
+ stvp->num_inputs = 0;
+ stvp->num_outputs = 0;
+
+ if (stvp->Base.IsPositionInvariant)
+ _mesa_insert_mvp_code(st->ctx, &stvp->Base);
+
+ assert(stvp->Base.Base.NumInstructions > 1);
+
+ /*
+ * Determine number of inputs, the mappings between VERT_ATTRIB_x
+ * and TGSI generic input indexes, plus input attrib semantic info.
+ */
+ for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
+ if (stvp->Base.Base.InputsRead & (1 << attr)) {
+ stvp->input_to_index[attr] = stvp->num_inputs;
+ stvp->index_to_input[stvp->num_inputs] = attr;
+ stvp->num_inputs++;
+ }
+ }
+ /* bit of a hack, presetup potentially unused edgeflag input */
+ stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
+ stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
+
+ /* Compute mapping of vertex program outputs to slots.
+ */
+ for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
+ if ((stvp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) == 0) {
+ stvp->result_to_output[attr] = ~0;
+ }
+ else {
+ unsigned slot = stvp->num_outputs++;
+
+ stvp->result_to_output[attr] = slot;
+
+ switch (attr) {
+ case VERT_RESULT_HPOS:
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
+ stvp->output_semantic_index[slot] = 0;
+ break;
+ case VERT_RESULT_COL0:
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
+ stvp->output_semantic_index[slot] = 0;
+ break;
+ case VERT_RESULT_COL1:
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
+ stvp->output_semantic_index[slot] = 1;
+ break;
+ case VERT_RESULT_BFC0:
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
+ stvp->output_semantic_index[slot] = 0;
+ break;
+ case VERT_RESULT_BFC1:
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
+ stvp->output_semantic_index[slot] = 1;
+ break;
+ case VERT_RESULT_FOGC:
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
+ stvp->output_semantic_index[slot] = 0;
+ break;
+ case VERT_RESULT_PSIZ:
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
+ stvp->output_semantic_index[slot] = 0;
+ break;
+ case VERT_RESULT_EDGE:
+ assert(0);
+ break;
+
+ case VERT_RESULT_TEX0:
+ case VERT_RESULT_TEX1:
+ case VERT_RESULT_TEX2:
+ case VERT_RESULT_TEX3:
+ case VERT_RESULT_TEX4:
+ case VERT_RESULT_TEX5:
+ case VERT_RESULT_TEX6:
+ case VERT_RESULT_TEX7:
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
+ stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0;
+ break;
+
+ case VERT_RESULT_VAR0:
+ default:
+ assert(attr < VERT_RESULT_MAX);
+ stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
+ stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 -
+ FRAG_ATTRIB_TEX0 +
+ attr -
+ VERT_RESULT_VAR0);
+ break;
+ }
+ }
+ }
+ /* similar hack to above, presetup potentially unused edgeflag output */
+ stvp->result_to_output[VERT_RESULT_EDGE] = stvp->num_outputs;
+ stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
+ stvp->output_semantic_index[stvp->num_outputs] = 0;
+}
+
+
+/**
+ * Translate a vertex program to create a new variant.
+ */
+static struct st_vp_variant *
+st_translate_vertex_program(struct st_context *st,
+ struct st_vertex_program *stvp,
+ const struct st_vp_variant_key *key)
+{
+ struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
+ struct pipe_context *pipe = st->pipe;
+ struct ureg_program *ureg;
+ enum pipe_error error;
+ unsigned num_outputs;
+
+ st_prepare_vertex_program( st, stvp );
+
+ _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_OUTPUT);
+ _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_VARYING);
+
+ ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
+ if (ureg == NULL) {
+ FREE(vpv);
+ return NULL;
+ }
+
+ vpv->key = *key;
+
+ vpv->num_inputs = stvp->num_inputs;
+ num_outputs = stvp->num_outputs;
+ if (key->passthrough_edgeflags) {
+ vpv->num_inputs++;
+ num_outputs++;
+ }
+
+ if (ST_DEBUG & DEBUG_MESA) {
+ _mesa_print_program(&stvp->Base.Base);
+ _mesa_print_program_parameters(st->ctx, &stvp->Base.Base);
+ debug_printf("\n");
+ }
+
+ error = st_translate_mesa_program(st->ctx,
+ TGSI_PROCESSOR_VERTEX,
+ ureg,
+ &stvp->Base.Base,
+ /* inputs */
+ vpv->num_inputs,
+ stvp->input_to_index,
+ NULL, /* input semantic name */
+ NULL, /* input semantic index */
+ NULL,
+ /* outputs */
+ num_outputs,
+ stvp->result_to_output,
+ stvp->output_semantic_name,
+ stvp->output_semantic_index,
+ key->passthrough_edgeflags );
+
+ if (error)
+ goto fail;
+
+ vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL );
+ if (!vpv->tgsi.tokens)
+ goto fail;
+
+ ureg_destroy( ureg );
+
+ vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
+
+ if (ST_DEBUG & DEBUG_TGSI) {
+ tgsi_dump( vpv->tgsi.tokens, 0 );
+ debug_printf("\n");
+ }
+
+ return vpv;
+
+fail:
+ debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__);
+ _mesa_print_program(&stvp->Base.Base);
+ debug_assert(0);
+
+ ureg_destroy( ureg );
+ return NULL;
+}
+
+
+/**
+ * Find/create a vertex program variant.
+ */
+struct st_vp_variant *
+st_get_vp_variant(struct st_context *st,
+ struct st_vertex_program *stvp,
+ const struct st_vp_variant_key *key)
+{
+ struct st_vp_variant *vpv;
+
+ /* Search for existing variant */
+ for (vpv = stvp->variants; vpv; vpv = vpv->next) {
+ if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
+ break;
+ }
+ }
+
+ if (!vpv) {
+ /* create now */
+ vpv = st_translate_vertex_program(st, stvp, key);
+ if (vpv) {
+ /* insert into list */
+ vpv->next = stvp->variants;
+ stvp->variants = vpv;
+ }
+ }
+
+ return vpv;
+}
+
+
+/**
+ * Translate a Mesa fragment shader into a TGSI shader using extra info in
+ * the key.
+ * \return new fragment program variant
+ */
+static struct st_fp_variant *
+st_translate_fragment_program(struct st_context *st,
+ struct st_fragment_program *stfp,
+ const struct st_fp_variant_key *key)
+{
+ struct pipe_context *pipe = st->pipe;
+ struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
+ GLboolean deleteFP = GL_FALSE;
+
+ if (!variant)
+ return NULL;
+
+ assert(!(key->bitmap && key->drawpixels));
+
+#if FEATURE_drawpix
+ if (key->bitmap) {
+ /* glBitmap drawing */
+ struct gl_fragment_program *fp; /* we free this temp program below */
+
+ st_make_bitmap_fragment_program(st, &stfp->Base,
+ &fp, &variant->bitmap_sampler);
+
+ variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
+ stfp = st_fragment_program(fp);
+ deleteFP = GL_TRUE;
+ }
+ else if (key->drawpixels) {
+ /* glDrawPixels drawing */
+ struct gl_fragment_program *fp; /* we free this temp program below */
+
+ if (key->drawpixels_z || key->drawpixels_stencil) {
+ fp = st_make_drawpix_z_stencil_program(st, key->drawpixels_z,
+ key->drawpixels_stencil);
+ }
+ else {
+ /* RGBA */
+ st_make_drawpix_fragment_program(st, &stfp->Base, &fp);
+ variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
+ deleteFP = GL_TRUE;
+ }
+ stfp = st_fragment_program(fp);
+ }
+#endif
+
+ if (!stfp->tgsi.tokens) {
+ /* need to translate Mesa instructions to TGSI now */
+ GLuint outputMapping[FRAG_RESULT_MAX];
+ GLuint inputMapping[FRAG_ATTRIB_MAX];
+ GLuint interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
+ GLuint attr;
+ enum pipe_error error;
+ const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
+ struct ureg_program *ureg;
+ GLboolean write_all = GL_FALSE;
+
+ ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
+ ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
+ uint fs_num_inputs = 0;
+
+ ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
+ ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
+ uint fs_num_outputs = 0;
+
+
+ _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT);
+
+ /*
+ * Convert Mesa program inputs to TGSI input register semantics.
+ */
+ for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
+ if (inputsRead & (1 << attr)) {
+ const GLuint slot = fs_num_inputs++;
+
+ inputMapping[attr] = slot;
+
+ switch (attr) {
+ case FRAG_ATTRIB_WPOS:
+ input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
+ input_semantic_index[slot] = 0;
+ interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
+ break;
+ case FRAG_ATTRIB_COL0:
+ input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
+ input_semantic_index[slot] = 0;
+ interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
+ break;
+ case FRAG_ATTRIB_COL1:
+ input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
+ input_semantic_index[slot] = 1;
+ interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
+ break;
+ case FRAG_ATTRIB_FOGC:
+ input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
+ input_semantic_index[slot] = 0;
+ interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
+ break;
+ case FRAG_ATTRIB_FACE:
+ input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
+ input_semantic_index[slot] = 0;
+ interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
+ break;
+ /* In most cases, there is nothing special about these
+ * inputs, so adopt a convention to use the generic
+ * semantic name and the mesa FRAG_ATTRIB_ number as the
+ * index.
+ *
+ * All that is required is that the vertex shader labels
+ * its own outputs similarly, and that the vertex shader
+ * generates at least every output required by the
+ * fragment shader plus fixed-function hardware (such as
+ * BFC).
+ *
+ * There is no requirement that semantic indexes start at
+ * zero or be restricted to a particular range -- nobody
+ * should be building tables based on semantic index.
+ */
+ case FRAG_ATTRIB_PNTC:
+ case FRAG_ATTRIB_TEX0:
+ case FRAG_ATTRIB_TEX1:
+ case FRAG_ATTRIB_TEX2:
+ case FRAG_ATTRIB_TEX3:
+ case FRAG_ATTRIB_TEX4:
+ case FRAG_ATTRIB_TEX5:
+ case FRAG_ATTRIB_TEX6:
+ case FRAG_ATTRIB_TEX7:
+ case FRAG_ATTRIB_VAR0:
+ default:
+ /* Actually, let's try and zero-base this just for
+ * readability of the generated TGSI.
+ */
+ assert(attr >= FRAG_ATTRIB_TEX0);
+ input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
+ input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
+ if (attr == FRAG_ATTRIB_PNTC)
+ interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
+ else
+ interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
+ break;
+ }
+ }
+ else {
+ inputMapping[attr] = -1;
+ }
+ }
+
+ /*
+ * Semantics and mapping for outputs
+ */
+ {
+ uint numColors = 0;
+ GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
+
+ /* if z is written, emit that first */
+ if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
+ fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
+ fs_output_semantic_index[fs_num_outputs] = 0;
+ outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
+ fs_num_outputs++;
+ outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
+ }
+
+ if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
+ fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
+ fs_output_semantic_index[fs_num_outputs] = 0;
+ outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
+ fs_num_outputs++;
+ outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
+ }
+
+ /* handle remaning outputs (color) */
+ for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
+ if (outputsWritten & BITFIELD64_BIT(attr)) {
+ switch (attr) {
+ case FRAG_RESULT_DEPTH:
+ case FRAG_RESULT_STENCIL:
+ /* handled above */
+ assert(0);
+ break;
+ case FRAG_RESULT_COLOR:
+ write_all = GL_TRUE; /* fallthrough */
+ default:
+ assert(attr == FRAG_RESULT_COLOR ||
+ (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
+ fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
+ fs_output_semantic_index[fs_num_outputs] = numColors;
+ outputMapping[attr] = fs_num_outputs;
+ numColors++;
+ break;
+ }
+
+ fs_num_outputs++;
+ }
+ }
+ }
+
+ ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
+ if (ureg == NULL)
+ return NULL;
+
+ if (ST_DEBUG & DEBUG_MESA) {
+ _mesa_print_program(&stfp->Base.Base);
+ _mesa_print_program_parameters(st->ctx, &stfp->Base.Base);
+ debug_printf("\n");
+ }
+ if (write_all == GL_TRUE)
+ ureg_property_fs_color0_writes_all_cbufs(ureg, 1);
+
+ error = st_translate_mesa_program(st->ctx,
+ TGSI_PROCESSOR_FRAGMENT,
+ ureg,
+ &stfp->Base.Base,
+ /* inputs */
+ fs_num_inputs,
+ inputMapping,
+ input_semantic_name,
+ input_semantic_index,
+ interpMode,
+ /* outputs */
+ fs_num_outputs,
+ outputMapping,
+ fs_output_semantic_name,
+ fs_output_semantic_index, FALSE );
+
+ stfp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
+ ureg_destroy( ureg );
+ }
+
+ /* fill in variant */
+ variant->driver_shader = pipe->create_fs_state(pipe, &stfp->tgsi);
+ variant->key = *key;
+
+ if (ST_DEBUG & DEBUG_TGSI) {
+ tgsi_dump( stfp->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
+ debug_printf("\n");
+ }
+
+ if (deleteFP) {
+ /* Free the temporary program made above */
+ struct gl_fragment_program *fp = &stfp->Base;
+ _mesa_reference_fragprog(st->ctx, &fp, NULL);
+ }
+
+ return variant;
+}
+
+
+/**
+ * Translate fragment program if needed.
+ */
+struct st_fp_variant *
+st_get_fp_variant(struct st_context *st,
+ struct st_fragment_program *stfp,
+ const struct st_fp_variant_key *key)
+{
+ struct st_fp_variant *fpv;
+
+ /* Search for existing variant */
+ for (fpv = stfp->variants; fpv; fpv = fpv->next) {
+ if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
+ break;
+ }
+ }
+
+ if (!fpv) {
+ /* create new */
+ fpv = st_translate_fragment_program(st, stfp, key);
+ if (fpv) {
+ /* insert into list */
+ fpv->next = stfp->variants;
+ stfp->variants = fpv;
+ }
+ }
+
+ return fpv;
+}
+
+
+/**
+ * Translate a geometry program to create a new variant.
+ */
+static struct st_gp_variant *
+st_translate_geometry_program(struct st_context *st,
+ struct st_geometry_program *stgp,
+ const struct st_gp_variant_key *key)
+{
+ GLuint inputMapping[GEOM_ATTRIB_MAX];
+ GLuint outputMapping[GEOM_RESULT_MAX];
+ struct pipe_context *pipe = st->pipe;
+ enum pipe_error error;
+ GLuint attr;
+ const GLbitfield inputsRead = stgp->Base.Base.InputsRead;
+ GLuint vslot = 0;
+ GLuint num_generic = 0;
+
+ uint gs_num_inputs = 0;
+ uint gs_builtin_inputs = 0;
+ uint gs_array_offset = 0;
+
+ ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
+ ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
+ uint gs_num_outputs = 0;
+
+ GLint i;
+ GLuint maxSlot = 0;
+ struct ureg_program *ureg;
+
+ struct st_gp_variant *gpv;
+
+ gpv = CALLOC_STRUCT(st_gp_variant);
+ if (!gpv)
+ return NULL;
+
+ _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_OUTPUT);
+ _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_VARYING);
+
+ ureg = ureg_create( TGSI_PROCESSOR_GEOMETRY );
+ if (ureg == NULL) {
+ FREE(gpv);
+ return NULL;
+ }
+
+ /* which vertex output goes to the first geometry input */
+ vslot = 0;
+
+ memset(inputMapping, 0, sizeof(inputMapping));
+ memset(outputMapping, 0, sizeof(outputMapping));
+
+ /*
+ * Convert Mesa program inputs to TGSI input register semantics.
+ */
+ for (attr = 0; attr < GEOM_ATTRIB_MAX; attr++) {
+ if (inputsRead & (1 << attr)) {
+ const GLuint slot = gs_num_inputs;
+
+ gs_num_inputs++;
+
+ inputMapping[attr] = slot;
+
+ stgp->input_map[slot + gs_array_offset] = vslot - gs_builtin_inputs;
+ stgp->input_to_index[attr] = vslot;
+ stgp->index_to_input[vslot] = attr;
+ ++vslot;
+
+ if (attr != GEOM_ATTRIB_PRIMITIVE_ID) {
+ gs_array_offset += 2;
+ } else
+ ++gs_builtin_inputs;
+
+#if 0
+ debug_printf("input map at %d = %d\n",
+ slot + gs_array_offset, stgp->input_map[slot + gs_array_offset]);
+#endif
+
+ switch (attr) {
+ case GEOM_ATTRIB_PRIMITIVE_ID:
+ stgp->input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
+ stgp->input_semantic_index[slot] = 0;
+ break;
+ case GEOM_ATTRIB_POSITION:
+ stgp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
+ stgp->input_semantic_index[slot] = 0;
+ break;
+ case GEOM_ATTRIB_COLOR0:
+ stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
+ stgp->input_semantic_index[slot] = 0;
+ break;
+ case GEOM_ATTRIB_COLOR1:
+ stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
+ stgp->input_semantic_index[slot] = 1;
+ break;
+ case GEOM_ATTRIB_FOG_FRAG_COORD:
+ stgp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
+ stgp->input_semantic_index[slot] = 0;
+ break;
+ case GEOM_ATTRIB_TEX_COORD:
+ stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
+ stgp->input_semantic_index[slot] = num_generic++;
+ break;
+ case GEOM_ATTRIB_VAR0:
+ /* fall-through */
+ default:
+ stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
+ stgp->input_semantic_index[slot] = num_generic++;
+ }
+ }
+ }
+
+ /* initialize output semantics to defaults */
+ for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
+ gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
+ gs_output_semantic_index[i] = 0;
+ }
+
+ num_generic = 0;
+ /*
+ * Determine number of outputs, the (default) output register
+ * mapping and the semantic information for each output.
+ */
+ for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
+ if (stgp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) {
+ GLuint slot;
+
+ slot = gs_num_outputs;
+ gs_num_outputs++;
+ outputMapping[attr] = slot;
+
+ switch (attr) {
+ case GEOM_RESULT_POS:
+ assert(slot == 0);
+ gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
+ gs_output_semantic_index[slot] = 0;
+ break;
+ case GEOM_RESULT_COL0:
+ gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
+ gs_output_semantic_index[slot] = 0;
+ break;
+ case GEOM_RESULT_COL1:
+ gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
+ gs_output_semantic_index[slot] = 1;
+ break;
+ case GEOM_RESULT_SCOL0:
+ gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
+ gs_output_semantic_index[slot] = 0;
+ break;
+ case GEOM_RESULT_SCOL1:
+ gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
+ gs_output_semantic_index[slot] = 1;
+ break;
+ case GEOM_RESULT_FOGC:
+ gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
+ gs_output_semantic_index[slot] = 0;
+ break;
+ case GEOM_RESULT_PSIZ:
+ gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
+ gs_output_semantic_index[slot] = 0;
+ break;
+ case GEOM_RESULT_TEX0:
+ case GEOM_RESULT_TEX1:
+ case GEOM_RESULT_TEX2:
+ case GEOM_RESULT_TEX3:
+ case GEOM_RESULT_TEX4:
+ case GEOM_RESULT_TEX5:
+ case GEOM_RESULT_TEX6:
+ case GEOM_RESULT_TEX7:
+ /* fall-through */
+ case GEOM_RESULT_VAR0:
+ /* fall-through */
+ default:
+ assert(slot < Elements(gs_output_semantic_name));
+ /* use default semantic info */
+ gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
+ gs_output_semantic_index[slot] = num_generic++;
+ }
+ }
+ }
+
+ assert(gs_output_semantic_name[0] == TGSI_SEMANTIC_POSITION);
+
+ /* find max output slot referenced to compute gs_num_outputs */
+ for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
+ if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot)
+ maxSlot = outputMapping[attr];
+ }
+ gs_num_outputs = maxSlot + 1;
+
+#if 0 /* debug */
+ {
+ GLuint i;
+ printf("outputMapping? %d\n", outputMapping ? 1 : 0);
+ if (outputMapping) {
+ printf("attr -> slot\n");
+ for (i = 0; i < 16; i++) {
+ printf(" %2d %3d\n", i, outputMapping[i]);
+ }
+ }
+ printf("slot sem_name sem_index\n");
+ for (i = 0; i < gs_num_outputs; i++) {
+ printf(" %2d %d %d\n",
+ i,
+ gs_output_semantic_name[i],
+ gs_output_semantic_index[i]);
+ }
+ }
+#endif
+
+ /* free old shader state, if any */
+ if (stgp->tgsi.tokens) {
+ st_free_tokens(stgp->tgsi.tokens);
+ stgp->tgsi.tokens = NULL;
+ }
+
+ ureg_property_gs_input_prim(ureg, stgp->Base.InputType);
+ ureg_property_gs_output_prim(ureg, stgp->Base.OutputType);
+ ureg_property_gs_max_vertices(ureg, stgp->Base.VerticesOut);
+
+ error = st_translate_mesa_program(st->ctx,
+ TGSI_PROCESSOR_GEOMETRY,
+ ureg,
+ &stgp->Base.Base,
+ /* inputs */
+ gs_num_inputs,
+ inputMapping,
+ stgp->input_semantic_name,
+ stgp->input_semantic_index,
+ NULL,
+ /* outputs */
+ gs_num_outputs,
+ outputMapping,
+ gs_output_semantic_name,
+ gs_output_semantic_index,
+ FALSE);
+
+ stgp->num_inputs = gs_num_inputs;
+ stgp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
+ ureg_destroy( ureg );
+
+ /* fill in new variant */
+ gpv->driver_shader = pipe->create_gs_state(pipe, &stgp->tgsi);
+ gpv->key = *key;
+
+ if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
+ _mesa_print_program(&stgp->Base.Base);
+ debug_printf("\n");
+ }
+
+ if (ST_DEBUG & DEBUG_TGSI) {
+ tgsi_dump(stgp->tgsi.tokens, 0);
+ debug_printf("\n");
+ }
+
+ return gpv;
+}
+
+
+/**
+ * Get/create geometry program variant.
+ */
+struct st_gp_variant *
+st_get_gp_variant(struct st_context *st,
+ struct st_geometry_program *stgp,
+ const struct st_gp_variant_key *key)
+{
+ struct st_gp_variant *gpv;
+
+ /* Search for existing variant */
+ for (gpv = stgp->variants; gpv; gpv = gpv->next) {
+ if (memcmp(&gpv->key, key, sizeof(*key)) == 0) {
+ break;
+ }
+ }
+
+ if (!gpv) {
+ /* create new */
+ gpv = st_translate_geometry_program(st, stgp, key);
+ if (gpv) {
+ /* insert into list */
+ gpv->next = stgp->variants;
+ stgp->variants = gpv;
+ }
+ }
+
+ return gpv;
+}
+
+
+
+
+/**
+ * Debug- print current shader text
+ */
+void
+st_print_shaders(struct gl_context *ctx)
+{
+ struct gl_shader_program *shProg[3] = {
+ ctx->Shader.CurrentVertexProgram,
+ ctx->Shader.CurrentGeometryProgram,
+ ctx->Shader.CurrentFragmentProgram,
+ };
+ unsigned j;
+
+ for (j = 0; j < 3; j++) {
+ unsigned i;
+
+ if (shProg[j] == NULL)
+ continue;
+
+ for (i = 0; i < shProg[j]->NumShaders; i++) {
+ struct gl_shader *sh;
+
+ switch (shProg[j]->Shaders[i]->Type) {
+ case GL_VERTEX_SHADER:
+ sh = (i != 0) ? NULL : shProg[j]->Shaders[i];
+ break;
+ case GL_GEOMETRY_SHADER_ARB:
+ sh = (i != 1) ? NULL : shProg[j]->Shaders[i];
+ break;
+ case GL_FRAGMENT_SHADER:
+ sh = (i != 2) ? NULL : shProg[j]->Shaders[i];
+ break;
+ default:
+ assert(0);
+ sh = NULL;
+ break;
+ }
+
+ if (sh != NULL) {
+ printf("GLSL shader %u of %u:\n", i, shProg[j]->NumShaders);
+ printf("%s\n", sh->Source);
+ }
+ }
+ }
+}
+
+
+/**
+ * Vert/Geom/Frag programs have per-context variants. Free all the
+ * variants attached to the given program which match the given context.
+ */
+static void
+destroy_program_variants(struct st_context *st, struct gl_program *program)
+{
+ if (!program)
+ return;
+
+ switch (program->Target) {
+ case GL_VERTEX_PROGRAM_ARB:
+ {
+ struct st_vertex_program *stvp = (struct st_vertex_program *) program;
+ struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
+
+ for (vpv = stvp->variants; vpv; ) {
+ struct st_vp_variant *next = vpv->next;
+ if (vpv->key.st == st) {
+ /* unlink from list */
+ *prevPtr = next;
+ /* destroy this variant */
+ delete_vp_variant(st, vpv);
+ }
+ else {
+ prevPtr = &vpv->next;
+ }
+ vpv = next;
+ }
+ }
+ break;
+ case GL_FRAGMENT_PROGRAM_ARB:
+ {
+ struct st_fragment_program *stfp =
+ (struct st_fragment_program *) program;
+ struct st_fp_variant *fpv, **prevPtr = &stfp->variants;
+
+ for (fpv = stfp->variants; fpv; ) {
+ struct st_fp_variant *next = fpv->next;
+ if (fpv->key.st == st) {
+ /* unlink from list */
+ *prevPtr = next;
+ /* destroy this variant */
+ delete_fp_variant(st, fpv);
+ }
+ else {
+ prevPtr = &fpv->next;
+ }
+ fpv = next;
+ }
+ }
+ break;
+ case MESA_GEOMETRY_PROGRAM:
+ {
+ struct st_geometry_program *stgp =
+ (struct st_geometry_program *) program;
+ struct st_gp_variant *gpv, **prevPtr = &stgp->variants;
+
+ for (gpv = stgp->variants; gpv; ) {
+ struct st_gp_variant *next = gpv->next;
+ if (gpv->key.st == st) {
+ /* unlink from list */
+ *prevPtr = next;
+ /* destroy this variant */
+ delete_gp_variant(st, gpv);
+ }
+ else {
+ prevPtr = &gpv->next;
+ }
+ gpv = next;
+ }
+ }
+ break;
+ default:
+ _mesa_problem(NULL, "Unexpected program target in "
+ "destroy_program_variants_cb()");
+ }
+}
+
+
+/**
+ * Callback for _mesa_HashWalk. Free all the shader's program variants
+ * which match the given context.
+ */
+static void
+destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
+{
+ struct st_context *st = (struct st_context *) userData;
+ struct gl_shader *shader = (struct gl_shader *) data;
+
+ switch (shader->Type) {
+ case GL_SHADER_PROGRAM_MESA:
+ {
+ struct gl_shader_program *shProg = (struct gl_shader_program *) data;
+ GLuint i;
+
+ for (i = 0; i < shProg->NumShaders; i++) {
+ destroy_program_variants(st, shProg->Shaders[i]->Program);
+ }
+
+ destroy_program_variants(st, (struct gl_program *)
+ shProg->VertexProgram);
+ destroy_program_variants(st, (struct gl_program *)
+ shProg->FragmentProgram);
+ destroy_program_variants(st, (struct gl_program *)
+ shProg->GeometryProgram);
+ }
+ break;
+ case GL_VERTEX_SHADER:
+ case GL_FRAGMENT_SHADER:
+ case GL_GEOMETRY_SHADER:
+ {
+ destroy_program_variants(st, shader->Program);
+ }
+ break;
+ default:
+ assert(0);
+ }
+}
+
+
+/**
+ * Callback for _mesa_HashWalk. Free all the program variants which match
+ * the given context.
+ */
+static void
+destroy_program_variants_cb(GLuint key, void *data, void *userData)
+{
+ struct st_context *st = (struct st_context *) userData;
+ struct gl_program *program = (struct gl_program *) data;
+ destroy_program_variants(st, program);
+}
+
+
+/**
+ * Walk over all shaders and programs to delete any variants which
+ * belong to the given context.
+ * This is called during context tear-down.
+ */
+void
+st_destroy_program_variants(struct st_context *st)
+{
+ /* ARB vert/frag program */
+ _mesa_HashWalk(st->ctx->Shared->Programs,
+ destroy_program_variants_cb, st);
+
+ /* GLSL vert/frag/geom shaders */
+ _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
+ destroy_shader_program_variants_cb, st);
+}
|