José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 1 | ########################################################################## |
| 2 | # |
José Fonseca | 2369129 | 2011-04-22 10:40:25 +0100 | [diff] [blame] | 3 | # Copyright 2011 Jose Fonseca |
José Fonseca | 99771fc | 2010-11-25 20:32:59 +0000 | [diff] [blame] | 4 | # Copyright 2008-2010 VMware, Inc. |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 5 | # 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é Fonseca | 4a826ed | 2010-11-30 16:58:22 +0000 | [diff] [blame] | 28 | """GLX tracing generator.""" |
| 29 | |
| 30 | |
José Fonseca | 452d325 | 2012-04-14 15:55:40 +0100 | [diff] [blame] | 31 | from gltrace import GlTracer |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 32 | from specs.stdapi import Module, API |
José Fonseca | bd86a22 | 2011-09-27 09:21:38 +0100 | [diff] [blame] | 33 | from specs.glapi import glapi |
| 34 | from specs.glxapi import glxapi |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 35 | |
| 36 | |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 37 | class GlxTracer(GlTracer): |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 38 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 39 | def isFunctionPublic(self, function): |
José Fonseca | 2369129 | 2011-04-22 10:40:25 +0100 | [diff] [blame] | 40 | # The symbols visible in libGL.so can vary, so expose them all |
| 41 | return True |
| 42 | |
José Fonseca | 1b6c875 | 2012-04-15 14:33:00 +0100 | [diff] [blame] | 43 | getProcAddressFunctionNames = [ |
| 44 | "glXGetProcAddress", |
| 45 | "glXGetProcAddressARB", |
| 46 | ] |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 47 | |
José Fonseca | a67ed0a | 2012-08-02 20:00:28 +0100 | [diff] [blame] | 48 | createContextFunctionNames = [ |
| 49 | 'glXCreateContext', |
| 50 | 'glXCreateContextAttribsARB', |
José Fonseca | 5814c1f | 2012-08-02 20:05:19 +0100 | [diff] [blame] | 51 | 'glXCreateContextWithConfigSGIX', |
José Fonseca | a67ed0a | 2012-08-02 20:00:28 +0100 | [diff] [blame] | 52 | 'glXCreateNewContext', |
| 53 | ] |
| 54 | |
| 55 | destroyContextFunctionNames = [ |
| 56 | 'glXDestroyContext', |
| 57 | ] |
| 58 | |
| 59 | makeCurrentFunctionNames = [ |
| 60 | 'glXMakeCurrent', |
| 61 | 'glXMakeContextCurrent', |
| 62 | 'glXMakeCurrentReadSGI', |
| 63 | ] |
| 64 | |
Imre Deak | 4f76773 | 2012-05-23 10:56:57 +0300 | [diff] [blame] | 65 | def traceFunctionImplBody(self, function): |
José Fonseca | a67ed0a | 2012-08-02 20:00:28 +0100 | [diff] [blame] | 66 | if function.name in self.destroyContextFunctionNames: |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 67 | print(' gltrace::releaseContext((uintptr_t)ctx);') |
José Fonseca | 5c298db | 2012-07-11 12:14:31 +0100 | [diff] [blame] | 68 | |
Imre Deak | 4f76773 | 2012-05-23 10:56:57 +0300 | [diff] [blame] | 69 | GlTracer.traceFunctionImplBody(self, function) |
| 70 | |
Robert Tarasov | 6ccf5bb | 2020-01-10 12:31:25 -0800 | [diff] [blame] | 71 | if function.name == 'glXCreateContextAttribsARB': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 72 | print(' if (_result != NULL)') |
Robert Tarasov | 6ccf5bb | 2020-01-10 12:31:25 -0800 | [diff] [blame] | 73 | 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 Deak | 4f76773 | 2012-05-23 10:56:57 +0300 | [diff] [blame] | 80 | |
José Fonseca | a67ed0a | 2012-08-02 20:00:28 +0100 | [diff] [blame] | 81 | if function.name in self.makeCurrentFunctionNames: |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 82 | 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 Deak | 4f76773 | 2012-05-23 10:56:57 +0300 | [diff] [blame] | 88 | |
José Fonseca | 151c370 | 2013-05-10 08:28:15 +0100 | [diff] [blame] | 89 | 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ły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 96 | print(r''' |
José Fonseca | 151c370 | 2013-05-10 08:28:15 +0100 | [diff] [blame] | 97 | 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é Fonseca | a24fe0c | 2013-05-10 13:47:04 +0100 | [diff] [blame] | 119 | // 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é Fonseca | 151c370 | 2013-05-10 08:28:15 +0100 | [diff] [blame] | 130 | 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é Fonseca | 04d9da1 | 2013-05-10 09:43:43 +0100 | [diff] [blame] | 135 | // 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é Fonseca | 151c370 | 2013-05-10 08:28:15 +0100 | [diff] [blame] | 144 | GLenum type = GL_UNSIGNED_BYTE; |
José Fonseca | 04d9da1 | 2013-05-10 09:43:43 +0100 | [diff] [blame] | 145 | 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é Fonseca | d87aabd | 2013-05-10 09:35:35 +0100 | [diff] [blame] | 152 | GLint alignment = 4; |
José Fonseca | 04d9da1 | 2013-05-10 09:43:43 +0100 | [diff] [blame] | 153 | GLint row_stride = _align(width * 4, alignment); |
Jose Fonseca | 9dac3c5 | 2017-07-30 13:30:35 +0100 | [diff] [blame] | 154 | std::unique_ptr<GLbyte[]> data(new GLbyte[height * row_stride]); |
| 155 | GLvoid *pixels = data.get(); |
José Fonseca | 151c370 | 2013-05-10 08:28:15 +0100 | [diff] [blame] | 156 | _glGetTexImage(target, level, format, type, pixels); |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 157 | ''') |
José Fonseca | 151c370 | 2013-05-10 08:28:15 +0100 | [diff] [blame] | 158 | self.emitFakeTexture2D() |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 159 | print(r''' |
José Fonseca | 151c370 | 2013-05-10 08:28:15 +0100 | [diff] [blame] | 160 | } |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 161 | ''') |
José Fonseca | 151c370 | 2013-05-10 08:28:15 +0100 | [diff] [blame] | 162 | |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 163 | |
| 164 | if __name__ == '__main__': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 165 | 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é Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 180 | |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 181 | module = Module() |
| 182 | module.mergeModule(glxapi) |
| 183 | module.mergeModule(glapi) |
José Fonseca | 68ec412 | 2011-02-20 11:25:25 +0000 | [diff] [blame] | 184 | api = API() |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 185 | api.addModule(module) |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 186 | tracer = GlxTracer() |
José Fonseca | 1b6c875 | 2012-04-15 14:33:00 +0100 | [diff] [blame] | 187 | tracer.traceApi(api) |