1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_GLF_DIAGNOSTIC_H
25 #define PXR_IMAGING_GLF_DIAGNOSTIC_H
26 
27 /// \file glf/diagnostic.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/glf/api.h"
31 #include "pxr/imaging/garch/glApi.h"
32 #include "pxr/base/tf/diagnostic.h"
33 
34 #include <string>
35 #include <cstdint>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 
40 /// Posts diagnostic errors for all GL errors in the current context.
41 /// This macro tags the diagnostic errors with the name of the calling
42 /// function.
43 #define GLF_POST_PENDING_GL_ERRORS() \
44         GlfPostPendingGLErrors(__ARCH_PRETTY_FUNCTION__)
45 
46 /// Posts diagnostic errors for all GL errors in the current context.
47 GLF_API
48 void GlfPostPendingGLErrors(std::string const & where = std::string());
49 
50 /// Registers GlfDefaultDebugOutputMessageCallback as the
51 /// debug message callback for the current GL context.
52 GLF_API
53 void GlfRegisterDefaultDebugOutputMessageCallback();
54 
55 /// A GL debug output message callback method which posts diagnostic
56 /// errors for messages of type DEBUG_TYPE_ERROR and diagnostic warnings
57 /// for other message types.
58 GLF_API
59 void GlfDefaultDebugOutputMessageCallback(
60         GLenum source, GLenum type, GLuint id, GLenum severity,
61         GLsizei length, char const * message, GLvoid const * userParam);
62 
63 /// Returns a string representation of debug output enum values.
64 GLF_API
65 char const * GlfDebugEnumToString(GLenum debugEnum);
66 
67 /// Emit a GlfDebugGroup tracing the current function.
68 #define GLF_GROUP_FUNCTION() \
69     GlfDebugGroup __glf_group_function(__ARCH_PRETTY_FUNCTION__)
70 
71 /// Emit a GlfDebugGroup tracing the current scope with the given string.
72 #define GLF_GROUP_SCOPE(str) \
73     GlfDebugGroup __glf_group_scope(str)
74 
75 /// \class GlfDebugGroup
76 ///
77 /// Represents a GL debug group in Glf
78 ///
79 /// The debug group conditionally adds debug objects to the GL stream
80 /// based on the value to the environment variable GLF_ENABLE_DIAGNOSTIC_TRACE.
81 /// If set to 1 (true) the debug objects will be pushed and popped in
82 /// the command stream as long as the GL implementation and version supports it.
83 ///
84 class GlfDebugGroup {
85     public:
86     /// Pushes a new debug group onto the GL api debug trace stack
87     GLF_API
88     GlfDebugGroup(char const *message);
89 
90     /// Pops a debug group off the GL api debug trace stack
91     GLF_API
92     ~GlfDebugGroup();
93 
94     GlfDebugGroup() = delete;
95     GlfDebugGroup(GlfDebugGroup const&) = delete;
96     GlfDebugGroup& operator =(GlfDebugGroup const&) = delete;
97 };
98 
99 /// Label a buffer object to improve tracing in the debug output.
100 GLF_API
101 void GlfDebugLabelBuffer(GLuint id, char const *label);
102 
103 /// Label a shader object to improve tracing in the debug output.
104 GLF_API
105 void GlfDebugLabelShader(GLuint id, char const *label);
106 
107 /// Label a program object to improve tracing in the debug output.
108 GLF_API
109 void GlfDebugLabelProgram(GLuint id, char const *label);
110 
111 /// \class GlfGLQueryObject
112 ///
113 /// Represents a GL query object in Glf
114 ///
115 class GlfGLQueryObject {
116 public:
117     GLF_API
118     GlfGLQueryObject();
119     GLF_API
120     ~GlfGLQueryObject();
121 
122     /// Begin query for the given \p target
123     /// target has to be one of
124     ///   GL_SAMPLES_PASSED, GL_ANY_SAMPLES_PASSED,
125     ///   GL_ANY_SAMPLES_PASSED_CONSERVATIVE, GL_PRIMITIVES_GENERATED
126     ///   GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
127     ///   GL_TIME_ELAPSED, GL_TIMESTAMP
128     GLF_API
129     void Begin(GLenum target);
130 
131     /// equivalent to Begin(GL_SAMPLES_PASSED).
132     /// The number of samples that pass the depth test for all drawing
133     /// commands within the scope of the query will be returned.
134     GLF_API
135     void BeginSamplesPassed();
136 
137     /// equivalent to Begin(GL_PRIMITIVES_GENERATED).
138     /// The number of primitives sent to the rasterizer by the scoped
139     /// drawing command will be returned.
140     GLF_API
141     void BeginPrimitivesGenerated();
142 
143     /// equivalent to Begin(GL_TIME_ELAPSED).
144     /// The time that it takes for the GPU to execute all of the scoped commands
145     /// will be returned in nanoseconds.
146     GLF_API
147     void BeginTimeElapsed();
148 
149     /// End query
150     GLF_API
151     void End();
152 
153     /// Return the query result (synchronous)
154     /// stalls CPU until the result becomes available.
155     GLF_API
156     int64_t GetResult();
157 
158     /// Return the query result (asynchronous)
159     /// returns 0 if the result hasn't been available.
160     GLF_API
161     int64_t GetResultNoWait();
162 
163     GlfGLQueryObject(GlfGLQueryObject const&) = delete;
164     GlfGLQueryObject& operator =(GlfGLQueryObject const&) = delete;
165 private:
166     GLuint _id;
167     GLenum _target;
168 };
169 
170 
171 PXR_NAMESPACE_CLOSE_SCOPE
172 
173 #endif
174