blob: b36638fbb5a1276c8dee5dd1b9d92a4959cb5293 [file] [log] [blame]
José Fonseca8fbdd3a2010-11-23 20:55:07 +00001##########################################################################
2#
José Fonseca23691292011-04-22 10:40:25 +01003# Copyright 2011 Jose Fonseca
José Fonseca99771fc2010-11-25 20:32:59 +00004# Copyright 2008-2010 VMware, Inc.
José Fonseca8fbdd3a2010-11-23 20:55:07 +00005# All Rights Reserved.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23# THE SOFTWARE.
24#
25##########################################################################/
26
27
José Fonseca4a826ed2010-11-30 16:58:22 +000028"""GLX tracing generator."""
29
30
José Fonseca452d3252012-04-14 15:55:40 +010031from gltrace import GlTracer
José Fonseca81301932012-11-11 00:10:20 +000032from specs.stdapi import Module, API
José Fonsecabd86a222011-09-27 09:21:38 +010033from specs.glapi import glapi
34from specs.glxapi import glxapi
José Fonseca8fbdd3a2010-11-23 20:55:07 +000035
36
José Fonseca669b1222011-02-20 09:05:10 +000037class GlxTracer(GlTracer):
José Fonseca8fbdd3a2010-11-23 20:55:07 +000038
José Fonseca54f304a2012-01-14 19:33:08 +000039 def isFunctionPublic(self, function):
José Fonseca23691292011-04-22 10:40:25 +010040 # The symbols visible in libGL.so can vary, so expose them all
41 return True
42
José Fonseca1b6c8752012-04-15 14:33:00 +010043 getProcAddressFunctionNames = [
44 "glXGetProcAddress",
45 "glXGetProcAddressARB",
46 ]
José Fonseca8fbdd3a2010-11-23 20:55:07 +000047
José Fonsecaa67ed0a2012-08-02 20:00:28 +010048 createContextFunctionNames = [
49 'glXCreateContext',
50 'glXCreateContextAttribsARB',
José Fonseca5814c1f2012-08-02 20:05:19 +010051 'glXCreateContextWithConfigSGIX',
José Fonsecaa67ed0a2012-08-02 20:00:28 +010052 'glXCreateNewContext',
53 ]
54
55 destroyContextFunctionNames = [
56 'glXDestroyContext',
57 ]
58
59 makeCurrentFunctionNames = [
60 'glXMakeCurrent',
61 'glXMakeContextCurrent',
62 'glXMakeCurrentReadSGI',
63 ]
64
Imre Deak4f767732012-05-23 10:56:57 +030065 def traceFunctionImplBody(self, function):
José Fonsecaa67ed0a2012-08-02 20:00:28 +010066 if function.name in self.destroyContextFunctionNames:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010067 print(' gltrace::releaseContext((uintptr_t)ctx);')
José Fonseca5c298db2012-07-11 12:14:31 +010068
Imre Deak4f767732012-05-23 10:56:57 +030069 GlTracer.traceFunctionImplBody(self, function)
70
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -080071 if function.name == 'glXCreateContextAttribsARB':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010072 print(' if (_result != NULL)')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -080073 print(' gltrace::createContext((uintptr_t)_result, (uintptr_t)share_context);')
74 elif function.name == 'glXCreateContextWithConfigSGIX':
75 print(' if (_result != NULL)')
76 print(' gltrace::createContext((uintptr_t)_result, (uintptr_t)share_list);')
77 elif function.name in self.createContextFunctionNames:
78 print(' if (_result != NULL)')
79 print(' gltrace::createContext((uintptr_t)_result, (uintptr_t)shareList);')
Imre Deak4f767732012-05-23 10:56:57 +030080
José Fonsecaa67ed0a2012-08-02 20:00:28 +010081 if function.name in self.makeCurrentFunctionNames:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010082 print(' if (_result) {')
83 print(' if (ctx != NULL)')
84 print(' gltrace::setContext((uintptr_t)ctx);')
85 print(' else')
86 print(' gltrace::clearContext();')
87 print(' }')
Imre Deak4f767732012-05-23 10:56:57 +030088
José Fonseca151c3702013-05-10 08:28:15 +010089 if function.name == 'glXBindTexImageEXT':
90 # FIXME: glXBindTexImageEXT gets called frequently, so we should
91 # avoid recording the same data over and over again somehow, e.g.:
92 # - get the pixels before and after glXBindTexImageEXT, and only
93 # emit emitFakeTexture2D when it changes
94 # - keep a global hash of the pixels
95 # FIXME: Handle mipmaps
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010096 print(r'''
José Fonseca151c3702013-05-10 08:28:15 +010097 unsigned glx_target = 0;
98 _glXQueryDrawable(display, drawable, GLX_TEXTURE_TARGET_EXT, &glx_target);
99 GLenum target;
100 switch (glx_target) {
101 // FIXME
102 //case GLX_TEXTURE_1D_EXT:
103 // target = GL_TEXTURE_1D;
104 // break;
105 case GLX_TEXTURE_2D_EXT:
106 target = GL_TEXTURE_2D;
107 break;
108 case GLX_TEXTURE_RECTANGLE_EXT:
109 target = GL_TEXTURE_RECTANGLE;
110 break;
111 default:
112 os::log("apitrace: warning: %s: unsupported GLX_TEXTURE_TARGET_EXT 0x%u\n", __FUNCTION__, glx_target);
113 target = GL_NONE;
114 break;
115 }
116 GLint level = 0;
117 GLint internalformat = GL_NONE;
118 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
José Fonsecaa24fe0c2013-05-10 13:47:04 +0100119 // XXX: GL_TEXTURE_INTERNAL_FORMAT cannot be trusted on NVIDIA
120 // -- it sometimes returns GL_BGRA, even though GL_BGR/BGRA is
121 // not a valid internal format.
122 switch (internalformat) {
123 case GL_BGR:
124 internalformat = GL_RGB;
125 break;
126 case GL_BGRA:
127 internalformat = GL_RGBA;
128 break;
129 }
José Fonseca151c3702013-05-10 08:28:15 +0100130 GLint width = 0;
131 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
132 GLint height = 0;
133 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
134 GLint border = 0;
José Fonseca04d9da12013-05-10 09:43:43 +0100135 // XXX: We always use GL_RGBA format to read the pixels because:
136 // - some implementations (Mesa) seem to return bogus results
137 // for GLX_TEXTURE_FORMAT_EXT
138 // - hardware usually stores GL_RGB with 32bpp, so it should be
139 // faster to read/write
140 // - it is more robust against GL_(UN)PACK_ALIGNMENT state
141 // changes
142 // The drawback is that traces will be slightly bigger.
143 GLenum format = GL_RGBA;
José Fonseca151c3702013-05-10 08:28:15 +0100144 GLenum type = GL_UNSIGNED_BYTE;
José Fonseca04d9da12013-05-10 09:43:43 +0100145 if (target && internalformat && height && width) {
146 // FIXME: This assumes (UN)PACK state (in particular
147 // GL_(UN)PACK_ROW_LENGTH) is set to its defaults. We
148 // really should temporarily reset the state here (and emit
149 // according fake calls) to cope when its not. At very
150 // least we need a heads up warning that this will cause
151 // problems.
José Fonsecad87aabd2013-05-10 09:35:35 +0100152 GLint alignment = 4;
José Fonseca04d9da12013-05-10 09:43:43 +0100153 GLint row_stride = _align(width * 4, alignment);
Jose Fonseca9dac3c52017-07-30 13:30:35 +0100154 std::unique_ptr<GLbyte[]> data(new GLbyte[height * row_stride]);
155 GLvoid *pixels = data.get();
José Fonseca151c3702013-05-10 08:28:15 +0100156 _glGetTexImage(target, level, format, type, pixels);
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100157 ''')
José Fonseca151c3702013-05-10 08:28:15 +0100158 self.emitFakeTexture2D()
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100159 print(r'''
José Fonseca151c3702013-05-10 08:28:15 +0100160 }
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100161 ''')
José Fonseca151c3702013-05-10 08:28:15 +0100162
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000163
164if __name__ == '__main__':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100165 print()
166 print('#include <stdlib.h>')
167 print('#include <string.h>')
168 print()
169 print('#include <memory>')
170 print()
171 print('#include "trace_writer_local.hpp"')
172 print()
173 print('// To validate our prototypes')
174 print('#define GL_GLEXT_PROTOTYPES')
175 print('#define GLX_GLXEXT_PROTOTYPES')
176 print()
177 print('#include "glproc.hpp"')
178 print('#include "glsize.hpp"')
179 print()
José Fonseca14c21bc2011-02-20 23:32:22 +0000180
José Fonseca81301932012-11-11 00:10:20 +0000181 module = Module()
182 module.mergeModule(glxapi)
183 module.mergeModule(glapi)
José Fonseca68ec4122011-02-20 11:25:25 +0000184 api = API()
José Fonseca81301932012-11-11 00:10:20 +0000185 api.addModule(module)
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000186 tracer = GlxTracer()
José Fonseca1b6c8752012-04-15 14:33:00 +0100187 tracer.traceApi(api)