summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/hdr/MipmapBSDF.cu
blob: 6ddac4beddcedf8cf5b83659a485ae0e2a57c751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#if defined (_PLATFORM_USE_EGL)
#include <GLES31/gl31.h>
#include <GLES31/gl2ext.h>
#endif

#include "CUDABSDFMipmap.h"
#include "cuda.h"
#include "cuda_runtime.h"
#include "cuda_gl_interop.h"
#include <iostream>

using namespace nv;
using namespace nv::render;
__host__ void jerror1(cudaError error)
{
   static  int i = 0;
   ++i;
}
#ifdef _DEBUG
#define CHECK_AND_HANDLE_CUDA_ERROR(func)													\
		func;																			\
		{																				\
			cudaError error = cudaGetLastError();												\
			if ( error != cudaSuccess )													\
			{																			\
				printf("%s\n", cudaGetErrorString(error));								\
                jerror1(error);\
				NV_ASSERT( false );														\
			}																			\
		}
#else
#define CHECK_AND_HANDLE_CUDA_ERROR(func)													\
func;
#endif

__device__ inline int wrapMod( int a, int base )
{
	int ret = a % base;
	if (ret < 0 ) ret += base;
	return ret;
}

__device__ inline void getWrappedCoords( int &sX, int &sY, int width, int height )
{
	if (sY < 0) { sX -= width >> 1; sY = -sY; }
	if (sY >= height) { sX += width >> 1; sY = height - sY; }
	sX = wrapMod( sX, width );
	sY = wrapMod( sY, height );
}

__device__ void decodeToFloat( void *inPtr, NVU32 byteOfs, float *outPtr, NVRenderTextureFormats::Enum inFmt, unsigned int numberOfComponent )
{
	outPtr[0] = 0.0f;	outPtr[1] = 0.0f;	outPtr[2] = 0.0f;	outPtr[3] = 0.0f;
	NVU8 *src = reinterpret_cast<NVU8 *>(inPtr);
	//float divisor;		// If we want to support RGBD?
	switch(inFmt)
	{
	case NVRenderTextureFormats::Alpha8:
		outPtr[0] = ((float)src[byteOfs]) / 255.0f;
		break;

	case NVRenderTextureFormats::Luminance8:
	case NVRenderTextureFormats::LuminanceAlpha8:
	case NVRenderTextureFormats::R8:
	case NVRenderTextureFormats::RG8:
	case NVRenderTextureFormats::RGB8:
	case NVRenderTextureFormats::RGBA8:
	case NVRenderTextureFormats::SRGB8:
	case NVRenderTextureFormats::SRGB8A8:
		// NOTE : RGBD Hack here for reference.  Not meant for installation.
		//divisor = (NVRenderTextureFormats::getSizeofFormat(inFmt) == 4) ? ((float)src[byteOfs+3]) / 255.0f : 1.0f;
		for ( NVU32 i = 0; i < numberOfComponent; ++i )
		{
			float val = ((float)src[byteOfs + i]) / 255.0f;
			outPtr[i] = (i < 3) ? powf(val, 0.4545454545f) : val;
			// Assuming RGBA8 actually means RGBD (which is stupid, I know)
			//if ( NVRenderTextureFormats::getSizeofFormat(inFmt) == 4 ) { outPtr[i] /= divisor; }
		}
		//outPtr[3] = divisor;
		break;

	case NVRenderTextureFormats::RGBA32F:
		outPtr[0] = reinterpret_cast<float *>(src+byteOfs)[0];
		outPtr[1] = reinterpret_cast<float *>(src+byteOfs)[1];
		outPtr[2] = reinterpret_cast<float *>(src+byteOfs)[2];
		outPtr[3] = reinterpret_cast<float *>(src+byteOfs)[3];
		break;
	case NVRenderTextureFormats::RGB32F:
		outPtr[0] = reinterpret_cast<float *>(src+byteOfs)[0];
		outPtr[1] = reinterpret_cast<float *>(src+byteOfs)[1];
		outPtr[2] = reinterpret_cast<float *>(src+byteOfs)[2];
		break;

	case NVRenderTextureFormats::RGBA16F:
        /*
		for ( NVU32 i = 0; i < 4; ++i )
		{
			// NOTE : This only works on the assumption that we don't have any denormals, Infs or NaNs.
			// Every pixel in our source image should be "regular"
			NVU16 h = reinterpret_cast<NVU16 *>(src + byteOfs)[i];
			NVU32 sign = (h & 0x8000) << 16;
			NVU32 exponent = (((((h & 0x7c00) >> 10) - 15) + 127) << 23);
			NVU32 mantissa =  ((h & 0x3ff) << 13);
			NVU32 result = sign | exponent | mantissa;
					
			if (h == 0 || h == 0x8000) { result = 0; }	// Special case for zero and negative zero
			memcpy( reinterpret_cast<NVU32 *>(outPtr) + i, &result, 4 );
		}*/

		for ( NVU32 i = 0; i < 2; i++ )
		{
			// NOTE : This only works on the assumption that we don't have any denormals, Infs or NaNs.
			// Every pixel in our source image should be "regular"

			NVU32 h1 = reinterpret_cast<NVU32 *>(src + byteOfs)[i];

			for ( NVU8 j = 0; j < 2; j++ ) 
			{
				NVU16 h = (h1 & (0x0000FFFF << j*16 )) >> j*16;
				NVU32 sign = (h & 0x8000) << 16;
				NVU32 exponent = (((((h & 0x7c00) >> 10) - 15) + 127) << 23);
				NVU32 mantissa =  ((h & 0x3ff) << 13);
				NVU32 result = sign | exponent | mantissa;
					
				if (h == 0 || h == 0x8000) { result = 0; }	// Special case for zero and negative zero
				memcpy( reinterpret_cast<NVU32 *>(outPtr) + i*2 + j, &result, 4 );
			}
		}
		break;

	case NVRenderTextureFormats::R11G11B10:
		// place holder
		NV_ASSERT( false );
		break;

	default:
		outPtr[0] = 0.0f;
		outPtr[1] = 0.0f;
		outPtr[2] = 0.0f;
		outPtr[3] = 0.0f;
		break;
	}
}

void __device__ encodeToPixel( float *inPtr, void *outPtr, NVU32 byteOfs, NVRenderTextureFormats::Enum inFmt, unsigned int noOfComponent )
{
	NVU8 *dest = reinterpret_cast<NVU8 *>(outPtr);
	switch(inFmt)
	{
	case NVRenderTextureFormats::Alpha8:
		dest[byteOfs] = NVU8( inPtr[0] * 255.0f );
		break;

	case NVRenderTextureFormats::Luminance8:
	case NVRenderTextureFormats::LuminanceAlpha8:
	case NVRenderTextureFormats::R8:
	case NVRenderTextureFormats::RG8:
	case NVRenderTextureFormats::RGB8:
	case NVRenderTextureFormats::RGBA8:
	case NVRenderTextureFormats::SRGB8:
	case NVRenderTextureFormats::SRGB8A8:
		for ( NVU32 i = 0; i < noOfComponent; ++i )
		{
			inPtr[i] = (inPtr[i] > 1.0f) ? 1.0f : inPtr[i];
			if (i < 3)
				dest[byteOfs+i] = NVU8( powf( inPtr[i], 2.2f ) * 255.0f);
			else
				dest[byteOfs+i] = NVU8( inPtr[i] * 255.0f );
		}
		break;

	case NVRenderTextureFormats::RGBA32F:
		reinterpret_cast<float *>(dest+byteOfs)[0] = inPtr[0];
		reinterpret_cast<float *>(dest+byteOfs)[1] = inPtr[1];
		reinterpret_cast<float *>(dest+byteOfs)[2] = inPtr[2];
		reinterpret_cast<float *>(dest+byteOfs)[3] = inPtr[3];
		break;
	case NVRenderTextureFormats::RGB32F:
		reinterpret_cast<float *>(dest+byteOfs)[0] = inPtr[0];
		reinterpret_cast<float *>(dest+byteOfs)[1] = inPtr[1];
		reinterpret_cast<float *>(dest+byteOfs)[2] = inPtr[2];
		break;

	case NVRenderTextureFormats::RGBA16F:
		for ( NVU32 i = 0; i < 4; ++i )
		{
			// NOTE : This also has the limitation of not handling  infs, NaNs and denormals, but it should be
			// sufficient for our purposes.
			if (inPtr[i] > 65519.0f) { inPtr[i] = 65519.0f; }
			if (fabs(inPtr[i]) < 6.10352E-5f) { inPtr[i] = 0.0f; }
			NVU32 f = reinterpret_cast<NVU32 *>(inPtr)[i];
			NVU32 sign = (f & 0x80000000) >> 16;
			NVI32 exponent = (f & 0x7f800000) >> 23;
			NVU32 mantissa = (f >> 13) & 0x3ff;
			exponent = exponent - 112;
			if (exponent > 31) { exponent = 31; }
			if (exponent < 0) { exponent = 0; }
			exponent = exponent << 10;
			reinterpret_cast<NVU16 *>(dest + byteOfs)[i] = NVU16(sign | exponent | mantissa);
		}
		break;

	case NVRenderTextureFormats::R11G11B10:
		// place holder
		NV_ASSERT( false );
		break;

	default:
		dest[byteOfs] = 0;
		dest[byteOfs+1] = 0;
		dest[byteOfs+2] = 0;
		dest[byteOfs+3] = 0;
		break;
	}
}

void __global__ Convert3To4Component( cudaTextureObject_t tex, float *d_outBuffer, Q3DStudio::INT32 dpitch, Q3DStudio::INT32 width, Q3DStudio::INT32 height )
{
	float *dest = d_outBuffer;

	int x = threadIdx.x + blockIdx.x * blockDim.x;
	int y = threadIdx.y + blockIdx.y * blockDim.y;
	if ( x >= width || y >= height ) 
		return;
	int inX = x * 3;
	int outX = x * 4;
	dest[outX + y * width * 4] = tex2D<float>(tex, inX, y);
	dest[outX + y * width * 4 + 1] = tex2D<float>(tex, inX + 1, y);
	dest[outX + y * width * 4 + 2] = tex2D<float>(tex, inX + 2, y);
	dest[outX + y * width * 4 + 3] = 255 * 255;
}

void __global__ ConvertData( void* d_InBuffer, NVRenderTextureFormats::Enum inFmt, int inSizeOfFormat, int inNoOfComponent, int inPitch, 
                             void* d_OutBuffer, NVRenderTextureFormats::Enum outFmt, int outSizeOfFormat, int outNoOfComponent, int outPitch, int width, int height )
{

	int x = threadIdx.x + blockIdx.x * blockDim.x;
	int y = threadIdx.y + blockIdx.y * blockDim.y;
	if ( x >= width || y >= height ) 
		return;
	float values[4];

	decodeToFloat( d_InBuffer, (inPitch * y) + (x * inSizeOfFormat), values, inFmt, inNoOfComponent );
	encodeToPixel( values, d_OutBuffer, (outPitch * y) + (x * outSizeOfFormat), outFmt, outSizeOfFormat );
}

void __global__ CreateBsdfMipLevel( cudaTextureObject_t tex, void *d_curBuffer, void *d_prevBuffer, 	Q3DStudio::INT32 pitch, Q3DStudio::INT32 width, Q3DStudio::INT32 height, 
										nv::render::NVRenderTextureFormats::Enum inFormat, unsigned int sizeOfFormat )
{
	float accumVal[4];
	//unsigned int sizeofFormat = getSizeofFormat(inFormat);
	//__shared__ float dataBlock[ ]; //(32+4) * (32+4) * 12 
	int x = threadIdx.x + blockIdx.x * blockDim.x;
	int y = threadIdx.y + blockIdx.y * blockDim.y;

	if ( x >= (width > 2 ? width >> 1 : 1) || y >= (height > 2 ? height >> 1 : 1)) return;

	accumVal[0] = 0;	accumVal[1] = 0;	accumVal[2] = 0;	accumVal[3] = 0;

	for ( int sy = -2; sy <= 2; ++sy )
	{
		for ( int sx = -2; sx <= 2; ++sx )
		{
			int sampleX = sx + (x << 1);
			int sampleY = sy + (y << 1);
			//getWrappedCoords(sampleX, sampleY, width, height);
			// Cauchy filter (this is simply because it's the easiest to evaluate, and requires no complex
			// functions).
			float filterPdf = 1.f / ( 1.f + float(sx*sx + sy*sy)*2.f );
			// With FP HDR formats, we're not worried about intensity loss so much as unnecessary energy gain, 
			// whereas with LDR formats, the fear with a continuous normalization factor is that we'd lose
			// intensity and saturation as well.
			filterPdf /= sizeOfFormat >= 8 ? 4.71238898f : 4.5403446f;
			//filterPdf /= 4.5403446f;		// Discrete normalization factor
			//filterPdf /= 4.71238898f;		// Continuous normalization factor
			//float curPix[4];
			sampleX = sampleX*4;
			getWrappedCoords(sampleX, sampleY, width*4, height);
			accumVal[0] += filterPdf * tex2D<float>(tex, sampleX, sampleY);
			accumVal[1] += filterPdf * tex2D<float>(tex, sampleX + 1, sampleY);
			accumVal[2] += filterPdf * tex2D<float>(tex, sampleX + 2, sampleY);
			accumVal[3] += filterPdf * tex2D<float>(tex, sampleX + 3, sampleY);
		}
		}

	encodeToPixel(accumVal, d_curBuffer, y * pitch + x * sizeOfFormat, inFormat, sizeOfFormat);
}

struct SMipTextureData
{
	void* data;
	unsigned int dataSize;
	unsigned int mipLevel;
	unsigned int width;
	unsigned int height;
	NVRenderTextureFormats::Enum format;	
};

__host__  void CUDABSDFMipMap::Build( void* inTextureData, int inTextureDataSize, NVRenderBackend::NVRenderBackendTextureObject inTextureHandle, NVRenderTextureFormats::Enum inFormat  )
{
	m_TextureHandle = inTextureHandle;
	m_InternalFormat = inFormat;
	m_SizeOfInternalFormat = NVRenderTextureFormats::getSizeofFormat( m_InternalFormat );
	m_InternalNoOfComponent = NVRenderTextureFormats::getNumberOfComponent( m_InternalFormat );

	m_Texture2D.SetTextureData(  NVDataRef<NVU8>( (NVU8*)inTextureData, inTextureDataSize )
								, 0
								, m_Width
								, m_Height
								, inFormat
								, m_DestinationFormat );

	size_t pitch;
	float* d_inTextureData;

	cudaMallocPitch(&d_inTextureData, &pitch, m_Width * m_SizeOfInternalFormat, m_Height); CHECK_AND_HANDLE_CUDA_ERROR();
	CHECK_AND_HANDLE_CUDA_ERROR( cudaMemcpy2D( d_inTextureData, pitch, inTextureData, m_Width * m_SizeOfInternalFormat, m_Width * m_SizeOfInternalFormat, m_Height, cudaMemcpyHostToDevice ) );
	{
		dim3 blockDim(16, 16, 1);	
		dim3 gridDim(ceil(m_Width / 16.0f), ceil(m_Height / 16.0f) ,1 );

		//std::cerr << "if= " << m_InternalFormat << " sizeOut= " << m_SizeOfInternalFormat << " numOfIntComp" << m_InternalNoOfComponent << " pitch= " << pitch << " destFormat= " << m_DestinationFormat << " sizeFormat= " << m_SizeOfFormat << " numOfComp= " << m_NoOfComponent << " Pitch0=" << m_Pitches[0] << std::endl;
		//NVLogWarn("cuda", "%i %i %i %i %i %i %i %i\n",(int)m_InternalFormat ,m_SizeOfInternalFormat ,m_InternalNoOfComponent , pitch, (int)m_DestinationFormat, m_SizeOfFormat, m_NoOfComponent ,m_Pitches[0]);
        	ConvertData<<<gridDim, blockDim>>>( d_inTextureData, m_InternalFormat, m_SizeOfInternalFormat, m_InternalNoOfComponent, pitch,
                        md_MipMapsData[0], m_DestinationFormat, m_SizeOfFormat, m_NoOfComponent, m_Pitches[0], m_Width, m_Height );
	}
	cudaFree(d_inTextureData);

	int curWidth  = m_Width;
	int curHeight = m_Height;

	cudaTextureObject_t* tex;
	tex = new cudaTextureObject_t[m_MaxMipMapLevel];
	for ( int idx = 1; idx <= m_MaxMipMapLevel; ++idx )
	{
		tex[idx-1] = -1;
		dim3 blockDim(16, 16, 1);	
		dim3 gridDim(ceil(curWidth / 32.0f), ceil(curHeight / 32.0f) ,1 );

		cudaResourceDesc resDesc;
		memset(&resDesc, 0, sizeof(resDesc));
		resDesc.res.pitch2D.desc.f = cudaChannelFormatKindFloat;
		resDesc.res.pitch2D.desc.x = m_SizeOfFormat / m_NoOfComponent * 8; // bits per channel
		resDesc.resType = cudaResourceTypePitch2D;
		resDesc.res.pitch2D.devPtr = (char*)(md_MipMapsData[idx-1]);
		resDesc.res.pitch2D.height = curHeight;
		resDesc.res.pitch2D.width  = curWidth * m_NoOfComponent;
		resDesc.res.pitch2D.pitchInBytes  = m_Pitches[idx-1];// aligned to texturePitchAlignment

		cudaTextureDesc texDesc;
		memset(&texDesc, 0, sizeof(texDesc));
		texDesc.addressMode[0] = cudaAddressModeWrap;
		texDesc.addressMode[1] = cudaAddressModeWrap;
		texDesc.readMode = cudaReadModeElementType;
		//texDesc.normalizedCoords = 1;

		
		CHECK_AND_HANDLE_CUDA_ERROR( cudaCreateTextureObject( &tex[idx-1], &resDesc, &texDesc, NULL ) );
		CreateBsdfMipLevel<<<gridDim, blockDim>>>( tex[idx-1], (reinterpret_cast<NVU8 *>(md_MipMapsData[idx])), (reinterpret_cast<NVU8 *>(md_MipMapsData[idx-1])), m_Pitches[idx], curWidth, curHeight, m_DestinationFormat, m_SizeOfFormat );

		curWidth  = curWidth > 2 ? curWidth >> 1 : 1;
		curHeight = curHeight > 2 ? curHeight >> 1 : 1;
	}

	CHECK_AND_HANDLE_CUDA_ERROR( cudaDeviceSynchronize(); )
	BindTexture();
	TransferTexture();
	for (int idx = 0; idx < m_MaxMipMapLevel;++idx )
		cudaDestroyTextureObject(tex[idx]);
//	CHECK_AND_HANDLE_CUDA_ERROR( cudaDeviceReset(); )
	CHECK_AND_HANDLE_CUDA_ERROR( cudaDeviceSynchronize(); )

    //NV_FREE( m_Foundation.getAllocator(), inTextureData );

}