Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double precision floating point as shader uniforms and texture buffers. #135

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/flamegpu/visualiser/config/TexBufferConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ struct TexBufferConfig {
Position_x, Position_y, Position_z,
Position_xy,
Position_xyz,
Position_dbl_x, Position_dbl_y, Position_dbl_z,
Position_dbl_xy,
Position_dbl_xyz,
/**
* Agent forward/up direction x/y/z
*/
Expand All @@ -26,13 +29,21 @@ struct TexBufferConfig {
Forward_xyz,
Up_x, Up_y, Up_z,
Up_xyz,
Forward_dbl_x, Forward_dbl_y, Forward_dbl_z,
Forward_dbl_xz,
Forward_dbl_xyz,
Up_dbl_x, Up_dbl_y, Up_dbl_z,
Up_dbl_xyz,
/**
* Agent rotation
* (Alternate to Forward/Up vectors)
*/
Heading, Pitch, Bank,
Direction_hp,
Direction_hpb,
Heading_dbl, Pitch_dbl, Bank_dbl,
Direction_dbl_hp,
Direction_dbl_hpb,
/**
* Agent color
*/
Expand Down
79 changes: 69 additions & 10 deletions src/flamegpu/visualiser/shader/ShaderCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ void ShaderCore::setupBindings() {
GLint location = GL_CALL(glGetUniformLocation(this->programId, i->uniformName.c_str()));
if (location != -1) {
if (i->type == GL_FLOAT) {
static_assert(sizeof(int) == sizeof(float), "Error: int and float sizes differ, static float uniforms may be corrupted.\n");
if (i->count == 1) {
GL_CALL(glUniform1fv(location, 1, reinterpret_cast<const GLfloat *>(glm::value_ptr(i->data))));
} else if (i->count == 2) {
Expand All @@ -115,13 +114,13 @@ void ShaderCore::setupBindings() {
}
} else if (i->type == GL_INT) {
if (i->count == 1) {
GL_CALL(glUniform1iv(location, 1, glm::value_ptr(i->data)));
GL_CALL(glUniform1iv(location, 1, reinterpret_cast<const GLint*>(glm::value_ptr(i->data))));
} else if (i->count == 2) {
GL_CALL(glUniform2iv(location, 1, glm::value_ptr(i->data)));
GL_CALL(glUniform2iv(location, 1, reinterpret_cast<const GLint*>(glm::value_ptr(i->data))));
} else if (i->count == 3) {
GL_CALL(glUniform3iv(location, 1, glm::value_ptr(i->data)));
GL_CALL(glUniform3iv(location, 1, reinterpret_cast<const GLint*>(glm::value_ptr(i->data))));
} else if (i->count == 4) {
GL_CALL(glUniform4iv(location, 1, glm::value_ptr(i->data)));
GL_CALL(glUniform4iv(location, 1, reinterpret_cast<const GLint*>(glm::value_ptr(i->data))));
}
} else if (i->type == GL_UNSIGNED_INT) {
if (i->count == 1) {
Expand All @@ -133,8 +132,18 @@ void ShaderCore::setupBindings() {
} else if (i->count == 4) {
GL_CALL(glUniform4uiv(location, 1, reinterpret_cast<const GLuint *>(glm::value_ptr(i->data))));
}
} else if (i->type == GL_DOUBLE) {
if (i->count == 1) {
GL_CALL(glUniform1dv(location, 1, reinterpret_cast<const GLdouble*>(glm::value_ptr(i->data))));
} else if (i->count == 2) {
GL_CALL(glUniform2dv(location, 1, reinterpret_cast<const GLdouble*>(glm::value_ptr(i->data))));
} else if (i->count == 3) {
GL_CALL(glUniform3dv(location, 1, reinterpret_cast<const GLdouble*>(glm::value_ptr(i->data))));
} else if (i->count == 4) {
GL_CALL(glUniform4dv(location, 1, reinterpret_cast<const GLdouble*>(glm::value_ptr(i->data))));
}
} else if (i->type == GL_FLOAT_MAT4) {
GL_CALL(glUniformMatrix4fv(location, 1, false, reinterpret_cast<const GLfloat *>(glm::value_ptr(i->data))));
GL_CALL(glUniformMatrix4fv(location, 1, false, glm::value_ptr(i->data)));
}
} else { // If the uniform isn't found again, remind the user
printf("%s: Static uniform '%s' could not be located on shader reload.\n", this->shaderTag.c_str(), i->uniformName.c_str());
Expand Down Expand Up @@ -233,6 +242,16 @@ void ShaderCore::prepare(bool autoClear) {
} else if (i->second.count == 4) {
GL_CALL(glUniform4uiv(i->first, 1, reinterpret_cast<const GLuint *>(i->second.data)));
}
} else if (i->second.type == GL_DOUBLE) {
if (i->second.count == 1) {
GL_CALL(glUniform1dv(i->first, 1, reinterpret_cast<const GLdouble*>(i->second.data)));
} else if (i->second.count == 2) {
GL_CALL(glUniform2dv(i->first, 1, reinterpret_cast<const GLdouble*>(i->second.data)));
} else if (i->second.count == 3) {
GL_CALL(glUniform3dv(i->first, 1, reinterpret_cast<const GLdouble*>(i->second.data)));
} else if (i->second.count == 4) {
GL_CALL(glUniform4dv(i->first, 1, reinterpret_cast<const GLdouble*>(i->second.data)));
}
} else if (i->second.type == GL_FLOAT_MAT4) {
GL_CALL(glUniformMatrix4fv(i->first, 1, false, reinterpret_cast<const GLfloat *>(i->second.data)));
}
Expand Down Expand Up @@ -286,6 +305,9 @@ bool ShaderCore::addDynamicUniform(const char *uniformName, const GLuint *arry,
bool ShaderCore::addDynamicUniform(const char *uniformName, const GLfloat *arry, unsigned int count) {
return addDynamicUniform({ GL_FLOAT, reinterpret_cast<const void*>(arry), count, uniformName });
}
bool ShaderCore::addDynamicUniform(const char* uniformName, const GLdouble* arry, unsigned int count) {
return addDynamicUniform({ GL_DOUBLE, reinterpret_cast<const void*>(arry), count, uniformName });
}
bool ShaderCore::addDynamicUniform(const char *uniformName, const glm::mat4 *mat) {
return addDynamicUniform({ GL_FLOAT_MAT4, reinterpret_cast<const void*>(mat), 1, uniformName });
}
Expand All @@ -311,8 +333,11 @@ bool ShaderCore::addDynamicUniform(DynamicUniformDetail d) {
bool ShaderCore::addStaticUniform(const char *uniformName, const GLfloat *arry, unsigned int count) {
// Purge any existing buffer which matches
removeStaticUniform(uniformName);
// Note we reinterpret the data to from float to mat4
glm::mat4 temp;
memcpy(&temp, arry, sizeof(float) * count);
// Note we reinterpret_cast the data to from float to int
staticUniforms.push_front({ GL_FLOAT, *reinterpret_cast<const glm::ivec4*>(arry), count, uniformName });
staticUniforms.push_front({ GL_FLOAT, temp, count, uniformName });
if (this->programId > 0 && count > 0 && count <= 4) {
GLint location = GL_CALL(glGetUniformLocation(this->programId, uniformName));
if (location != -1) {
Expand All @@ -337,7 +362,10 @@ bool ShaderCore::addStaticUniform(const char *uniformName, const GLfloat *arry,
bool ShaderCore::addStaticUniform(const char *uniformName, const GLint *arry, unsigned int count) {
// Purge any existing buffer which matches
removeStaticUniform(uniformName);
staticUniforms.push_front({ GL_INT, *reinterpret_cast<const glm::ivec4*>(arry), count, uniformName });
// Note we reinterpret the data to from int to mat4
glm::mat4 temp;
memcpy(&temp, arry, sizeof(int) * count);
staticUniforms.push_front({ GL_INT, temp, count, uniformName });
if (this->programId > 0 && count > 0 && count <= 4) {
GLint location = GL_CALL(glGetUniformLocation(this->programId, uniformName));
if (location != -1) {
Expand All @@ -362,7 +390,10 @@ bool ShaderCore::addStaticUniform(const char *uniformName, const GLint *arry, un
bool ShaderCore::addStaticUniform(const char *uniformName, const GLuint *arry, unsigned int count) {
// Purge any existing buffer which matches
removeStaticUniform(uniformName);
staticUniforms.push_front({ GL_UNSIGNED_INT, *reinterpret_cast<const glm::ivec4*>(arry), count, uniformName });
// Note we reinterpret the data to from uint to mat4
glm::mat4 temp;
memcpy(&temp, arry, sizeof(unsigned int) * count);
staticUniforms.push_front({ GL_UNSIGNED_INT, temp, count, uniformName });
if (this->programId > 0 && count > 0 && count <= 4) {
GLint location = GL_CALL(glGetUniformLocation(this->programId, uniformName));
if (location != -1) {
Expand All @@ -384,10 +415,38 @@ bool ShaderCore::addStaticUniform(const char *uniformName, const GLuint *arry, u
}
return false;
}
bool ShaderCore::addStaticUniform(const char* uniformName, const GLdouble* arry, unsigned int count) {
// Purge any existing buffer which matches
removeStaticUniform(uniformName);
// Note we reinterpret the data to from double to mat4
glm::mat4 temp;
memcpy(&temp, arry, sizeof(double) * count);
staticUniforms.push_front({ GL_DOUBLE, temp, count, uniformName });
if (this->programId > 0 && count > 0 && count <= 4) {
GLint location = GL_CALL(glGetUniformLocation(this->programId, uniformName));
if (location != -1) {
GL_CALL(glUseProgram(this->programId));
if (count == 1) {
GL_CALL(glUniform1dv(location, 1, arry));
} else if (count == 2) {
GL_CALL(glUniform2dv(location, 1, arry));
} else if (count == 3) {
GL_CALL(glUniform3dv(location, 1, arry));
} else if (count == 4) {
GL_CALL(glUniform4dv(location, 1, arry));
}
GL_CALL(glUseProgram(0));
return true;
} else {
fprintf(stderr, "%s: Static uniform named: %s was not found.\n", shaderTag.c_str(), uniformName);
}
}
return false;
}
bool ShaderCore::addStaticUniform(const char *uniformName, const glm::mat4 *mat) {
// Purge any existing buffer which matches
removeStaticUniform(uniformName);
staticUniforms.push_front({ GL_FLOAT_MAT4, *reinterpret_cast<const glm::ivec4*>(mat), 1, uniformName });
staticUniforms.push_front({ GL_FLOAT_MAT4, *mat, 1, uniformName });
if (this->programId > 0) {
GLint location = GL_CALL(glGetUniformLocation(this->programId, uniformName));
if (location != -1) {
Expand Down
49 changes: 35 additions & 14 deletions src/flamegpu/visualiser/shader/ShaderCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ShaderCore : public Reloadable {
*/
void destroyProgram();
/**
* Remembers a pointer to an array of upto 4 floats that will be updated everytime useProgram() is called on this Shaders object
* Remembers a pointer to an array of upto 4 floats that will be updated everytime useProgram() is called on this Shaders object
* If a dynamic uniform with the same uniformName is already bound, it will be replaced
* @param uniformName The name of the uniform within the shader
* @param arry A pointer to the array of floats
Expand All @@ -131,16 +131,27 @@ class ShaderCore : public Reloadable {
*/
bool addDynamicUniform(const char *uniformName, const GLint *arry, unsigned int count = 1);
/**
* Remembers a pointer to an array of upto 4 unsigned integers that will be updated everytime useProgram() is called on this Shaders object
* If a dynamic uniform with the same uniformName is already bound, it will be replaced
* @param uniformName The name of the uniform within the shader
* @param arry A pointer to the array of unsigned integers
* @param count The number of unsigned integers provided in the array (a maximum of 4)
* @returns false if the uniform name was not found or count is outside of the inclusive range 1-4
* @note Even when false is returned, the value will be stored for reprocessing on shader reloads
* @see addDynamicUniform(const char *, const GLfloat *, unsigned int)
*/
* Remembers a pointer to an array of upto 4 unsigned integers that will be updated everytime useProgram() is called on this Shaders object
* If a dynamic uniform with the same uniformName is already bound, it will be replaced
* @param uniformName The name of the uniform within the shader
* @param arry A pointer to the array of unsigned integers
* @param count The number of unsigned integers provided in the array (a maximum of 4)
* @returns false if the uniform name was not found or count is outside of the inclusive range 1-4
* @note Even when false is returned, the value will be stored for reprocessing on shader reloads
* @see addDynamicUniform(const char *, const GLfloat *, unsigned int)
*/
bool addDynamicUniform(const char *uniformName, const GLuint *arry, unsigned int count = 1);
/**
* Remembers a pointer to an array of upto 4 doubles that will be updated everytime useProgram() is called on this Shaders object
* If a dynamic uniform with the same uniformName is already bound, it will be replaced
* @param uniformName The name of the uniform within the shader
* @param arry A pointer to the array of doubles
* @param count The number of doubles provided in the array (a maximum of 4)
* @returns false if the uniform name was not found or count is outside of the inclusive range 1-4
* @note Even when false is returned, the value will be stored for reprocessing on shader reloads
* @see addDynamicUniform(const char *, const GLint *, unsigned int)
*/
bool addDynamicUniform(const char *uniformName, const GLdouble *arry, unsigned int count = 1);
/**
* Remembers a pointer to a mat4 that will be updated everytime useProgram() is called on this Shaders object
* If a dynamic uniform with the same uniformName is already bound, it will be replaced
Expand Down Expand Up @@ -181,6 +192,16 @@ class ShaderCore : public Reloadable {
* @note Even when false is returned, the value will be stored for reprocessing on shader reloads
*/
bool addStaticUniform(const char *uniformName, const GLuint *arry, unsigned int count = 1);
/**
* Sets the value of a vector of upto 4 doubles within the shader
* If a static uniform with the same uniformName is already bound, it will be replaced
* @param uniformName The name of the uniform within the shader
* @param arry A pointer to the array of doubles
* @param count The number of doubles provided in the array (a maximum of 4)
* @returns false if the uniform name was not found or count is outside of the inclusive range 1-4
* @note Even when false is returned, the value will be stored for reprocessing on shader reloads
*/
bool addStaticUniform(const char *uniformName, const GLdouble *arry, unsigned int count = 1);
/**
* Sets the value of a mat4 within the shader
* If a static uniform with the same uniformName is already bound, it will be replaced
Expand Down Expand Up @@ -221,13 +242,13 @@ class ShaderCore : public Reloadable {
* @param bufferBindingPoint The buffer's bind point
* @return True if buffer name is found in the current shader to be bound
*/
bool addBuffer(const char *bufferNameInShader, const GLenum bufferType, const GLuint bufferBindingPoint);
bool addBuffer(const char *bufferNameInShader, GLenum bufferType, GLuint bufferBindingPoint);
/**
* Attaches the specified buffer to the shader if bufferNameInShader can be found
* If a buffer with the same bufferNameInShader is already bound, it will be replaced
* This is for 'program resources', NOT texture buffers
* This will not retain the shared_ptr, it's upto you to keep it alive
* @param bufferNameInShader The indentifier of the buffer within the shader source
* @param bufferNameInShader The identifier of the buffer within the shader source
* @param buffer The buffer to be used
* @return True if buffer name is found in the current shader to be bound
* @note Convenience method, implemented in BufferCore.cpp
Expand Down Expand Up @@ -353,9 +374,9 @@ class ShaderCore : public Reloadable {
/**
* Data to be stored in the shader uniform
* @note components greater than count-1 are likely to contain garbage data
* @note this may actually contain a glm::vec4 if type == GL_FLOAT
* @note this may actually contain a glm::vec4 if type == GL_FLOAT, glm::ivec4 etc
*/
const glm::ivec4 data;
const glm::mat4 data;
/**
* Number of vector components
*/
Expand Down
Loading