blob: eb7ed51796fec43290e67e09bf49baf8fe8d516c [file] [log] [blame]
Nicolas Capensee16f0d2015-07-16 17:40:10 -04001// SwiftShader Software Renderer
2//
3// Copyright(c) 2005-2011 TransGaming Inc.
4//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12#ifndef Debug_hpp
13#define Debug_hpp
14
15#ifndef WIN32_LEAN_AND_MEAN
16 #define WIN32_LEAN_AND_MEAN
17#endif
18#include <windows.h>
19#include <d3d9.h>
20#include <stdio.h>
21#include <guiddef.h>
22#include <assert.h>
23
24#define APPEND(x, y) x ## y
25#define MACRO_APPEND(x, y) APPEND(x, y)
26#define UNIQUE_IDENTIFIER(prefix) MACRO_APPEND(prefix, __COUNTER__)
27
28struct Trace
29{
30 Trace(const char *format, ...)
31 {
32 if(false)
33 {
34 FILE *file = fopen("debug.txt", "a");
35
36 if(file)
37 {
38 for(int i = 0; i < indent; i++) fprintf(file, " ");
39
40 va_list vararg;
41 va_start(vararg, format);
42 vfprintf(file, format, vararg);
43 va_end(vararg);
44
45 fclose(file);
46 }
47 }
48
49 indent++;
50 }
51
52 ~Trace()
53 {
54 indent--;
55 }
56
57 static int indent;
58};
59
60#ifndef NDEBUG
61 #define TRACE(format, ...) Trace UNIQUE_IDENTIFIER(_tracer_)("[0x%0.8X]%s("format")\n", this, __FUNCTION__, __VA_ARGS__)
62 #define GTRACE(format, ...) Trace("%s("format")\n", __FUNCTION__, __VA_ARGS__)
63#else
64 #define TRACE(...) ((void)0)
65 #define GTRACE(...) ((void)0)
66#endif
67
68#ifndef NDEBUG
69 #define ASSERT(expression) {if(!(expression)) Trace("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); assert(expression);}
70#else
71 #define ASSERT assert
72#endif
73
74#ifndef NDEBUG
75 #define UNIMPLEMENTED() {Trace("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__); ASSERT(false);}
76#else
77 #define UNIMPLEMENTED() ((void)0)
78#endif
79
80#ifndef NDEBUG
81 #define NOINTERFACE(iid) _NOINTERFACE(__FUNCTION__, iid)
82
83 inline long _NOINTERFACE(const char *function, const IID &iid)
84 {
85 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);
86
87 return E_NOINTERFACE;
88 }
89#else
90 #define NOINTERFACE(iid) E_NOINTERFACE
91#endif
92
93#ifndef NDEBUG
94 inline long INVALIDCALL()
95 {
96 Trace("\t! D3DERR_INVALIDCALL\n");
97
98 return D3DERR_INVALIDCALL;
99 }
100#else
101 #define INVALIDCALL() D3DERR_INVALIDCALL
102#endif
103
104#ifndef NDEBUG
105 inline long OUTOFMEMORY()
106 {
107 Trace("\t! E_OUTOFMEMORY\n");
108
109 return E_OUTOFMEMORY;
110 }
111#else
112 #define OUTOFMEMORY() E_OUTOFMEMORY
113#endif
114
115#ifndef NDEBUG
116 inline long OUTOFVIDEOMEMORY()
117 {
118 Trace("\t! D3DERR_OUTOFVIDEOMEMORY\n");
119
120 return D3DERR_OUTOFVIDEOMEMORY;
121 }
122#else
123 #define OUTOFVIDEOMEMORY() D3DERR_OUTOFVIDEOMEMORY
124#endif
125
126#ifndef NDEBUG
127 inline long NOTAVAILABLE()
128 {
129 Trace("\t! D3DERR_NOTAVAILABLE\n");
130
131 return D3DERR_NOTAVAILABLE;
132 }
133#else
134 #define NOTAVAILABLE() D3DERR_NOTAVAILABLE
135#endif
136
137#ifndef NDEBUG
138 inline long NOTFOUND()
139 {
140 Trace("\t! D3DERR_NOTFOUND\n");
141
142 return D3DERR_NOTFOUND;
143 }
144#else
145 #define NOTFOUND() D3DERR_NOTFOUND
146#endif
147
148#ifndef NDEBUG
149 inline long MOREDATA()
150 {
151 Trace("\t! D3DERR_MOREDATA\n");
152
153 return D3DERR_MOREDATA;
154 }
155#else
156 #define MOREDATA() D3DERR_MOREDATA
157#endif
158
159#ifndef NDEBUG
160 inline long FAIL()
161 {
162 Trace("\t! E_FAIL\n");
163
164 return E_FAIL;
165 }
166#else
167 #define FAIL() E_FAIL
168#endif
169
170#endif // Debug_hpp