1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**************************************************************************
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved.
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sub license, and/or sell copies of the Software, and to
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions:
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial portions
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software.
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **************************************************************************/
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /*
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  * \brief polygon stipple state
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  *
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  * Authors:
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  *   Brian Paul
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org  */
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <assert.h>
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "st_context.h"
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "st_atom.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_context.h"
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_defines.h"
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OpenGL's polygon stipple is indexed with window coordinates in which
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the origin (0,0) is the lower-left corner of the window.
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * With Gallium, the origin is the upper-left corner of the window.
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * To convert GL's polygon stipple to what gallium expects we need to
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * invert the pattern vertically and rotate the stipple rows according
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to the window height.
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginvert_stipple(GLuint dest[32], const GLuint src[32], GLuint winHeight)
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint i;
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < 32; i++) {
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      dest[i] = src[(winHeight - 1 - i) & 0x1f];
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgupdate_stipple( struct st_context *st )
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const struct gl_context *ctx = st->ctx;
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLuint sz = sizeof(st->state.poly_stipple);
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(sz == sizeof(ctx->PolygonStipple));
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (memcmp(st->state.poly_stipple, ctx->PolygonStipple, sz)) {
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* state has changed */
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      struct pipe_poly_stipple newStipple;
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      memcpy(st->state.poly_stipple, ctx->PolygonStipple, sz);
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      invert_stipple(newStipple.stipple, ctx->PolygonStipple,
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                     ctx->DrawBuffer->Height);
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      st->pipe->set_polygon_stipple(st->pipe, &newStipple);
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** Update the stipple when the pattern or window height changes */
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst struct st_tracked_state st_update_polygon_stipple = {
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   "st_update_polygon_stipple",				/* name */
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {							/* dirty */
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      (_NEW_POLYGONSTIPPLE |
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       _NEW_BUFFERS),					/* mesa */
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      0,						/* st */
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   },
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   update_stipple					/* update */
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
95