NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CudaRenderer.cpp
Go to the documentation of this file.
1 #include "cuda/CudaRenderer.hpp"
3 #include "gui/Window.hpp"
4 #include "io/File.hpp"
5 
6 using namespace FW;
7 
8 //------------------------------------------------------------------------
9 
11 : Renderer(Renderer::tBVH, env)
12 {
13  m_compiler.setSourceFile("src/rt/cuda/RendererKernels.cu");
14  m_compiler.addOptions("-use_fast_math");
15  m_compiler.include("src/rt");
16  m_compiler.include("src/framework");
17 
18  m_platform = Platform("GPU");
20 }
21 
22 //------------------------------------------------------------------------
23 
25 {
26 }
27 
28 //------------------------------------------------------------------------
29 
31 {
32  m_params = params;
33  m_tracer.setKernel(params.kernelName);
34 }
35 
36 //------------------------------------------------------------------------
37 
39 {
41 
42  // Clean up the previous batch.
43 
44  if (m_batchRays)
46  m_batchRays = NULL;
47 
48  // Generate new batch.
49 
50  U32 randomSeed = (m_enableRandom) ? m_random.getU32() : 0;
51  switch (m_params.rayType)
52  {
53  case RayType_Primary:
54  if (!m_newBatch)
55  return false;
56  m_newBatch = false;
58  break;
59 
60  case RayType_AO:
62  return false;
64  break;
65 
66  case RayType_Diffuse:
68  return false;
71  break;
72 
73  default:
74  FW_ASSERT(false);
75  return false;
76  }
77 
78  // Sort rays.
79 
82  return true;
83 }
84 
85 //------------------------------------------------------------------------
86 
88 {
90  return m_tracer.traceBatch(*m_batchRays);
91 }
92 
93 //------------------------------------------------------------------------
94 
96 {
98 
99  // Compile kernel.
100 
101  CudaModule* module = m_compiler.compile();
102 
103  // Setup input struct.
104 
105  ReconstructInput& in = *(ReconstructInput*)module->getGlobal("c_ReconstructInput").getMutablePtr();
110  in.isAO = (m_params.rayType == RayType_AO);
119 
120  // Launch.
121 
122  module->getKernel("reconstructKernel").launch(in.numPrimary);
123 }
124 
125 //------------------------------------------------------------------------
126 
128 {
129  // Casting primary rays => no degenerates.
130 
132  return m_primaryRays.getSize();
133 
134  // Compile kernel.
135 
136  CudaModule* module = m_compiler.compile();
137 
138  // Set input and output.
139 
140  CountHitsInput& in = *(CountHitsInput*)module->getGlobal("c_CountHitsInput").getMutablePtr();
143  in.raysPerThread = 32;
144  module->getGlobal("g_CountHitsOutput").clear();
145 
146  // Count primary ray hits.
147 
148  module->getKernel("countHitsKernel").launch(
149  (in.numRays - 1) / in.raysPerThread + 1,
151 
152  int numHits = *(S32*)module->getGlobal("g_CountHitsOutput").getPtr();
153 
154  // numSecondary = secondaryPerPrimary * primaryHits
155 
156  return numHits * m_params.numSamples;
157 }
158 
159 //------------------------------------------------------------------------
Buffer & getTriMaterialColorBuffer(void)
Returns material color buffer.
Definition: Scene.hpp:89
virtual bool nextBatch(void)
virtual void setParams(const Params &params)
S32 getSize() const
Gets size of the buffer (number of rays).
Definition: RayBuffer.hpp:52
#define NULL
Definition: Defs.hpp:39
virtual ~CudaRenderer(void)
Params m_params
Definition: Renderer.hpp:132
void setLeafPreferences(S32 minSize, S32 maxSize)
Sets leaf size preferences (desired number of triangles in one leaf node).
Definition: Platform.hpp:147
Image * m_image
Definition: Renderer.hpp:139
RayBuffer * m_batchRays
Definition: Renderer.hpp:145
S32 m_batchStart
Definition: Renderer.hpp:146
void include(const String &path)
void clear(int value=0)
Definition: Buffer.hpp:100
CudaModule * compile(bool enablePrints=true, bool autoFail=true)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule * module
Definition: DLLImports.inl:60
Buffer & getIDToSlotBuffer()
Gets buffer mapping ids to slots.
Definition: RayBuffer.hpp:179
F32 traceBatch(RayBuffer &rays)
Traces given batch of rays.
bool ao(RayBuffer &orays, RayBuffer &irays, Scene &scene, int numSamples, float maxDist, bool &newBatch, U32 randomSeed=0)
Generates ao rays on the GPU. Batches rays if necessary.
Definition: RayGen.cpp:196
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
Definition: DLLImports.inl:373
CUdeviceptr getCudaPtr(S64 ofs=0)
Definition: Buffer.hpp:108
F32 traceBatch(void)
Definition: Renderer.cpp:364
Scene * m_scene
Definition: Renderer.hpp:137
CudaKernel getKernel(const String &name)
Definition: CudaModule.cpp:80
Structure holding ray statistics. Also provides print to the console. These statistics are used in a ...
Definition: BVH.hpp:45
void setKernel(const String &kernelName)
Sets kernel that should perform the actual traversal of the k-d tree on the gpu.
const U8 * getPtr(S64 ofs=0)
Definition: Buffer.hpp:106
RayBuffer m_primaryRays
Definition: Renderer.hpp:141
Buffer & getSlotToIDBuffer()
Gets buffer slots to ids.
Definition: RayBuffer.hpp:185
Buffer & getTriShadedColorBuffer(void)
Returns shaded color buffer.
Definition: Scene.hpp:96
float F32
Definition: Defs.hpp:89
CUdeviceptr getMutableCudaPtr(S64 ofs=0)
Definition: Buffer.hpp:112
CUdeviceptr triMaterialColor
Buffer & getResultBuffer()
Gets ray result buffer.
Definition: RayBuffer.hpp:173
U8 * getMutablePtr(S64 ofs=0)
Definition: Buffer.hpp:110
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
virtual int getTotalNumRays(void)
unsigned int U32
Definition: Defs.hpp:85
Buffer & getBuffer(void) const
Definition: Image.hpp:148
Buffer & getGlobal(const String &name)
Definition: CudaModule.cpp:117
RayBuffer m_secondaryRays
Definition: Renderer.hpp:142
virtual void updateResult(void)
CudaKernel & launch(void)
Definition: CudaKernel.cpp:179
Class holding various SAH and batch processing parameters.
Definition: Platform.hpp:46
U32 getU32(void)
Definition: Random.hpp:51
RayGen m_raygen
Definition: Renderer.hpp:129
void setNeedClosestHit(bool c)
Sets whether the closet hit is needed.
Definition: RayBuffer.hpp:144
Platform m_platform
Definition: Renderer.hpp:127
void addOptions(const String &options)
bool m_newBatch
Definition: Renderer.hpp:144
void setSourceFile(const String &path)
bool m_enableRandom
Definition: Renderer.hpp:134
void mortonSort()
Performs morton sort.
Definition: RayBuffer.cpp:103
CudaRenderer(Environment *env)
Random m_random
Definition: Renderer.hpp:130