NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "base/Main.hpp"
29 #include "base/DLLImports.hpp"
30 #include "base/Thread.hpp"
31 #include "gui/Window.hpp"
32 #include "gpu/GLContext.hpp"
33 #include "gpu/CudaModule.hpp"
34 #include "gpu/CudaCompiler.hpp"
35 
36 #include <crtdbg.h>
37 #include <conio.h>
38 
39 using namespace FW;
40 
41 //------------------------------------------------------------------------
42 
43 int FW::argc = 0;
44 char** FW::argv = NULL;
45 int FW::exitCode = 0;
46 
47 static bool s_enableLeakCheck = true;
48 
49 //------------------------------------------------------------------------
50 
51 int main(int argc, char* argv[])
52 {
53  // Store arguments.
54 
55  FW::argc = argc;
56  FW::argv = argv;
57 
58  // Force the main thread to run on a single core.
59 
60  SetThreadAffinityMask(GetCurrentThread(), 1);
61 
62  // Initialize CRTDBG.
63 
64 #if FW_DEBUG
65  int flag = 0;
66  flag |= _CRTDBG_ALLOC_MEM_DF; // use allocation guards
67 // flag |= _CRTDBG_CHECK_ALWAYS_DF; // check whole memory on each alloc
68 // flag |= _CRTDBG_DELAY_FREE_MEM_DF; // keep freed memory and check that it isn't written
69  _CrtSetDbgFlag(flag);
70 
71 // _CrtSetBreakAlloc(64);
72  _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
73  _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
74 #endif
75 
76  // Initialize the application.
77 
79  FW::init();
80  failIfError();
81 
82  // Message loop.
83 
84  while (Window::getNumOpen())
85  {
86  // Wait for a message.
87 
88  MSG msg;
89  if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
90  {
92  GetMessage(&msg, NULL, 0, 0);
93  }
94 
95  // Process the message.
96 
97  TranslateMessage(&msg);
98  DispatchMessage(&msg);
99 
100  // Nesting level was not restored => something fishy is going on.
101 
102  if (incNestingLevel(0) != 0)
103  {
104  fail(
105  "Unhandled access violation detected!\n"
106  "\n"
107  "To get a stack trace, try the following:\n"
108  "- Select \"Debug / Exceptions...\" in Visual Studio.\n"
109  "- Expand the \"Win32 Exceptions\" category.\n"
110  "- Check the \"Thrown\" box for \"Access violation\".\n"
111  "- Re-run the application under debugger (F5).");
112  }
113  }
114 
115  // Clean up.
116 
117  failIfError();
123  profileEnd(false);
124  failIfError();
125 
126  while (hasLogFile())
127  popLogFile();
128 
129  delete Thread::getCurrent();
130 
131  // Dump memory leaks.
132 
133 #if FW_DEBUG
134  if (s_enableLeakCheck && _CrtDumpMemoryLeaks())
135  {
136  FW::printf("Press any key to continue . . . ");
137  _getch();
138  FW::printf("\n");
139  }
140 #endif
141 
142  return exitCode;
143 }
144 
145 //------------------------------------------------------------------------
146 
147 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
148 {
149  FW_UNREF(hInstance);
150  FW_UNREF(hPrevInstance);
151  FW_UNREF(lpCmdLine); // TODO: Parse this to argc/argv.
152  FW_UNREF(nShowCmd);
153  return main(0, NULL);
154 }
155 
156 //------------------------------------------------------------------------
157 
159 {
160  s_enableLeakCheck = false;
161 }
162 
163 //------------------------------------------------------------------------
#define FW_UNREF(X)
Definition: Defs.hpp:78
#define NULL
Definition: Defs.hpp:39
bool hasLogFile(void)
Definition: Defs.cpp:425
static void staticDeinit(void)
Definition: CudaModule.cpp:361
static void staticDeinit(void)
Definition: GLContext.cpp:962
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
Definition: Main.cpp:147
static void staticDeinit(void)
Definition: Window.cpp:535
int argc
Definition: Main.cpp:43
static void staticDeinit(void)
int exitCode
Definition: Main.cpp:45
void popLogFile(void)
Definition: Defs.cpp:406
void deinitDLLImports(void)
Definition: DLLImports.cpp:132
void init(void)
Definition: App.cpp:770
int main(int argc, char *argv[])
Definition: Main.cpp:51
int incNestingLevel(int delta)
Definition: Defs.cpp:369
static int getNumOpen(void)
Definition: Window.hpp:145
char ** argv
Definition: Main.cpp:44
void printf(const char *fmt,...)
Definition: Defs.cpp:225
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat GLfloat z GLuint GLint GLenum GLboolean GLsizei const GLvoid pointer GLuint GLuint const GLchar name GLenum GLsizei GLenum GLsizei GLsizei height GLenum GLuint renderbuffer GLenum GLenum GLint params GLuint GLsizei range GLuint GLsizei const GLubyte GLsizei GLenum const GLvoid coords GLuint GLsizei GLsizei GLsizei const GLubyte GLsizei GLenum const GLvoid coords GLuint GLenum GLsizei const GLvoid pathString GLuint GLenum const GLvoid GLbitfield GLuint GLsizei GLenum GLuint GLfloat emScale GLuint GLuint srcPath GLuint GLuint GLenum const GLfloat transformValues GLuint GLenum GLint value GLuint GLenum GLfloat value GLenum GLint GLuint mask GLuint GLenum GLuint mask GLsizei GLenum const GLvoid GLuint GLenum GLuint GLenum const GLfloat transformValues GLenum func GLenum GLenum GLint const GLfloat coeffs GLuint GLenum coverMode GLsizei GLenum const GLvoid GLuint GLenum GLenum const GLfloat transformValues GLuint GLenum GLint value GLuint GLubyte commands GLuint GLfloat dashArray GLbitfield GLuint GLsizei GLsizei GLfloat metrics GLenum GLenum GLint value GLenum GLenum GLint value GLuint GLuint GLfloat GLfloat y GLuint GLsizei GLsizei numSegments WINAPI
Definition: DLLImports.inl:437
void failIfError(void)
Definition: Defs.cpp:361
void disableLeakCheck(void)
Definition: Main.cpp:158
static Thread * getCurrent(void)
Definition: Thread.cpp:291
void fail(const char *fmt,...)
Definition: Defs.cpp:304
void profileEnd(bool printResults=true)
Definition: Defs.cpp:618
static void realizeAll(void)
Definition: Window.cpp:584