~unity-team/nux/nux.redirected-views

« back to all changes in this revision

Viewing changes to NuxGraphics/GpuDeviceTexture.cpp

  • Committer: Tarmac
  • Author(s): Eleni Maria Stea
  • Date: 2012-10-01 16:50:33 UTC
  • mfrom: (653.2.50 nuxref)
  • Revision ID: tarmac-20121001165033-cbst7tkwdo5k71jj
Resolved conflicts, merged to trunk. Fixes: . Approved by Jay Taoko.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 *
20
20
 */
21
21
 
22
 
 
23
22
#include "GLResource.h"
24
23
#include "GpuDevice.h"
25
24
#include "GLDeviceObjects.h"
40
39
    , BitmapFormat PixelFormat
41
40
    , NUX_FILE_LINE_DECL)
42
41
  {
43
 
    IOpenGLTexture2D *ptr;
44
 
    CreateTexture(Width, Height, Levels, PixelFormat, (IOpenGLTexture2D **) &ptr, NUX_FILE_LINE_PARAM);
45
 
    ObjectPtr<IOpenGLTexture2D> h = ObjectPtr<IOpenGLTexture2D> (ptr);
46
 
    ptr->UnReference();
47
 
    return h;
48
 
  }
49
 
 
50
 
  int GpuDevice::CreateTexture(
51
 
    unsigned int Width
52
 
    , unsigned int Height
53
 
    , unsigned int Levels
54
 
    , BitmapFormat PixelFormat
55
 
    , IOpenGLTexture2D **ppTexture
56
 
    , NUX_FILE_LINE_DECL
57
 
    )
58
 
  {
59
 
    if ((Width <= 0) || (Height <= 0))
 
42
    GpuInfo gpu_info = GetGpuInfo();
 
43
    int msz = gpu_info.GetMaxTextureSize();
 
44
    if(Width <= 0 || Height <= 0 || Width > msz || Height > msz)
60
45
    {
61
 
      *ppTexture = NULL;
62
 
      return 0;
 
46
      return ObjectPtr<IOpenGLTexture2D>();
63
47
    }
64
48
 
65
 
    // From : http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_non_power_of_two.txt
66
 
    //    The "floor" convention has a relatively straightforward way to
67
 
    //        evaluate(with integer math) means to determine how many mipmap
68
 
    //        levels are required for a complete pyramid:
69
 
    //    numLevels = 1 + floor(log2(max(w, h, d)))
70
 
    unsigned int NumTotalMipLevel    = 1 + floorf(Log2(Max(Width, Height)));
71
 
 
72
 
    //    Levels
73
 
    //        [in] Number of levels in the texture. If this is zero, generate all texture sublevels
74
 
    //        down to 1 by 1 pixels for hardware that supports mip-maps textures. Call GetNumMipLevel to see the
75
 
    //        number of levels generated.
 
49
    unsigned int NumTotalMipLevel = 1 + floorf(Log2(Max(Width, Height)));
76
50
    unsigned int NumMipLevel = 0;
77
51
 
78
52
    if (Levels == 0)
79
53
    {
80
54
      NumMipLevel = NumTotalMipLevel;
81
55
    }
82
 
    else if (Levels > NumTotalMipLevel)
 
56
    else if (Levels > (int)NumTotalMipLevel)
83
57
    {
84
58
      NumMipLevel = NumTotalMipLevel;
85
59
    }
88
62
      NumMipLevel = Levels;
89
63
    }
90
64
 
91
 
 
92
 
    //    The "floor" convention can be evaluated incrementally with the
93
 
    //        following recursion:
94
 
    //
95
 
    //    nextLODdim = max(1, currentLODdim >> 1)
96
 
    //
97
 
    //        where currentLODdim is the dimension of a level N and nextLODdim
98
 
    //        is the dimension of level N+1.  The recursion stops when level
99
 
    //        numLevels-1 is reached.
100
 
 
101
 
    *ppTexture = new IOpenGLTexture2D(Width, Height, NumMipLevel, PixelFormat, false, NUX_FILE_LINE_PARAM);
102
 
 
103
 
    return 1;
 
65
    ObjectPtr<IOpenGLTexture2D> result;
 
66
    result.Adopt(new IOpenGLTexture2D(Width, Height, NumMipLevel, PixelFormat, false, NUX_FILE_LINE_PARAM));
 
67
    return result;
104
68
  }
105
69
 
106
70
  ObjectPtr<IOpenGLTexture2D> GpuDevice::CreateTexture2DFromID(int id
110
74
    , BitmapFormat pixel_format
111
75
    , NUX_FILE_LINE_DECL)
112
76
  {
113
 
    IOpenGLTexture2D *ptr;
114
 
    ptr = new IOpenGLTexture2D(width, height, levels, pixel_format, true, NUX_FILE_LINE_PARAM); // ref count = 1;
115
 
    ptr->_OpenGLID = id;
116
 
    ObjectPtr<IOpenGLTexture2D> h = ObjectPtr<IOpenGLTexture2D> (ptr); // ref count = 2
117
 
    ptr->UnReference(); // ref count = 1
118
 
    return h;
 
77
    GpuInfo gpu_info = GetGpuInfo();
 
78
    int msz = gpu_info.GetMaxTextureSize();
 
79
    if(width <= 0 || height <=0 || width > msz || height > msz)
 
80
    {
 
81
      return ObjectPtr<IOpenGLTexture2D>();
 
82
    }
 
83
 
 
84
    ObjectPtr<IOpenGLTexture2D> result;
 
85
    result.Adopt(new IOpenGLTexture2D(width, height, levels, pixel_format, true, NUX_FILE_LINE_PARAM));
 
86
    return result;
119
87
  }
120
88
 
121
89
  ObjectPtr<IOpenGLRectangleTexture> GpuDevice::CreateRectangleTexture(
125
93
    , BitmapFormat PixelFormat
126
94
    , NUX_FILE_LINE_DECL)
127
95
  {
128
 
    IOpenGLRectangleTexture *ptr;
129
 
    CreateRectangleTexture(Width, Height, Levels, PixelFormat, (IOpenGLRectangleTexture **) &ptr, NUX_FILE_LINE_PARAM);
130
 
    ObjectPtr<IOpenGLRectangleTexture> h = ObjectPtr<IOpenGLRectangleTexture> (ptr);
131
 
    ptr->UnReference();
132
 
    return h;
133
 
  }
134
 
 
135
 
  int GpuDevice::CreateRectangleTexture(
136
 
    unsigned int Width
137
 
    , unsigned int Height
138
 
    , unsigned int Levels
139
 
    , BitmapFormat PixelFormat
140
 
    , IOpenGLRectangleTexture **ppTexture
141
 
    , NUX_FILE_LINE_DECL
142
 
    )
143
 
  {
144
 
    if ((Width <= 0) || (Height <= 0))
 
96
    GpuInfo gpu_info = GetGpuInfo();
 
97
    int msz = gpu_info.GetMaxTextureSize();
 
98
    if(Width <= 0 || Height <= 0 || Width > msz || Height > msz)
145
99
    {
146
 
      *ppTexture = NULL;
147
 
      return 0;
 
100
      return ObjectPtr<IOpenGLRectangleTexture>();
148
101
    }
149
102
 
150
 
    // From : http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_non_power_of_two.txt
151
 
    //    The "floor" convention has a relatively straightforward way to
152
 
    //        evaluate(with integer math) means to determine how many mipmap
153
 
    //        levels are required for a complete pyramid:
154
 
    //    numLevels = 1 + floor(log2(max(w, h, d)))
155
 
    unsigned int NumTotalMipLevel    = 1 + floorf(Log2(Max(Width, Height)));
156
 
 
157
 
    //    Levels
158
 
    //        [in] Number of levels in the texture. If this is zero, generate all texture sublevels
159
 
    //        down to 1 by 1 pixels for hardware that supports mip-maps textures. Call GetNumMipLevel to see the
160
 
    //        number of levels generated.
 
103
    unsigned int NumTotalMipLevel = 1 + floorf(Log2(Max(Width, Height)));
161
104
    unsigned int NumMipLevel = 0;
162
105
 
163
106
    if (Levels == 0)
164
107
    {
165
 
      //Rectangle texture texture don't support mipmaps
166
108
      NumMipLevel = 1;
167
109
    }
168
 
    else if (Levels > NumTotalMipLevel)
 
110
    else if (Levels > (int)NumTotalMipLevel)
169
111
    {
170
112
      NumMipLevel = 1;
171
113
    }
174
116
      NumMipLevel = 1;
175
117
    }
176
118
 
177
 
 
178
 
    //    The "floor" convention can be evaluated incrementally with the
179
 
    //        following recursion:
180
 
    //
181
 
    //    nextLODdim = max(1, currentLODdim >> 1)
182
 
    //
183
 
    //        where currentLODdim is the dimension of a level N and nextLODdim
184
 
    //        is the dimension of level N+1.  The recursion stops when level
185
 
    //        numLevels-1 is reached.
186
 
 
187
 
    *ppTexture = new IOpenGLRectangleTexture(Width, Height, NumMipLevel, PixelFormat, false, NUX_FILE_LINE_PARAM);
188
 
 
189
 
 
190
 
    return 1;
 
119
    ObjectPtr<IOpenGLRectangleTexture> result;
 
120
    result.Adopt(new IOpenGLRectangleTexture(Width, Height, NumMipLevel, PixelFormat, false, NUX_FILE_LINE_PARAM));
 
121
    return result;
191
122
  }
192
123
 
193
124
  ObjectPtr<IOpenGLCubeTexture> GpuDevice::CreateCubeTexture(
196
127
    , BitmapFormat PixelFormat
197
128
    , NUX_FILE_LINE_DECL)
198
129
  {
199
 
    IOpenGLCubeTexture *ptr;
200
 
    CreateCubeTexture(EdgeLength, Levels, PixelFormat, (IOpenGLCubeTexture **) &ptr);
201
 
    ObjectPtr<IOpenGLCubeTexture> h = ObjectPtr<IOpenGLCubeTexture> (ptr);
202
 
    ptr->UnReference();
203
 
    return h;
204
 
  }
205
130
 
206
 
  int GpuDevice::CreateCubeTexture(
207
 
    unsigned int EdgeLength
208
 
    , unsigned int Levels
209
 
    , BitmapFormat PixelFormat
210
 
    , IOpenGLCubeTexture **ppCubeTexture
211
 
    , NUX_FILE_LINE_DECL
212
 
    )
213
 
  {
214
 
    unsigned int NumTotalMipLevel    = 1 + floorf(Log2(EdgeLength));
215
 
    //    Levels
216
 
    //        [in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels
217
 
    //        down to 1 by 1 pixels for hardware that supports mipmapped textures. Call GetNumMipLevel to see the
218
 
    //        number of levels generated.
 
131
    unsigned int NumTotalMipLevel = 1 + floorf(Log2(EdgeLength));
219
132
    unsigned int NumMipLevel = 0;
220
133
 
221
134
    if (Levels == 0)
222
135
    {
223
136
      NumMipLevel = NumTotalMipLevel;
224
137
    }
225
 
    else if (Levels > NumTotalMipLevel)
 
138
    else if (Levels > (int)NumTotalMipLevel)
226
139
    {
227
140
      NumMipLevel = NumTotalMipLevel;
228
141
    }
231
144
      NumMipLevel = Levels;
232
145
    }
233
146
 
234
 
    *ppCubeTexture = new IOpenGLCubeTexture(EdgeLength, NumMipLevel, PixelFormat);
235
 
 
236
 
    return 1;
 
147
    ObjectPtr<IOpenGLCubeTexture> result;
 
148
    result.Adopt(new IOpenGLCubeTexture(EdgeLength, NumMipLevel, PixelFormat));
 
149
    return result;
237
150
  }
238
151
 
239
152
  ObjectPtr<IOpenGLVolumeTexture> GpuDevice::CreateVolumeTexture(
244
157
    , BitmapFormat PixelFormat
245
158
    , NUX_FILE_LINE_DECL)
246
159
  {
247
 
    IOpenGLVolumeTexture *ptr;
248
 
    CreateVolumeTexture(Width, Height, Depth, Levels, PixelFormat, (IOpenGLVolumeTexture **) &ptr, NUX_FILE_LINE_PARAM);
249
 
    ObjectPtr<IOpenGLVolumeTexture> h = ObjectPtr<IOpenGLVolumeTexture> (ptr);
250
 
    ptr->UnReference();
251
 
    return h;
252
 
  }
 
160
    GpuInfo gpu_info = GetGpuInfo();
 
161
    int msz = gpu_info.GetMaxTextureSize();
 
162
    if(Width <= 0 || Height <= 0 || Width > msz || Height > msz)
 
163
    {
 
164
      return ObjectPtr<IOpenGLVolumeTexture>();
 
165
    }
253
166
 
254
 
  int GpuDevice::CreateVolumeTexture(
255
 
    unsigned int Width
256
 
    , unsigned int Height
257
 
    , unsigned int Depth
258
 
    , unsigned int Levels
259
 
    , BitmapFormat PixelFormat
260
 
    , IOpenGLVolumeTexture **ppVolumeTexture
261
 
    , NUX_FILE_LINE_DECL
262
 
    )
263
 
  {
264
167
    unsigned int NumTotalMipLevel = 1 + floorf(Log2(Max(Max(Width, Height), Depth)));
265
 
    //    Levels
266
 
    //        [in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels
267
 
    //        down to 1 by 1 pixels for hardware that supports mipmapped textures. Call GetNumMipLevel to see the
268
 
    //        number of levels generated.
269
168
    unsigned int NumMipLevel = 0;
270
169
 
271
170
    if (Levels == 0)
272
171
    {
273
172
      NumMipLevel = NumTotalMipLevel;
274
173
    }
275
 
    else if (Levels > NumTotalMipLevel)
 
174
    else if (Levels > (int)NumTotalMipLevel)
276
175
    {
277
176
      NumMipLevel = NumTotalMipLevel;
278
177
    }
281
180
      NumMipLevel = Levels;
282
181
    }
283
182
 
284
 
    *ppVolumeTexture = new IOpenGLVolumeTexture(Width, Height, Depth, NumMipLevel, PixelFormat);
285
 
 
286
 
    return OGL_OK;
 
183
    ObjectPtr<IOpenGLVolumeTexture> result;
 
184
    result.Adopt(new IOpenGLVolumeTexture(Width, Height, Depth, NumMipLevel, PixelFormat));
 
185
    return result;
287
186
  }
288
187
 
289
188
  ObjectPtr<IOpenGLAnimatedTexture> GpuDevice::CreateAnimatedTexture(
292
191
    , int Depth
293
192
    , BitmapFormat PixelFormat)
294
193
  {
295
 
    IOpenGLAnimatedTexture *ptr;
296
 
    CreateAnimatedTexture(Width, Height, Depth, PixelFormat, (IOpenGLAnimatedTexture **) &ptr);
297
 
    ObjectPtr<IOpenGLAnimatedTexture> h = ObjectPtr<IOpenGLAnimatedTexture> (ptr);
298
 
    ptr->UnReference();
299
 
    return h;
300
 
  }
301
 
 
302
 
  int GpuDevice::CreateAnimatedTexture(unsigned int Width,
303
 
    unsigned int Height,
304
 
    unsigned int Depth,
305
 
    BitmapFormat PixelFormat,
306
 
    IOpenGLAnimatedTexture **ppAnimatedTexture)
307
 
  {
308
 
    *ppAnimatedTexture = new IOpenGLAnimatedTexture(Width, Height, Depth, PixelFormat);
309
 
 
310
 
    return OGL_OK;
 
194
    GpuInfo gpu_info = GetGpuInfo();
 
195
    int msz = gpu_info.GetMaxTextureSize();
 
196
    if(Width <= 0 || Height <= 0 || Width > msz || Height > msz)
 
197
    {
 
198
      return ObjectPtr<IOpenGLAnimatedTexture>();
 
199
    }
 
200
 
 
201
    ObjectPtr<IOpenGLAnimatedTexture> result;
 
202
    result.Adopt(new IOpenGLAnimatedTexture(Width, Height, Depth, PixelFormat));
 
203
    return result;
311
204
  }
312
205
 
313
206
  ObjectPtr<IOpenGLQuery> GpuDevice::CreateQuery(QUERY_TYPE Type)
314
207
  {
315
 
    IOpenGLQuery *ptr;
316
 
    CreateQuery(Type, (IOpenGLQuery **) &ptr);
317
 
    ObjectPtr<IOpenGLQuery> h = ObjectPtr<IOpenGLQuery> (ptr);
318
 
    ptr->UnReference();
319
 
    return h;
320
 
  }
321
 
 
322
 
  int GpuDevice::CreateQuery(QUERY_TYPE Type, IOpenGLQuery **ppQuery)
323
 
  {
324
 
    *ppQuery = new IOpenGLQuery(Type);
325
 
 
326
 
    return OGL_OK;
 
208
    ObjectPtr<IOpenGLQuery> result;
 
209
    result.Adopt(new IOpenGLQuery(Type));
 
210
    return result;
327
211
  }
328
212
}