Skip to content

Commit 558b261

Browse files
committed
♻️ Update texture and buffer and add a function to simplify dimension grid calculation
1 parent 397fa45 commit 558b261

File tree

4 files changed

+98
-18
lines changed

4 files changed

+98
-18
lines changed

Plugin/PluginInteropUnityCUDA/include/Buffer/vertexBuffer_OpenGLCoreES.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class VertexBuffer_OpenGLCoreES : public VertexBuffer
1010
public:
1111
VertexBuffer_OpenGLCoreES(void* bufferHandle, int size);
1212
~VertexBuffer_OpenGLCoreES();
13-
virtual void registerBufferInCUDA();
14-
virtual void unRegisterBufferInCUDA();
15-
virtual int SetTextureFromBuffer(Texture& texture);
13+
void registerBufferInCUDA() override;
14+
void unRegisterBufferInCUDA() override;
15+
int SetTextureFromBuffer(Texture& texture) const override;
1616

1717
};
1818

Plugin/PluginInteropUnityCUDA/include/cudaInclude.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,75 @@ inline void gpuAssert(cudaError_t code, const char* file, int line)
1414
Log::log().debugLogError(buffer);
1515
}
1616
}
17+
18+
19+
/// <summary>
20+
/// Get thedim grid to use for a dispatch, from a multiple of
21+
/// dim block that are used by the kernel, and the number of
22+
/// calculation that has to be done.
23+
/// </summary>
24+
/// <param name="dimBlock">Number of threads per block
25+
/// </param>
26+
/// <param name="numCalculation">Number of calculation
27+
/// to do on kernel (eg. if we make calculation on a 1024x1024 texture, and
28+
/// we only want to compute a value on the first 528x528 pixels , then
29+
/// numCalculation = 528,528,1)
30+
/// </param>
31+
/// <param name="getUp">If true will get the
32+
/// upper multiple of dimBlock, else will get the lower multiple. By
33+
/// default its true.
34+
/// </param>
35+
/// <param name="mustDoAllCalculation">if true
36+
/// imply that dimBlock must be multiple of numCalculation
37+
/// </param>
38+
/// <returns>The dim of grid to use in dispatch</returns>
39+
inline dim3 calculateDimGrid(dim3 dimBlock, dim3 numCalculation, bool getUp = true,
40+
bool mustDoAllCalculation = false)
41+
{
42+
int addFactor = getUp ? 1 : 0;
43+
float invDimBlockX = 1.0f / dimBlock.x;
44+
float invDimBlockY = 1.0f / dimBlock.y;
45+
float invDimBlockZ = 1.0f / dimBlock.z;
46+
47+
if (mustDoAllCalculation)
48+
{
49+
if (numCalculation.x % dimBlock.x != 0 ||
50+
numCalculation.y % dimBlock.y != 0 ||
51+
numCalculation.z % dimBlock.z != 0)
52+
{
53+
Log::log().debugLogError(
54+
"Number of threads per block (" + std::to_string(dimBlock.x) +
55+
", " + std::to_string(dimBlock.y) + ", " +
56+
std::to_string(dimBlock.z) +
57+
")"
58+
" is not a multiple of (" +
59+
std::to_string(numCalculation.x) + ", " +
60+
std::to_string(numCalculation.y) + ", " +
61+
std::to_string(numCalculation.z) +
62+
")"
63+
", therefore the compute shader will not compute on all data.");
64+
}
65+
}
66+
67+
unsigned int multipleDimBlockX =
68+
dimBlock.x * ((int)(numCalculation.x * invDimBlockX) + addFactor);
69+
unsigned int dimGridX = multipleDimBlockX / dimBlock.x;
70+
71+
unsigned int multipleDimBlockY =
72+
dimBlock.y * ((int)(numCalculation.y * invDimBlockY) + addFactor);
73+
unsigned int dimGridY = multipleDimBlockY / dimBlock.y;
74+
75+
unsigned int multipleDimBlockZ =
76+
dimBlock.z * ((int)(numCalculation.z * invDimBlockZ) + addFactor);
77+
unsigned int dimGridZ = multipleDimBlockZ / dimBlock.z;
78+
79+
if (dimGridX < 1 || dimGridY < 1 || dimGridZ <1)
80+
{
81+
Log::log().debugLogError(
82+
"Threads group size " + std::to_string(dimGridX) +
83+
std::to_string(dimGridY) + std::to_string(dimGridZ) +
84+
" must be above zero.");
85+
}
86+
87+
return dim3{dimGridX, dimGridY, dimGridZ};
88+
}

Plugin/PluginInteropUnityCUDA/src/Buffer/vertexBuffer_OpenGLCoreES.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,22 @@ void VertexBuffer_OpenGLCoreES::registerBufferInCUDA()
3232
CUDA_CHECK(cudaGraphicsGLRegisterBuffer(&_pGraphicsResource, glBuffer, cudaGraphicsRegisterFlagsNone));
3333
}
3434

35-
int VertexBuffer_OpenGLCoreES::SetTextureFromBuffer(Texture& texture)
35+
int VertexBuffer_OpenGLCoreES::SetTextureFromBuffer(Texture& texture) const
3636
{
37-
//TODO: add some check on type
38-
if (texture.getHeight() * texture.getWidth() != _size)
39-
{
40-
Log::log().debugLogError("Cannot create a texture from a buffer which has a different size");
41-
return -1;
42-
}
43-
44-
// Select the appropriate buffer
45-
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, (GLuint)_bufferHandle);
46-
// Select the appropriate texture
47-
glBindTexture(GL_TEXTURE_2D, (GLuint)texture.getNativeTexturePtr());
48-
49-
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture.getWidth(), texture.getHeight(), GL_BGRA, GL_UNSIGNED_BYTE, NULL);
37+
return 0;
38+
////TODO: add some check on type
39+
//if (texture.getHeight() * texture.getWidth() != _size)
40+
//{
41+
// Log::log().debugLogError("Cannot create a texture from a buffer which has a different size");
42+
// return -1;
43+
//}
44+
45+
//// Select the appropriate buffer
46+
//glBindBuffer(GL_PIXEL_UNPACK_BUFFER, (GLuint)_bufferHandle);
47+
//// Select the appropriate texture
48+
//glBindTexture(GL_TEXTURE_2D, (GLuint)texture.getNativeTexturePtr());
49+
50+
//glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture.getWidth(), texture.getHeight(), GL_BGRA, GL_UNSIGNED_BYTE, NULL);
5051
}
5152

5253
void VertexBuffer_OpenGLCoreES::unRegisterBufferInCUDA()

Plugin/PluginInteropUnityCUDA/src/Texture/texture.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ Texture::Texture(void* textureHandle, int textureWidth, int textureHeight, int t
1212
// set a default size of grid and block to avoid calculating it each time
1313
// TODO : update this for texture depth
1414
_dimBlock = { 8, 8, 1 };
15-
_dimGrid = { (textureWidth + _dimBlock.x - 1) / _dimBlock.x,
15+
_dimGrid = calculateDimGrid(_dimBlock, {textureWidth, textureHeight, 1});
16+
dim3 dimGrid = { (textureWidth + _dimBlock.x - 1) / _dimBlock.x,
1617
(textureHeight + _dimBlock.y - 1) / _dimBlock.y, 1};
18+
Log::log().debugLog("(" + std::to_string(dimGrid.x) + ", " +
19+
std::to_string(dimGrid.y) + ", " +
20+
std::to_string(dimGrid.z) + ")");
21+
Log::log().debugLog("(" + std::to_string(_dimGrid.x) + ", " +
22+
std::to_string(_dimGrid.y) + ", " +
23+
std::to_string(_dimGrid.z) + ")");
1724
_pGraphicsResource = nullptr;
1825
}
1926

0 commit comments

Comments
 (0)