1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg
26848b8605Smrg#include "main/glheader.h"
27848b8605Smrg#include "main/context.h"
28848b8605Smrg#include "main/imports.h"
29848b8605Smrg#include "main/macros.h"
30848b8605Smrg
31848b8605Smrg#include "s_context.h"
32848b8605Smrg#include "s_logic.h"
33848b8605Smrg#include "s_span.h"
34848b8605Smrg
35848b8605Smrg
36848b8605Smrg/**
37848b8605Smrg * We do all logic ops on 4-byte GLuints.
38848b8605Smrg * Depending on bytes per pixel, the mask array elements correspond to
39848b8605Smrg * 1, 2 or 4 GLuints.
40848b8605Smrg */
41848b8605Smrg#define LOGIC_OP_LOOP(MODE, MASKSTRIDE)		\
42848b8605Smrgdo {						\
43848b8605Smrg   GLuint i;					\
44848b8605Smrg   switch (MODE) {				\
45848b8605Smrg      case GL_CLEAR:				\
46848b8605Smrg         for (i = 0; i < n; i++) {		\
47848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
48848b8605Smrg	       src[i] = 0;			\
49848b8605Smrg	    }					\
50848b8605Smrg	 }					\
51848b8605Smrg	 break;					\
52848b8605Smrg      case GL_SET:				\
53848b8605Smrg         for (i = 0; i < n; i++) {		\
54848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
55848b8605Smrg	       src[i] = ~0;			\
56848b8605Smrg	    }					\
57848b8605Smrg	 }					\
58848b8605Smrg	 break;					\
59848b8605Smrg      case GL_COPY:				\
60848b8605Smrg	 /* do nothing */			\
61848b8605Smrg	 break;					\
62848b8605Smrg      case GL_COPY_INVERTED:			\
63848b8605Smrg         for (i = 0; i < n; i++) {		\
64848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
65848b8605Smrg	       src[i] = ~src[i];		\
66848b8605Smrg	    }					\
67848b8605Smrg	 }					\
68848b8605Smrg	 break;					\
69848b8605Smrg      case GL_NOOP:				\
70848b8605Smrg         for (i = 0; i < n; i++) {		\
71848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
72848b8605Smrg	       src[i] = dest[i];		\
73848b8605Smrg	    }					\
74848b8605Smrg	 }					\
75848b8605Smrg	 break;					\
76848b8605Smrg      case GL_INVERT:				\
77848b8605Smrg         for (i = 0; i < n; i++) {		\
78848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
79848b8605Smrg	       src[i] = ~dest[i];		\
80848b8605Smrg	    }					\
81848b8605Smrg	 }					\
82848b8605Smrg	 break;					\
83848b8605Smrg      case GL_AND:				\
84848b8605Smrg         for (i = 0; i < n; i++) {		\
85848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
86848b8605Smrg	       src[i] &= dest[i];		\
87848b8605Smrg	    }					\
88848b8605Smrg	 }					\
89848b8605Smrg	 break;					\
90848b8605Smrg      case GL_NAND:				\
91848b8605Smrg         for (i = 0; i < n; i++) {		\
92848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
93848b8605Smrg	       src[i] = ~(src[i] & dest[i]);	\
94848b8605Smrg	    }					\
95848b8605Smrg	 }					\
96848b8605Smrg	 break;					\
97848b8605Smrg      case GL_OR:				\
98848b8605Smrg         for (i = 0; i < n; i++) {		\
99848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
100848b8605Smrg	       src[i] |= dest[i];		\
101848b8605Smrg	    }					\
102848b8605Smrg	 }					\
103848b8605Smrg	 break;					\
104848b8605Smrg      case GL_NOR:				\
105848b8605Smrg         for (i = 0; i < n; i++) {		\
106848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
107848b8605Smrg	       src[i] = ~(src[i] | dest[i]);	\
108848b8605Smrg	    }					\
109848b8605Smrg	 }					\
110848b8605Smrg	 break;					\
111848b8605Smrg      case GL_XOR:				\
112848b8605Smrg         for (i = 0; i < n; i++) {		\
113848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
114848b8605Smrg	       src[i] ^= dest[i];		\
115848b8605Smrg	    }					\
116848b8605Smrg	 }					\
117848b8605Smrg	 break;					\
118848b8605Smrg      case GL_EQUIV:				\
119848b8605Smrg         for (i = 0; i < n; i++) {		\
120848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
121848b8605Smrg	       src[i] = ~(src[i] ^ dest[i]);	\
122848b8605Smrg	    }					\
123848b8605Smrg	 }					\
124848b8605Smrg	 break;					\
125848b8605Smrg      case GL_AND_REVERSE:			\
126848b8605Smrg         for (i = 0; i < n; i++) {		\
127848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
128848b8605Smrg	       src[i] = src[i] & ~dest[i];	\
129848b8605Smrg	    }					\
130848b8605Smrg	 }					\
131848b8605Smrg	 break;					\
132848b8605Smrg      case GL_AND_INVERTED:			\
133848b8605Smrg         for (i = 0; i < n; i++) {		\
134848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
135848b8605Smrg	       src[i] = ~src[i] & dest[i];	\
136848b8605Smrg	    }					\
137848b8605Smrg	 }					\
138848b8605Smrg	 break;					\
139848b8605Smrg      case GL_OR_REVERSE:			\
140848b8605Smrg         for (i = 0; i < n; i++) {		\
141848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
142848b8605Smrg	       src[i] = src[i] | ~dest[i];	\
143848b8605Smrg	    }					\
144848b8605Smrg	 }					\
145848b8605Smrg	 break;					\
146848b8605Smrg      case GL_OR_INVERTED:			\
147848b8605Smrg         for (i = 0; i < n; i++) {		\
148848b8605Smrg	    if (mask[i / MASKSTRIDE]) {		\
149848b8605Smrg	       src[i] = ~src[i] | dest[i];	\
150848b8605Smrg	    }					\
151848b8605Smrg	 }					\
152848b8605Smrg	 break;					\
153848b8605Smrg      default:					\
154848b8605Smrg	 _mesa_problem(ctx, "bad logicop mode");\
155848b8605Smrg   }						\
156848b8605Smrg} while (0)
157848b8605Smrg
158848b8605Smrg
159848b8605Smrg
160848b8605Smrgstatic inline void
161848b8605Smrglogicop_uint1(struct gl_context *ctx, GLuint n, GLuint src[], const GLuint dest[],
162848b8605Smrg              const GLubyte mask[])
163848b8605Smrg{
164848b8605Smrg   LOGIC_OP_LOOP(ctx->Color.LogicOp, 1);
165848b8605Smrg}
166848b8605Smrg
167848b8605Smrg
168848b8605Smrgstatic inline void
169848b8605Smrglogicop_uint2(struct gl_context *ctx, GLuint n, GLuint src[], const GLuint dest[],
170848b8605Smrg              const GLubyte mask[])
171848b8605Smrg{
172848b8605Smrg   LOGIC_OP_LOOP(ctx->Color.LogicOp, 2);
173848b8605Smrg}
174848b8605Smrg
175848b8605Smrg
176848b8605Smrgstatic inline void
177848b8605Smrglogicop_uint4(struct gl_context *ctx, GLuint n, GLuint src[], const GLuint dest[],
178848b8605Smrg              const GLubyte mask[])
179848b8605Smrg{
180848b8605Smrg   LOGIC_OP_LOOP(ctx->Color.LogicOp, 4);
181848b8605Smrg}
182848b8605Smrg
183848b8605Smrg
184848b8605Smrg
185848b8605Smrg/**
186848b8605Smrg * Apply the current logic operator to a span of RGBA pixels.
187848b8605Smrg * We can handle horizontal runs of pixels (spans) or arrays of x/y
188848b8605Smrg * pixel coordinates.
189848b8605Smrg */
190848b8605Smrgvoid
191848b8605Smrg_swrast_logicop_rgba_span(struct gl_context *ctx, struct gl_renderbuffer *rb,
192848b8605Smrg                          SWspan *span)
193848b8605Smrg{
194848b8605Smrg   void *rbPixels;
195848b8605Smrg
196b8e80941Smrg   assert(span->end < SWRAST_MAX_WIDTH);
197b8e80941Smrg   assert(span->arrayMask & SPAN_RGBA);
198848b8605Smrg
199848b8605Smrg   rbPixels = _swrast_get_dest_rgba(ctx, rb, span);
200848b8605Smrg
201848b8605Smrg   if (span->array->ChanType == GL_UNSIGNED_BYTE) {
202848b8605Smrg      /* treat 4*GLubyte as GLuint */
203848b8605Smrg      logicop_uint1(ctx, span->end,
204848b8605Smrg                    (GLuint *) span->array->rgba8,
205848b8605Smrg                    (const GLuint *) rbPixels, span->array->mask);
206848b8605Smrg   }
207848b8605Smrg   else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
208848b8605Smrg      /* treat 2*GLushort as GLuint */
209848b8605Smrg      logicop_uint2(ctx, 2 * span->end,
210848b8605Smrg                    (GLuint *) span->array->rgba16,
211848b8605Smrg                    (const GLuint *) rbPixels, span->array->mask);
212848b8605Smrg   }
213848b8605Smrg   else {
214848b8605Smrg      logicop_uint4(ctx, 4 * span->end,
215848b8605Smrg                    (GLuint *) span->array->attribs[VARYING_SLOT_COL0],
216848b8605Smrg                    (const GLuint *) rbPixels, span->array->mask);
217848b8605Smrg   }
218848b8605Smrg}
219