1711af67fe4afa7d982649c101d68474be9294e07Brian/**************************************************************************
2711af67fe4afa7d982649c101d68474be9294e07Brian *
3711af67fe4afa7d982649c101d68474be9294e07Brian * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4711af67fe4afa7d982649c101d68474be9294e07Brian * All Rights Reserved.
5711af67fe4afa7d982649c101d68474be9294e07Brian *
6711af67fe4afa7d982649c101d68474be9294e07Brian * Permission is hereby granted, free of charge, to any person obtaining a
7711af67fe4afa7d982649c101d68474be9294e07Brian * copy of this software and associated documentation files (the
8711af67fe4afa7d982649c101d68474be9294e07Brian * "Software"), to deal in the Software without restriction, including
9711af67fe4afa7d982649c101d68474be9294e07Brian * without limitation the rights to use, copy, modify, merge, publish,
10711af67fe4afa7d982649c101d68474be9294e07Brian * distribute, sub license, and/or sell copies of the Software, and to
11711af67fe4afa7d982649c101d68474be9294e07Brian * permit persons to whom the Software is furnished to do so, subject to
12711af67fe4afa7d982649c101d68474be9294e07Brian * the following conditions:
13711af67fe4afa7d982649c101d68474be9294e07Brian *
14711af67fe4afa7d982649c101d68474be9294e07Brian * The above copyright notice and this permission notice (including the
15711af67fe4afa7d982649c101d68474be9294e07Brian * next paragraph) shall be included in all copies or substantial portions
16711af67fe4afa7d982649c101d68474be9294e07Brian * of the Software.
17711af67fe4afa7d982649c101d68474be9294e07Brian *
18711af67fe4afa7d982649c101d68474be9294e07Brian * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19711af67fe4afa7d982649c101d68474be9294e07Brian * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20711af67fe4afa7d982649c101d68474be9294e07Brian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21711af67fe4afa7d982649c101d68474be9294e07Brian * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22711af67fe4afa7d982649c101d68474be9294e07Brian * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23711af67fe4afa7d982649c101d68474be9294e07Brian * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24711af67fe4afa7d982649c101d68474be9294e07Brian * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25711af67fe4afa7d982649c101d68474be9294e07Brian *
26711af67fe4afa7d982649c101d68474be9294e07Brian **************************************************************************/
27711af67fe4afa7d982649c101d68474be9294e07Brian
28711af67fe4afa7d982649c101d68474be9294e07Brian /*
29711af67fe4afa7d982649c101d68474be9294e07Brian  * Authors:
30711af67fe4afa7d982649c101d68474be9294e07Brian  *   Keith Whitwell <keith@tungstengraphics.com>
31711af67fe4afa7d982649c101d68474be9294e07Brian  */
32711af67fe4afa7d982649c101d68474be9294e07Brian
33711af67fe4afa7d982649c101d68474be9294e07Brian
3447d30b0c2c36f952cc14deefb9f937f1b0a9b531Brian Paul#include "main/macros.h"
35711af67fe4afa7d982649c101d68474be9294e07Brian#include "st_context.h"
36943964a1e5bad86bdceb0a06d60fb3b302ebce6aKeith Whitwell#include "pipe/p_context.h"
37711af67fe4afa7d982649c101d68474be9294e07Brian#include "st_atom.h"
38711af67fe4afa7d982649c101d68474be9294e07Brian
39711af67fe4afa7d982649c101d68474be9294e07Brian
409267341bd01900bc78700928e5836d0f52572e38Brian/**
419267341bd01900bc78700928e5836d0f52572e38Brian * Scissor depends on the scissor box, and the framebuffer dimensions.
42711af67fe4afa7d982649c101d68474be9294e07Brian */
439267341bd01900bc78700928e5836d0f52572e38Brianstatic void
449267341bd01900bc78700928e5836d0f52572e38Brianupdate_scissor( struct st_context *st )
45711af67fe4afa7d982649c101d68474be9294e07Brian{
46ea92566ed9cabf5eb5d0993b39c4372d5bfcf3f1Brian   struct pipe_scissor_state scissor;
4799feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul   const struct gl_context *ctx = st->ctx;
4899feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul   const struct gl_framebuffer *fb = ctx->DrawBuffer;
49717831ea712c07e18f2c2e797c49754bf2500e5eBrian   GLint miny, maxy;
509267341bd01900bc78700928e5836d0f52572e38Brian
519267341bd01900bc78700928e5836d0f52572e38Brian   scissor.minx = 0;
529267341bd01900bc78700928e5836d0f52572e38Brian   scissor.miny = 0;
539267341bd01900bc78700928e5836d0f52572e38Brian   scissor.maxx = fb->Width;
549267341bd01900bc78700928e5836d0f52572e38Brian   scissor.maxy = fb->Height;
559267341bd01900bc78700928e5836d0f52572e38Brian
5699feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul   if (ctx->Scissor.Enabled) {
5747d30b0c2c36f952cc14deefb9f937f1b0a9b531Brian Paul      /* need to be careful here with xmax or ymax < 0 */
5899feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul      GLint xmax = MAX2(0, ctx->Scissor.X + ctx->Scissor.Width);
5999feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul      GLint ymax = MAX2(0, ctx->Scissor.Y + ctx->Scissor.Height);
6047d30b0c2c36f952cc14deefb9f937f1b0a9b531Brian Paul
6199feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul      if (ctx->Scissor.X > (GLint)scissor.minx)
6299feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul         scissor.minx = ctx->Scissor.X;
6399feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul      if (ctx->Scissor.Y > (GLint)scissor.miny)
6499feecc7d1d0a6efb2511859973d6029d9fed9fdBrian Paul         scissor.miny = ctx->Scissor.Y;
659267341bd01900bc78700928e5836d0f52572e38Brian
6647d30b0c2c36f952cc14deefb9f937f1b0a9b531Brian Paul      if (xmax < (GLint) scissor.maxx)
6747d30b0c2c36f952cc14deefb9f937f1b0a9b531Brian Paul         scissor.maxx = xmax;
6847d30b0c2c36f952cc14deefb9f937f1b0a9b531Brian Paul      if (ymax < (GLint) scissor.maxy)
6947d30b0c2c36f952cc14deefb9f937f1b0a9b531Brian Paul         scissor.maxy = ymax;
709267341bd01900bc78700928e5836d0f52572e38Brian
719267341bd01900bc78700928e5836d0f52572e38Brian      /* check for null space */
729267341bd01900bc78700928e5836d0f52572e38Brian      if (scissor.minx >= scissor.maxx || scissor.miny >= scissor.maxy)
739267341bd01900bc78700928e5836d0f52572e38Brian         scissor.minx = scissor.miny = scissor.maxx = scissor.maxy = 0;
749267341bd01900bc78700928e5836d0f52572e38Brian   }
75711af67fe4afa7d982649c101d68474be9294e07Brian
76c060265bdb953f0c9d73e60f08c53a2e3b1a1176Brian Paul   /* Now invert Y if needed.
77c060265bdb953f0c9d73e60f08c53a2e3b1a1176Brian Paul    * Gallium drivers use the convention Y=0=top for surfaces.
78717831ea712c07e18f2c2e797c49754bf2500e5eBrian    */
79c060265bdb953f0c9d73e60f08c53a2e3b1a1176Brian Paul   if (st_fb_orientation(fb) == Y_0_TOP) {
80c060265bdb953f0c9d73e60f08c53a2e3b1a1176Brian Paul      miny = fb->Height - scissor.maxy;
81c060265bdb953f0c9d73e60f08c53a2e3b1a1176Brian Paul      maxy = fb->Height - scissor.miny;
82c060265bdb953f0c9d73e60f08c53a2e3b1a1176Brian Paul      scissor.miny = miny;
83c060265bdb953f0c9d73e60f08c53a2e3b1a1176Brian Paul      scissor.maxy = maxy;
84c060265bdb953f0c9d73e60f08c53a2e3b1a1176Brian Paul   }
85717831ea712c07e18f2c2e797c49754bf2500e5eBrian
86711af67fe4afa7d982649c101d68474be9294e07Brian   if (memcmp(&scissor, &st->state.scissor, sizeof(scissor)) != 0) {
87711af67fe4afa7d982649c101d68474be9294e07Brian      /* state has changed */
88711af67fe4afa7d982649c101d68474be9294e07Brian      st->state.scissor = scissor;  /* struct copy */
89ea92566ed9cabf5eb5d0993b39c4372d5bfcf3f1Brian      st->pipe->set_scissor_state(st->pipe, &scissor); /* activate */
90711af67fe4afa7d982649c101d68474be9294e07Brian   }
91711af67fe4afa7d982649c101d68474be9294e07Brian}
92711af67fe4afa7d982649c101d68474be9294e07Brian
93711af67fe4afa7d982649c101d68474be9294e07Brian
94711af67fe4afa7d982649c101d68474be9294e07Brianconst struct st_tracked_state st_update_scissor = {
9554507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   "st_update_scissor",					/* name */
9654507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   {							/* dirty */
9754507125e735ffa595e252282eaabf38095c21e1Alan Hourihane      (_NEW_SCISSOR | _NEW_BUFFERS),			/* mesa */
9854507125e735ffa595e252282eaabf38095c21e1Alan Hourihane      0,						/* st */
99711af67fe4afa7d982649c101d68474be9294e07Brian   },
10054507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   update_scissor					/* update */
101711af67fe4afa7d982649c101d68474be9294e07Brian};
102