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