NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Texture.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 "3d/Texture.hpp"
29 #include "gpu/CudaModule.hpp"
30 
31 using namespace FW;
32 
33 //------------------------------------------------------------------------
34 
35 Hash<String, Texture::Data*>* Texture::s_hash = NULL;
36 
37 //------------------------------------------------------------------------
38 
40 {
41  m_data = createData(id);
42  m_data->image = image;
43 }
44 
45 //------------------------------------------------------------------------
46 
48 {
49  Texture tex;
50  tex.m_data = findData(id);
51  return tex;
52 }
53 
54 //------------------------------------------------------------------------
55 
56 Texture Texture::import(const String& fileName)
57 {
58  Texture tex;
59  tex.m_data = findData(fileName);
60  if (!tex.m_data)
61  {
62  tex.m_data = createData(fileName);
63  tex.m_data->image = importImage(fileName);
64  }
65  return tex;
66 }
67 
68 //------------------------------------------------------------------------
69 
70 void Texture::set(const Texture& other)
71 {
72  Data* old = m_data;
73  m_data = other.m_data;
74  if (m_data)
75  referData(m_data);
76  if (old)
77  unreferData(old);
78 }
79 
80 //------------------------------------------------------------------------
81 
82 GLuint Texture::getGLTexture(ImageFormat::ID desiredFormat, bool generateMipmaps) const
83 {
84  if (!m_data)
85  return 0;
86  if (m_data->glTexture == 0 && m_data->image)
87  m_data->glTexture = m_data->image->createGLTexture(desiredFormat, generateMipmaps);
88  return m_data->glTexture;
89 }
90 
91 //------------------------------------------------------------------------
92 
93 CUarray Texture::getCudaArray(const ImageFormat::ID desiredFormat) const
94 {
95  if (!m_data)
96  return NULL;
97  if (!m_data->cudaArray && m_data->image)
98  m_data->cudaArray = m_data->image->createCudaArray(desiredFormat);
99  return m_data->cudaArray;
100 }
101 
102 //------------------------------------------------------------------------
103 
105 {
106  const Texture* curr = this;
107  for (int i = 0; i < level && curr->exists(); i++)
108  {
109  if (!curr->m_data->nextMip)
110  {
111  Image* scaled = curr->m_data->image->downscale2x();
112  if (!scaled)
113  break;
114  curr->m_data->nextMip = new Texture(scaled);
115  }
116  curr = curr->m_data->nextMip;
117  }
118  return *curr;
119 }
120 
121 //------------------------------------------------------------------------
122 
123 Texture::Data* Texture::findData(const String& id)
124 {
125  Data** found = (s_hash) ? s_hash->search(id) : NULL;
126  if (!found)
127  return NULL;
128 
129  referData(*found);
130  return *found;
131 }
132 
133 //------------------------------------------------------------------------
134 
135 Texture::Data* Texture::createData(const String& id)
136 {
137  Data* data = new Data;
138  data->id = id;
139  data->refCount = 1;
140  data->isInHash = false;
141  data->image = NULL;
142  data->glTexture = 0;
143  data->cudaArray = NULL;
144  data->nextMip = NULL;
145 
146  // Update hash.
147 
148  if (id.getLength())
149  {
150  if (!s_hash)
151  s_hash = new Hash<String, Data*>;
152 
153  Data** old = s_hash->search(id);
154  if (old)
155  {
156  s_hash->remove(id);
157  (*old)->isInHash = false;
158  }
159 
160  s_hash->add(id, data);
161  data->isInHash = true;
162  }
163  return data;
164 }
165 
166 //------------------------------------------------------------------------
167 
168 void Texture::referData(Data* data)
169 {
170  FW_ASSERT(data);
171  data->refCountLock.enter();
172  data->refCount++;
173  data->refCountLock.leave();
174 }
175 
176 //------------------------------------------------------------------------
177 
178 void Texture::unreferData(Data* data)
179 {
180  FW_ASSERT(data);
181 
182  // Decrease refcount.
183 
184  data->refCountLock.enter();
185  int refCount = --data->refCount;
186  data->refCountLock.leave();
187 
188  if (refCount != 0)
189  return;
190 
191  // Remove from hash.
192 
193  if (data->isInHash)
194  {
195  FW_ASSERT(s_hash);
196  s_hash->remove(data->id);
197  if (!s_hash->getSize())
198  {
199  delete s_hash;
200  s_hash = NULL;
201  }
202  }
203 
204  // Delete data.
205 
206  if (data->glTexture != 0)
207  glDeleteTextures(1, &data->glTexture);
208 
209  if (data->cudaArray)
210  CudaModule::checkError("cuArrayDestroy", cuArrayDestroy(data->cudaArray));
211 
212  delete data->image;
213  delete data->nextMip;
214  delete data;
215 }
216 
217 //------------------------------------------------------------------------
static Texture find(const String &id)
Definition: Texture.cpp:47
#define NULL
Definition: Defs.hpp:39
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 * data
Definition: DLLImports.inl:319
Image * importImage(const String &fileName)
Definition: Image.cpp:1051
GLuint getGLTexture(const ImageFormat::ID desiredFormat=ImageFormat::ID_Max, bool generateMipmaps=true) const
Definition: Texture.cpp:82
static void checkError(const char *funcName, CUresult res)
Definition: CudaModule.cpp:487
Texture getMipLevel(int level) const
Definition: Texture.cpp:104
static Texture import(const String &fileName)
Definition: Texture.cpp:56
Texture(void)
Definition: Texture.hpp:54
#define FW_ASSERT(X)
Definition: Defs.hpp:67
bool exists(void) const
Definition: Texture.hpp:62
void set(const Texture &other)
Definition: Texture.cpp:70
CUarray getCudaArray(const ImageFormat::ID desiredFormat=ImageFormat::ID_Max) const
Definition: Texture.cpp:93
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
Definition: DLLImports.inl:333
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void * image
Definition: DLLImports.inl:60