NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Texture.hpp
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 #pragma once
29 #include "gui/Image.hpp"
30 #include "base/Hash.hpp"
31 #include "base/Thread.hpp"
32 
33 namespace FW
34 {
35 //------------------------------------------------------------------------
36 
37 class Texture
38 {
39 private:
40  struct Data
41  {
42  S32 refCount;
43  Spinlock refCountLock;
44 
45  String id;
46  bool isInHash;
47  Image* image;
48  GLuint glTexture;
49  CUarray cudaArray;
50  Texture* nextMip;
51  };
52 
53 public:
54  Texture (void) : m_data(NULL) {}
55  Texture (const Texture& other) : m_data(NULL) { set(other); }
56  Texture (Image* image, const String& id = ""); // takes ownership of the image
57  ~Texture (void) { if (m_data) unreferData(m_data); }
58 
59  static Texture find (const String& id);
60  static Texture import (const String& fileName);
61 
62  bool exists (void) const { return (m_data && m_data->image && m_data->image->getSize().min() > 0); }
63  String getID (void) const { return (m_data) ? m_data->id : ""; }
64  const Image* getImage (void) const { return (m_data) ? m_data->image : NULL; }
65  Vec2i getSize (void) const { return (exists()) ? getImage()->getSize() : 0; }
66 
67  void clear (void) { if (m_data) unreferData(m_data); m_data = NULL; }
68  void set (const Texture& other);
69 
70  GLuint getGLTexture (const ImageFormat::ID desiredFormat = ImageFormat::ID_Max, bool generateMipmaps = true) const;
71  CUarray getCudaArray (const ImageFormat::ID desiredFormat = ImageFormat::ID_Max) const;
72  Texture getMipLevel (int level) const;
73 
74  Texture& operator= (const Texture& other) { set(other); return *this; }
75  bool operator== (const Texture& other) const { return (m_data == other.m_data); }
76  bool operator!= (const Texture& other) const { return (m_data != other.m_data); }
77 
78 private:
79  static Data* findData (const String& id);
80  static Data* createData (const String& id);
81  static void referData (Data* data);
82  static void unreferData (Data* data);
83 
84 private:
85  static Hash<String, Data*>* s_hash;
86 
87  Data* m_data;
88 };
89 
90 //------------------------------------------------------------------------
91 }
static Texture find(const String &id)
Definition: Texture.cpp:47
#define NULL
Definition: Defs.hpp:39
Vec2i getSize(void) const
Definition: Texture.hpp:65
const Image * getImage(void) const
Definition: Texture.hpp:64
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
GLuint getGLTexture(const ImageFormat::ID desiredFormat=ImageFormat::ID_Max, bool generateMipmaps=true) const
Definition: Texture.cpp:82
Texture getMipLevel(int level) const
Definition: Texture.cpp:104
void clear(void)
Definition: Texture.hpp:67
Texture(void)
Definition: Texture.hpp:54
signed int S32
Definition: Defs.hpp:88
bool exists(void) const
Definition: Texture.hpp:62
bool operator==(const Texture &other) const
Definition: Texture.hpp:75
bool operator!=(const Texture &other) const
Definition: Texture.hpp:76
const Vec2i & getSize(void) const
Definition: Image.hpp:143
void set(const Texture &other)
Definition: Texture.cpp:70
CUarray getCudaArray(const ImageFormat::ID desiredFormat=ImageFormat::ID_Max) const
Definition: Texture.cpp:93
Texture & operator=(const Texture &other)
Definition: Texture.hpp:74
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
String getID(void) const
Definition: Texture.hpp:63
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void * image
Definition: DLLImports.inl:60
Texture(const Texture &other)
Definition: Texture.hpp:55
~Texture(void)
Definition: Texture.hpp:57