1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef Debug_hpp 16 #define Debug_hpp 17 18 #ifndef WIN32_LEAN_AND_MEAN 19 #define WIN32_LEAN_AND_MEAN 20 #endif 21 #include <windows.h> 22 #include <d3d9.h> 23 #include <stdio.h> 24 #include <guiddef.h> 25 #include <assert.h> 26 27 #define APPEND(x, y) x ## y 28 #define MACRO_APPEND(x, y) APPEND(x, y) 29 #define UNIQUE_IDENTIFIER(prefix) MACRO_APPEND(prefix, __COUNTER__) 30 31 struct Trace 32 { 33 Trace(const char *format, ...) 34 { 35 if(false) 36 { 37 FILE *file = fopen("debug.txt", "a"); 38 39 if(file) 40 { 41 for(int i = 0; i < indent; i++) fprintf(file, " "); 42 43 va_list vararg; 44 va_start(vararg, format); 45 vfprintf(file, format, vararg); 46 va_end(vararg); 47 48 fclose(file); 49 } 50 } 51 52 indent++; 53 } 54 55 ~Trace() 56 { 57 indent--; 58 } 59 60 static int indent; 61 }; 62 63 #ifndef NDEBUG 64 #define TRACE(format, ...) Trace UNIQUE_IDENTIFIER(_tracer_)("[0x%0.8X]%s("format")\n", this, __FUNCTION__, __VA_ARGS__) 65 #define GTRACE(format, ...) Trace("%s("format")\n", __FUNCTION__, __VA_ARGS__) 66 #else 67 #define TRACE(...) ((void)0) 68 #define GTRACE(...) ((void)0) 69 #endif 70 71 #ifndef NDEBUG 72 #define ASSERT(expression) {if(!(expression)) Trace("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); assert(expression);} 73 #else 74 #define ASSERT assert 75 #endif 76 77 #ifndef NDEBUG 78 #define UNIMPLEMENTED() {Trace("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); ASSERT(false);} 79 #else 80 #define UNIMPLEMENTED() ((void)0) 81 #endif 82 83 #ifndef NDEBUG 84 #define NOINTERFACE(iid) _NOINTERFACE(__FUNCTION__, iid) 85 86 inline long _NOINTERFACE(const char *function, const IID &iid) 87 { 88 Trace("\t! No interface {0x%0.8X, 0x%0.4X, 0x%0.4X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X, 0x%0.2X} for %s\n", iid.Data1, iid.Data2, iid.Data3, iid.Data4[0], iid.Data4[1], iid.Data4[2], iid.Data4[3], iid.Data4[4], iid.Data4[5], iid.Data4[6], iid.Data4[7], function); 89 90 return E_NOINTERFACE; 91 } 92 #else 93 #define NOINTERFACE(iid) E_NOINTERFACE 94 #endif 95 96 #ifndef NDEBUG 97 inline long INVALIDCALL() 98 { 99 Trace("\t! D3DERR_INVALIDCALL\n"); 100 101 return D3DERR_INVALIDCALL; 102 } 103 #else 104 #define INVALIDCALL() D3DERR_INVALIDCALL 105 #endif 106 107 #ifndef NDEBUG 108 inline long OUTOFMEMORY() 109 { 110 Trace("\t! E_OUTOFMEMORY\n"); 111 112 return E_OUTOFMEMORY; 113 } 114 #else 115 #define OUTOFMEMORY() E_OUTOFMEMORY 116 #endif 117 118 #ifndef NDEBUG 119 inline long OUTOFVIDEOMEMORY() 120 { 121 Trace("\t! D3DERR_OUTOFVIDEOMEMORY\n"); 122 123 return D3DERR_OUTOFVIDEOMEMORY; 124 } 125 #else 126 #define OUTOFVIDEOMEMORY() D3DERR_OUTOFVIDEOMEMORY 127 #endif 128 129 #ifndef NDEBUG 130 inline long NOTAVAILABLE() 131 { 132 Trace("\t! D3DERR_NOTAVAILABLE\n"); 133 134 return D3DERR_NOTAVAILABLE; 135 } 136 #else 137 #define NOTAVAILABLE() D3DERR_NOTAVAILABLE 138 #endif 139 140 #ifndef NDEBUG 141 inline long NOTFOUND() 142 { 143 Trace("\t! D3DERR_NOTFOUND\n"); 144 145 return D3DERR_NOTFOUND; 146 } 147 #else 148 #define NOTFOUND() D3DERR_NOTFOUND 149 #endif 150 151 #ifndef NDEBUG 152 inline long MOREDATA() 153 { 154 Trace("\t! D3DERR_MOREDATA\n"); 155 156 return D3DERR_MOREDATA; 157 } 158 #else 159 #define MOREDATA() D3DERR_MOREDATA 160 #endif 161 162 #ifndef NDEBUG 163 inline long FAIL() 164 { 165 Trace("\t! E_FAIL\n"); 166 167 return E_FAIL; 168 } 169 #else 170 #define FAIL() E_FAIL 171 #endif 172 173 #endif // Debug_hpp 174