18e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell/**************************************************************************
28e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
3877128505431adaf817dc8069172ebe4a1cdf5d8José Fonseca * Copyright 2007 VMware, Inc.
48e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * All Rights Reserved.
58e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
68e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
78e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * copy of this software and associated documentation files (the
88e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * "Software"), to deal in the Software without restriction, including
98e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * without limitation the rights to use, copy, modify, merge, publish,
108e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * distribute, sub license, and/or sell copies of the Software, and to
118e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * permit persons to whom the Software is furnished to do so, subject to
128e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * the following conditions:
138e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
148e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * The above copyright notice and this permission notice (including the
158e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * next paragraph) shall be included in all copies or substantial portions
168e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * of the Software.
178e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
188e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
198e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
208e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21877128505431adaf817dc8069172ebe4a1cdf5d8José Fonseca * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
228e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
238e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
248e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
258e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *
268e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell **************************************************************************/
278e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
288e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
29101d1a658a614d1e2ec02b1e697f6161291af653José Fonseca#include "main/context.h"
306340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich#include "main/viewport.h"
318e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell#include "st_context.h"
328e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell#include "st_atom.h"
337d95efde0a0e13e13c59444703bc47eb13926385Brian Paul#include "pipe/p_context.h"
347d95efde0a0e13e13c59444703bc47eb13926385Brian Paul#include "cso_cache/cso_context.h"
358e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
368e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell/**
378e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell * Update the viewport transformation matrix.  Depends on:
388e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *  - viewport pos/size
398e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *  - depthrange
408e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell *  - window pos/size or FBO size
418e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell */
424698483f849ba8dfde20a5d649f1ea099291f241Brian Paulstatic void
434698483f849ba8dfde20a5d649f1ea099291f241Brian Paulupdate_viewport( struct st_context *st )
448e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell{
45f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = st->ctx;
463d31252d44fb5983a089b9f3488745757772adeaBrian Paul   GLfloat yScale, yBias;
473cb10cce371cb62e0c4a988ab939bf640b75ebabJan Vesely   unsigned i;
483d31252d44fb5983a089b9f3488745757772adeaBrian Paul   /* _NEW_BUFFERS
493d31252d44fb5983a089b9f3488745757772adeaBrian Paul    */
504698483f849ba8dfde20a5d649f1ea099291f241Brian Paul   if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
51bd67962c5e8469062516da396e9c466a9446e00dBrian Paul      /* Drawing to a window.  The corresponding gallium surface uses
52bd67962c5e8469062516da396e9c466a9446e00dBrian Paul       * Y=0=TOP but OpenGL is Y=0=BOTTOM.  So we need to invert the viewport.
53bd67962c5e8469062516da396e9c466a9446e00dBrian Paul       */
543d31252d44fb5983a089b9f3488745757772adeaBrian Paul      yScale = -1;
5553174afeeb68a79e471185cb463c13ff90af698fJosé Fonseca      yBias = (GLfloat)ctx->DrawBuffer->Height;
563d31252d44fb5983a089b9f3488745757772adeaBrian Paul   }
573d31252d44fb5983a089b9f3488745757772adeaBrian Paul   else {
58bd67962c5e8469062516da396e9c466a9446e00dBrian Paul      /* Drawing to an FBO where Y=0=BOTTOM, like OpenGL - don't invert */
593d31252d44fb5983a089b9f3488745757772adeaBrian Paul      yScale = 1.0;
603d31252d44fb5983a089b9f3488745757772adeaBrian Paul      yBias = 0.0;
618e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   }
628e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
633d31252d44fb5983a089b9f3488745757772adeaBrian Paul   /* _NEW_VIEWPORT
643d31252d44fb5983a089b9f3488745757772adeaBrian Paul    */
650705fa35cdaf15ec969c28dc85e88b8be1149a3bDave Airlie   for (i = 0; i < ctx->Const.MaxViewports; i++)
668e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   {
67f8a647883a14694f1b758c12187b3f35b9d039a7Matt Turner      float scale[3], translate[3];
686340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich      _mesa_get_viewport_xform(ctx, i, scale, translate);
696340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich
706340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich      st->state.viewport[i].scale[0] = scale[0];
716340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich      st->state.viewport[i].scale[1] = scale[1] * yScale;
726340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich      st->state.viewport[i].scale[2] = scale[2];
738e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
746340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich      st->state.viewport[i].translate[0] = translate[0];
756340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich      st->state.viewport[i].translate[1] = translate[1] * yScale + yBias;
766340e609a354770e04192b9b44e91fb06aab0159Mathias Fröhlich      st->state.viewport[i].translate[2] = translate[2];
778e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell   }
780705fa35cdaf15ec969c28dc85e88b8be1149a3bDave Airlie
790705fa35cdaf15ec969c28dc85e88b8be1149a3bDave Airlie   cso_set_viewport(st->cso_context, &st->state.viewport[0]);
800705fa35cdaf15ec969c28dc85e88b8be1149a3bDave Airlie   if (ctx->Const.MaxViewports > 1)
810705fa35cdaf15ec969c28dc85e88b8be1149a3bDave Airlie      st->pipe->set_viewport_states(st->pipe, 1, ctx->Const.MaxViewports - 1, &st->state.viewport[1]);
828e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell}
838e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
848e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell
858e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwellconst struct st_tracked_state st_update_viewport = {
8654507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   update_viewport					/* update */
878e4a95a93d15a6707a29454cd47e10b08314cda2Keith Whitwell};
88