1b9abf16eSmaya/**************************************************************************
2b9abf16eSmaya *
3b9abf16eSmaya * Copyright 2019 VMware, Inc.
4b9abf16eSmaya * All Rights Reserved.
5b9abf16eSmaya *
6b9abf16eSmaya * Permission is hereby granted, free of charge, to any person obtaining a
7b9abf16eSmaya * copy of this software and associated documentation files (the
8b9abf16eSmaya * "Software"), to deal in the Software without restriction, including
9b9abf16eSmaya * without limitation the rights to use, copy, modify, merge, publish,
10b9abf16eSmaya * distribute, sub license, and/or sell copies of the Software, and to
11b9abf16eSmaya * permit persons to whom the Software is furnished to do so, subject to
12b9abf16eSmaya * the following conditions:
13b9abf16eSmaya *
14b9abf16eSmaya * The above copyright notice and this permission notice (including the
15b9abf16eSmaya * next paragraph) shall be included in all copies or substantial portions
16b9abf16eSmaya * of the Software.
17b9abf16eSmaya *
18b9abf16eSmaya * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19b9abf16eSmaya * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20b9abf16eSmaya * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21b9abf16eSmaya * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22b9abf16eSmaya * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23b9abf16eSmaya * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24b9abf16eSmaya * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25b9abf16eSmaya *
26b9abf16eSmaya **************************************************************************/
27b9abf16eSmaya
28b9abf16eSmaya
29b9abf16eSmaya/*
30b9abf16eSmaya * Miscellantous state tracker utility functions, macros.
31b9abf16eSmaya */
32b9abf16eSmaya
33b9abf16eSmaya
34b9abf16eSmaya#ifndef ST_UTIL
35b9abf16eSmaya#define ST_UTIL
36b9abf16eSmaya
37b9abf16eSmaya
38b9abf16eSmaya#include "state_tracker/st_context.h"
39b9abf16eSmaya
40b9abf16eSmaya
41b9abf16eSmaya#ifdef __cplusplus
42b9abf16eSmayaextern "C" {
43b9abf16eSmaya#endif
44b9abf16eSmaya
45b9abf16eSmaya
46b9abf16eSmaya/** For drawing quads for glClear, glDraw/CopyPixels, glBitmap, etc. */
47b9abf16eSmayastruct st_util_vertex
48b9abf16eSmaya{
49b9abf16eSmaya   float x, y, z;
50b9abf16eSmaya   float r, g, b, a;
51b9abf16eSmaya   float s, t;
52b9abf16eSmaya};
53b9abf16eSmaya
54b9abf16eSmaya
55b9abf16eSmaya
56b9abf16eSmaya/* Invalidate the readpixels cache to ensure we don't read stale data.
57b9abf16eSmaya */
58b9abf16eSmayastatic inline void
59b9abf16eSmayast_invalidate_readpix_cache(struct st_context *st)
60b9abf16eSmaya{
61b9abf16eSmaya   if (unlikely(st->readpix_cache.src)) {
62b9abf16eSmaya      pipe_resource_reference(&st->readpix_cache.src, NULL);
63b9abf16eSmaya      pipe_resource_reference(&st->readpix_cache.cache, NULL);
64b9abf16eSmaya   }
65b9abf16eSmaya}
66b9abf16eSmaya
67b9abf16eSmaya
68b9abf16eSmaya#define Y_0_TOP 1
69b9abf16eSmaya#define Y_0_BOTTOM 2
70b9abf16eSmaya
71b9abf16eSmayastatic inline GLuint
72b9abf16eSmayast_fb_orientation(const struct gl_framebuffer *fb)
73b9abf16eSmaya{
747ec681f3Smrg   if (fb && fb->FlipY) {
75b9abf16eSmaya      /* Drawing into a window (on-screen buffer).
76b9abf16eSmaya       *
77b9abf16eSmaya       * Negate Y scale to flip image vertically.
78b9abf16eSmaya       * The NDC Y coords prior to viewport transformation are in the range
79b9abf16eSmaya       * [y=-1=bottom, y=1=top]
80b9abf16eSmaya       * Hardware window coords are in the range [y=0=top, y=H-1=bottom] where
81b9abf16eSmaya       * H is the window height.
82b9abf16eSmaya       * Use the viewport transformation to invert Y.
83b9abf16eSmaya       */
84b9abf16eSmaya      return Y_0_TOP;
85b9abf16eSmaya   }
86b9abf16eSmaya   else {
87b9abf16eSmaya      /* Drawing into user-created FBO (very likely a texture).
88b9abf16eSmaya       *
89b9abf16eSmaya       * For textures, T=0=Bottom, so by extension Y=0=Bottom for rendering.
90b9abf16eSmaya       */
91b9abf16eSmaya      return Y_0_BOTTOM;
92b9abf16eSmaya   }
93b9abf16eSmaya}
94b9abf16eSmaya
95b9abf16eSmaya
96b9abf16eSmayastatic inline bool
97b9abf16eSmayast_user_clip_planes_enabled(struct gl_context *ctx)
98b9abf16eSmaya{
99b9abf16eSmaya   return (ctx->API == API_OPENGL_COMPAT ||
100b9abf16eSmaya           ctx->API == API_OPENGLES) && /* only ES 1.x */
101b9abf16eSmaya          ctx->Transform.ClipPlanesEnabled;
102b9abf16eSmaya}
103b9abf16eSmaya
1047ec681f3Smrgstatic inline bool
1057ec681f3Smrgst_point_size_per_vertex(struct gl_context *ctx)
1067ec681f3Smrg{
1077ec681f3Smrg   const struct gl_program *vertProg = ctx->VertexProgram._Current;
1087ec681f3Smrg   if (vertProg) {
1097ec681f3Smrg      if (vertProg->Id == 0) {
1107ec681f3Smrg         if (vertProg->info.outputs_written &
1117ec681f3Smrg             BITFIELD64_BIT(VARYING_SLOT_PSIZ)) {
1127ec681f3Smrg            /* generated program which emits point size */
1137ec681f3Smrg            return true;
1147ec681f3Smrg         }
1157ec681f3Smrg      }
1167ec681f3Smrg      else if (ctx->API != API_OPENGLES2) {
1177ec681f3Smrg         /* PointSizeEnabled is always set in ES2 contexts */
1187ec681f3Smrg         return ctx->VertexProgram.PointSizeEnabled;
1197ec681f3Smrg      }
1207ec681f3Smrg      else {
1217ec681f3Smrg         /* ST_NEW_TESSEVAL_PROGRAM | ST_NEW_GEOMETRY_PROGRAM */
1227ec681f3Smrg         /* We have to check the last bound stage and see if it writes psize */
1237ec681f3Smrg         struct gl_program *last = NULL;
1247ec681f3Smrg         if (ctx->GeometryProgram._Current)
1257ec681f3Smrg            last = ctx->GeometryProgram._Current;
1267ec681f3Smrg         else if (ctx->TessEvalProgram._Current)
1277ec681f3Smrg            last = ctx->TessEvalProgram._Current;
1287ec681f3Smrg         else if (ctx->VertexProgram._Current)
1297ec681f3Smrg            last = ctx->VertexProgram._Current;
1307ec681f3Smrg         if (last)
1317ec681f3Smrg            return !!(last->info.outputs_written &
1327ec681f3Smrg                      BITFIELD64_BIT(VARYING_SLOT_PSIZ));
1337ec681f3Smrg      }
1347ec681f3Smrg   }
1357ec681f3Smrg   return false;
1367ec681f3Smrg}
137b9abf16eSmaya
138b9abf16eSmaya/** clear-alloc a struct-sized object, with casting */
139b9abf16eSmaya#define ST_CALLOC_STRUCT(T)   (struct T *) calloc(1, sizeof(struct T))
140b9abf16eSmaya
141b9abf16eSmaya
142b9abf16eSmaya#ifdef __cplusplus
143b9abf16eSmaya}
144b9abf16eSmaya#endif
145b9abf16eSmaya
146b9abf16eSmaya
147b9abf16eSmaya#endif /* ST_UTIL */
148