11ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt/*
21ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * Copyright © 2009 Intel Corporation
31ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt *
41ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
51ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * copy of this software and associated documentation files (the "Software"),
61ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * to deal in the Software without restriction, including without limitation
71ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * and/or sell copies of the Software, and to permit persons to whom the
91ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * Software is furnished to do so, subject to the following conditions:
101ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt *
111ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * The above copyright notice and this permission notice (including the next
121ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * paragraph) shall be included in all copies or substantial portions of the
131ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * Software.
141ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt *
151ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
211ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * IN THE SOFTWARE.
221ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt *
231ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * Authors:
241ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt *    Eric Anholt <eric@anholt.net>
251ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt *
261ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt */
271ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
281ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt#include "brw_context.h"
291ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt#include "brw_state.h"
301ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt#include "brw_defines.h"
311ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt#include "intel_batchbuffer.h"
324433b0302d0aa9dc61002e8bb4fd1b752b0be338Brian Paul#include "main/fbobject.h"
331ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
341ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt/* The clip VP defines the guardband region where expensive clipping is skipped
351ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt * and fragments are allowed to be generated and clipped out cheaply by the SF.
36dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden *
37dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden * By setting it to NDC bounds of [-1,1], we don't do GB clipping.  It's
38dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden * supposed to cause seams to become visible in apps due to shared edges taking
39dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden * different clip/no clip paths depending on whether the rest of the prim ends
40dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden * up in the guardband or not.
411ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt */
421ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholtstatic void
43752cad8f33ad1adc8950e5c0b5c9c4aa2ad046ecEric Anholtgen6_upload_clip_vp(struct brw_context *brw)
441ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt{
453628e123f13a480abfad8e8af9f875e3d8eeb5a6Eric Anholt   struct brw_clipper_viewport *vp;
461ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
47d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt   vp = brw_state_batch(brw, AUB_TRACE_CLIP_VP_STATE,
48d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt			sizeof(*vp), 32, &brw->clip.vp_offset);
491ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
50dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden   vp->xmin = -1.0;
51dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden   vp->xmax = 1.0;
52dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden   vp->ymin = -1.0;
53dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden   vp->ymax = 1.0;
543628e123f13a480abfad8e8af9f875e3d8eeb5a6Eric Anholt
553628e123f13a480abfad8e8af9f875e3d8eeb5a6Eric Anholt   brw->state.dirty.cache |= CACHE_NEW_CLIP_VP;
561ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt}
571ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
581ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholtconst struct brw_tracked_state gen6_clip_vp = {
591ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   .dirty = {
60dbe13c105faafed6823c32ed37596f530577be91Oliver McFadden      .mesa = 0,
613628e123f13a480abfad8e8af9f875e3d8eeb5a6Eric Anholt      .brw = BRW_NEW_BATCH,
621ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      .cache = 0,
631ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   },
64752cad8f33ad1adc8950e5c0b5c9c4aa2ad046ecEric Anholt   .emit = gen6_upload_clip_vp,
651ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt};
661ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
671ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholtstatic void
68752cad8f33ad1adc8950e5c0b5c9c4aa2ad046ecEric Anholtgen6_upload_sf_vp(struct brw_context *brw)
691ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt{
70f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
711ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   const GLfloat depth_scale = 1.0F / ctx->DrawBuffer->_DepthMaxF;
728d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   struct brw_sf_viewport *sfv;
731ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   GLfloat y_scale, y_bias;
744433b0302d0aa9dc61002e8bb4fd1b752b0be338Brian Paul   const bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
751ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   const GLfloat *v = ctx->Viewport._WindowMap.m;
761ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
77d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt   sfv = brw_state_batch(brw, AUB_TRACE_SF_VP_STATE,
78d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt			 sizeof(*sfv), 32, &brw->sf.vp_offset);
798d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   memset(sfv, 0, sizeof(*sfv));
801ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
811ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   /* _NEW_BUFFERS */
821ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   if (render_to_fbo) {
831ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      y_scale = 1.0;
841ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      y_bias = 0;
851ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   } else {
861ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      y_scale = -1.0;
871ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      y_bias = ctx->DrawBuffer->Height;
881ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   }
891ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
901ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   /* _NEW_VIEWPORT */
918d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   sfv->viewport.m00 = v[MAT_SX];
928d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   sfv->viewport.m11 = v[MAT_SY] * y_scale;
938d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   sfv->viewport.m22 = v[MAT_SZ] * depth_scale;
948d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   sfv->viewport.m30 = v[MAT_TX];
958d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   sfv->viewport.m31 = v[MAT_TY] * y_scale + y_bias;
968d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   sfv->viewport.m32 = v[MAT_TZ] * depth_scale;
978d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt
988d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt   brw->state.dirty.cache |= CACHE_NEW_SF_VP;
991ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt}
1001ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
1011ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholtconst struct brw_tracked_state gen6_sf_vp = {
1021ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   .dirty = {
1031ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      .mesa = _NEW_VIEWPORT | _NEW_BUFFERS,
1048d2047ca7e8a533e1853a2ba51cd1bd6e52ae4a3Eric Anholt      .brw = BRW_NEW_BATCH,
1051ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      .cache = 0,
1061ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   },
107752cad8f33ad1adc8950e5c0b5c9c4aa2ad046ecEric Anholt   .emit = gen6_upload_sf_vp,
1081ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt};
1091ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
1101ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholtstatic void upload_viewport_state_pointers(struct brw_context *brw)
1111ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt{
1121ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   struct intel_context *intel = &brw->intel;
1131ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
1141ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   BEGIN_BATCH(4);
115e31defc825ee94f1d1092e277954abad7097c552Kenneth Graunke   OUT_BATCH(_3DSTATE_VIEWPORT_STATE_POINTERS << 16 | (4 - 2) |
1161ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt	     GEN6_CC_VIEWPORT_MODIFY |
1171ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt	     GEN6_SF_VIEWPORT_MODIFY |
1181ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt	     GEN6_CLIP_VIEWPORT_MODIFY);
119a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   OUT_BATCH(brw->clip.vp_offset);
120a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   OUT_BATCH(brw->sf.vp_offset);
121a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   OUT_BATCH(brw->cc.vp_offset);
1221ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   ADVANCE_BATCH();
1231ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt}
1241ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt
1251ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholtconst struct brw_tracked_state gen6_viewport_state = {
1261ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   .dirty = {
1271ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      .mesa = 0,
128a09c5c2e3053c48a33134cf28229105bfef52e6fEric Anholt      .brw = (BRW_NEW_BATCH |
129a09c5c2e3053c48a33134cf28229105bfef52e6fEric Anholt	      BRW_NEW_STATE_BASE_ADDRESS),
1301ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt      .cache = (CACHE_NEW_CLIP_VP |
1311ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt		CACHE_NEW_SF_VP |
1321ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt		CACHE_NEW_CC_VP)
1331ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   },
1341ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt   .emit = upload_viewport_state_pointers,
1351ae0cb5f286bbba10e99c8e3bc1c55d2aeb38b59Eric Anholt};
136