Skip to content

Commit

Permalink
Initial progress on supporting double precision floating point as sha…
Browse files Browse the repository at this point in the history
…der uniforms and texture buffers.

Texture buffer support is blocked by CUDATextureBuffer type being templated, making it awkward to have dynamic support for any type.
  • Loading branch information
Robadob committed Aug 12, 2024
1 parent 5fe5abf commit fcdb371
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 44 deletions.
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
122 changes: 102 additions & 20 deletions src/flamegpu/visualiser/shader/DirectionFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ DirectionFunction::DirectionFunction(const std::map<TexBufferConfig::Function, T
, has_bank(tex_buffers.find(TexBufferConfig::Bank) != tex_buffers.end())
, has_direction_hp(tex_buffers.find(TexBufferConfig::Direction_hp) != tex_buffers.end())
, has_direction_hpb(tex_buffers.find(TexBufferConfig::Direction_hpb) != tex_buffers.end())
, has_fw_dbl_x(tex_buffers.find(TexBufferConfig::Forward_dbl_x) != tex_buffers.end())
, has_fw_dbl_y(tex_buffers.find(TexBufferConfig::Forward_dbl_y) != tex_buffers.end())
, has_fw_dbl_z(tex_buffers.find(TexBufferConfig::Forward_dbl_z) != tex_buffers.end())
, has_fw_dbl_xz(tex_buffers.find(TexBufferConfig::Forward_dbl_xz) != tex_buffers.end())
, has_fw_dbl_xyz(tex_buffers.find(TexBufferConfig::Forward_dbl_xyz) != tex_buffers.end())
, has_up_dbl_x(tex_buffers.find(TexBufferConfig::Up_dbl_x) != tex_buffers.end())
, has_up_dbl_y(tex_buffers.find(TexBufferConfig::Up_dbl_y) != tex_buffers.end())
, has_up_dbl_z(tex_buffers.find(TexBufferConfig::Up_dbl_z) != tex_buffers.end())
, has_up_dbl_xyz(tex_buffers.find(TexBufferConfig::Up_dbl_xyz) != tex_buffers.end())
, has_heading_dbl(tex_buffers.find(TexBufferConfig::Heading_dbl) != tex_buffers.end())
, has_pitch_dbl(tex_buffers.find(TexBufferConfig::Pitch_dbl) != tex_buffers.end())
, has_bank_dbl(tex_buffers.find(TexBufferConfig::Bank_dbl) != tex_buffers.end())
, has_direction_dbl_hp(tex_buffers.find(TexBufferConfig::Direction_dbl_hp) != tex_buffers.end())
, has_direction_dbl_hpb(tex_buffers.find(TexBufferConfig::Direction_dbl_hpb) != tex_buffers.end())
{ }

std::string DirectionFunction::getSrc() {
Expand All @@ -68,20 +82,20 @@ std::string DirectionFunction::getSrc() {
ss << ROTATION_MAT_FN;
ss << ATAN2_FN;
// Define any sampler buffers
if (has_fw_x) ss << "uniform samplerBuffer _fw_x;" << "\n";
if (has_fw_y) ss << "uniform samplerBuffer _fw_y;" << "\n";
if (has_fw_z) ss << "uniform samplerBuffer _fw_z;" << "\n";
if (has_fw_xz) ss << "uniform samplerBuffer _fw_xz;" << "\n";
if (has_fw_xyz) ss << "uniform samplerBuffer _fw_xyz;" << "\n";
if (has_up_x) ss << "uniform samplerBuffer _up_x;" << "\n";
if (has_up_y) ss << "uniform samplerBuffer _up_y;" << "\n";
if (has_up_z) ss << "uniform samplerBuffer _up_z;" << "\n";
if (has_up_xyz) ss << "uniform samplerBuffer _up_xyz;" << "\n";
if (has_heading) ss << "uniform samplerBuffer _heading;" << "\n";
if (has_pitch) ss << "uniform samplerBuffer _pitch;" << "\n";
if (has_bank) ss << "uniform samplerBuffer _bank;" << "\n";
if (has_direction_hp) ss << "uniform samplerBuffer _direction_hp;" << "\n";
if (has_direction_hpb) ss << "uniform samplerBuffer _direction_hpb;" << "\n";
if (has_fw_x || has_fw_dbl_x) ss << "uniform samplerBuffer _fw_x;" << "\n";
if (has_fw_y || has_fw_dbl_y) ss << "uniform samplerBuffer _fw_y;" << "\n";
if (has_fw_z || has_fw_dbl_z) ss << "uniform samplerBuffer _fw_z;" << "\n";
if (has_fw_xz || has_fw_dbl_xz) ss << "uniform samplerBuffer _fw_xz;" << "\n";
if (has_fw_xyz || has_fw_dbl_xyz) ss << "uniform samplerBuffer _fw_xyz;" << "\n";
if (has_up_x || has_up_dbl_x) ss << "uniform samplerBuffer _up_x;" << "\n";
if (has_up_y || has_up_dbl_y) ss << "uniform samplerBuffer _up_y;" << "\n";
if (has_up_z || has_up_dbl_z) ss << "uniform samplerBuffer _up_z;" << "\n";
if (has_up_xyz || has_up_dbl_xyz) ss << "uniform samplerBuffer _up_xyz;" << "\n";
if (has_heading || has_heading_dbl) ss << "uniform samplerBuffer _heading;" << "\n";
if (has_pitch || has_pitch_dbl) ss << "uniform samplerBuffer _pitch;" << "\n";
if (has_bank || has_bank_dbl) ss << "uniform samplerBuffer _bank;" << "\n";
if (has_direction_hp || has_direction_dbl_hp) ss << "uniform samplerBuffer _direction_hp;" << "\n";
if (has_direction_hpb || has_direction_dbl_hpb) ss << "uniform samplerBuffer _direction_hpb;" << "\n";
// Begin function
ss << "mat3 getDirection() {" << "\n";
// Define vectors for our global coordinate system
Expand Down Expand Up @@ -122,6 +136,39 @@ std::string DirectionFunction::getSrc() {
ss << " return mat3(1);" << "\n";
// Normalize target incase the user forgot
ss << "target = normalize(target);" << "\n";
} else if (has_fw_dbl_x || has_fw_dbl_y || has_fw_dbl_z || has_fw_dbl_xz || has_fw_dbl_xyz) {
// missing buffers always return 0
ss << "vec3 target = vec3(" << "\n";
if (has_fw_dbl_x) {
ss << " float(texelFetch(_fw_x, gl_InstanceID).x)," << "\n";
} else if (has_fw_dbl_xz) {
ss << " float(texelFetch(_fw_xz, gl_InstanceID * 2).x)," << "\n";
} else if (has_fw_dbl_xyz) {
ss << " float(texelFetch(_fw_xyz, gl_InstanceID * 3).x)," << "\n";
} else {
ss << " 0," << "\n";
}
if (has_fw_dbl_y) {
ss << " float(texelFetch(_fw_y, gl_InstanceID).x)," << "\n";
} else if (has_fw_dbl_xyz) {
ss << " float(texelFetch(_fw_xyz, (gl_InstanceID * 3) + 1).x)," << "\n";
} else {
ss << " 0," << "\n";
}
if (has_fw_dbl_z) {
ss << " float(texelFetch(_fw_z, gl_InstanceID).x));" << "\n";
} else if (has_fw_dbl_xz) {
ss << " float(texelFetch(_fw_xz, (gl_InstanceID * 2) + 1).x));" << "\n";
} else if (has_fw_dbl_xyz) {
ss << " float(texelFetch(_fw_xyz, (gl_InstanceID * 3) + 2).x));" << "\n";
} else {
ss << " 0);" << "\n";
}
// If target is null, don't rotate
ss << "if (target.xyz == vec3(0))" << "\n";
ss << " return mat3(1);" << "\n";
// Normalize target incase the user forgot
ss << "target = normalize(target);" << "\n";
} else {
if (has_direction_hp) {
ss << "const int t = gl_InstanceID * 2;" << "\n";
Expand All @@ -131,12 +178,24 @@ std::string DirectionFunction::getSrc() {
ss << "const int t = gl_InstanceID * 3;" << "\n";
ss << "const float angle_H = texelFetch(_direction_hpb, t).x;" << "\n";
ss << "const float angle_P = texelFetch(_direction_hpb, t + 1).x;" << "\n";
} else if (has_direction_dbl_hp) {
ss << "const int t = gl_InstanceID * 2;" << "\n";
ss << "const float angle_H = float(texelFetch(_direction_hp, t).x);" << "\n";
ss << "const float angle_P = float(texelFetch(_direction_hp, t + 1).x);" << "\n";
} else if (has_direction_dbl_hpb) {
ss << "const int t = gl_InstanceID * 3;" << "\n";
ss << "const float angle_H = float(texelFetch(_direction_hpb, t).x);" << "\n";
ss << "const float angle_P = float(texelFetch(_direction_hpb, t + 1).x);" << "\n";
} else {
if (has_heading) {
ss << "const float angle_H = texelFetch(_heading, gl_InstanceID).x;" << "\n";
} else if(has_heading_dbl) {

Check failure on line 192 in src/flamegpu/visualiser/shader/DirectionFunction.cpp

View workflow job for this annotation

GitHub Actions / cpplint (11.0, ubuntu-20.04)

Missing space before ( in if(

Check failure on line 192 in src/flamegpu/visualiser/shader/DirectionFunction.cpp

View workflow job for this annotation

GitHub Actions / cpplint (11.0, ubuntu-20.04)

Missing space before ( in if(
ss << "const float angle_H = float(texelFetch(_heading, gl_InstanceID).x);" << "\n";
}
if (has_pitch) {
ss << "const float angle_P = texelFetch(_pitch, gl_InstanceID).x;" << "\n";
} else if (has_pitch_dbl) {
ss << "const float angle_P = float(texelFetch(_pitch, gl_InstanceID).x);" << "\n";
}
}
}
Expand All @@ -153,36 +212,59 @@ std::string DirectionFunction::getSrc() {
}
// Normalize target_up incase the user forgot
ss << "target_up = normalize(target_up);" << "\n";
} else if (((has_fw_dbl_x && has_fw_dbl_y && has_fw_dbl_z) || (has_fw_dbl_xz && has_fw_dbl_y) || has_fw_dbl_xyz) && ((has_up_dbl_x && has_up_dbl_y && has_up_dbl_z) || has_up_dbl_xyz)) {
ss << "vec3 target_up = vec3(" << "\n";
if (has_up_dbl_xyz) {
ss << " float(texelFetch(_up_xyz, gl_InstanceID * 3).x)," << "\n";
ss << " float(texelFetch(_up_xyz, (gl_InstanceID * 3) + 1).x)," << "\n";
ss << " float(texelFetch(_up_xyz, (gl_InstanceID * 3) + 2).x));" << "\n";
} else {
ss << " float(texelFetch(_up_x, gl_InstanceID).x)," << "\n";
ss << " float(texelFetch(_up_y, gl_InstanceID).x)," << "\n";
ss << " float(texelFetch(_up_z, gl_InstanceID).x));" << "\n";
}
// Normalize target_up incase the user forgot
ss << "target_up = normalize(target_up);" << "\n";
} else if (has_bank) {
ss << "const float angle_B = texelFetch(_bank, gl_InstanceID).x;" << "\n";
} else if (has_direction_hpb) {
ss << "const float angle_B = texelFetch(_direction_hpb, t + 2).x;" << "\n";
} else if (has_bank_dbl) {
ss << "const float angle_B = float(texelFetch(_bank, gl_InstanceID).x);" << "\n";
} else if (has_direction_dbl_hpb) {
ss << "const float angle_B = float(texelFetch(_direction_hpb, t + 2).x);" << "\n";
}
// Begin to perform rotation
ss << "mat3 rm = mat3(1);" << "\n";
// Euler angle extraction is based on: https://stackoverflow.com/questions/21622956/how-to-convert-direction-vector-to-euler-angles
// Apply rotation about 1st axis
if (has_heading || (has_fw_x || has_fw_z) || has_fw_xz || has_fw_xyz || has_direction_hp || has_direction_hpb) {
if (has_heading || (has_fw_x || has_fw_z) || has_fw_xz || has_fw_xyz || has_direction_hp || has_direction_hpb ||
has_heading_dbl || (has_fw_dbl_x || has_fw_dbl_z) || has_fw_dbl_xz || has_fw_dbl_xyz || has_direction_dbl_hp || has_direction_dbl_hpb) {
// Calculate the heading angle (yaw, about Y)
if (!(has_heading || has_direction_hp || has_direction_hpb)) {
if (!(has_heading || has_direction_hp || has_direction_hpb||
has_heading_dbl || has_direction_dbl_hp || has_direction_dbl_hpb)) {
ss << "float angle_H = atan2(target.z, target.x);" << "\n";
}
// Apply the heading angle
ss << "rm = RotationMat(UP, angle_H) * rm;" << "\n";
}
// Apply rotation about 2nd axis
if (has_pitch || has_fw_y || has_fw_xyz || has_direction_hp || has_direction_hpb) {
if (has_pitch || has_fw_y || has_fw_xyz || has_direction_hp || has_direction_hpb ||
has_pitch_dbl || has_fw_dbl_y || has_fw_dbl_xyz || has_direction_dbl_hp || has_direction_dbl_hpb) {
// Calculate the pitch angle (pitch, about Z)
if (!(has_pitch || has_direction_hp || has_direction_hpb)) {
if (!(has_pitch || has_direction_hp || has_direction_hpb ||
has_pitch_dbl || has_direction_dbl_hp || has_direction_dbl_hpb)) {
ss << "float angle_P = -asin(target.y);" << "\n";
}
// Apply the pitch angle
ss << "rm = RotationMat(rm * RIGHT, angle_P) * rm;" << "\n";
}
// Apply rotation about 3rd axis
if (has_bank || has_direction_hpb || (((has_fw_x && has_fw_y && has_fw_z) || (has_fw_xz && has_fw_y) || has_fw_xyz) && ((has_up_x && has_up_y && has_up_z) || has_up_xyz))) {
if (has_bank || has_direction_hpb || (((has_fw_x && has_fw_y && has_fw_z) || (has_fw_xz && has_fw_y) || has_fw_xyz) && ((has_up_x && has_up_y && has_up_z) || has_up_xyz ||
has_bank_dbl || has_direction_dbl_hpb || (((has_fw_dbl_x && has_fw_dbl_y && has_fw_dbl_z) || (has_fw_dbl_xz && has_fw_dbl_y) || has_fw_dbl_xyz) && ((has_up_dbl_x && has_up_dbl_y && has_up_dbl_z) || has_up_dbl_xyz))) {
// Calculate the bank angle (roll, about X)
if (!(has_bank || has_direction_hpb)) {
if (!(has_bank || has_direction_hpb ||
has_bank_dbl || has_direction_dbl_hpb)) {
ss << "float angle_B = 0; " << "\n";
ss << "if (target.x != 0 || target.z != 0) {" << "\n";
ss << " vec3 W0 = vec3(-target.z, 0, target.x);" << "\n";
Expand Down
14 changes: 14 additions & 0 deletions src/flamegpu/visualiser/shader/DirectionFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ class DirectionFunction {
bool has_bank;
bool has_direction_hp;
bool has_direction_hpb;
bool has_fw_dbl_x;
bool has_fw_dbl_y;
bool has_fw_dbl_z;
bool has_fw_dbl_xz;
bool has_fw_dbl_xyz;
bool has_up_dbl_x;
bool has_up_dbl_y;
bool has_up_dbl_z;
bool has_up_dbl_xyz;
bool has_heading_dbl;
bool has_pitch_dbl;
bool has_bank_dbl;
bool has_direction_dbl_hp;
bool has_direction_dbl_hpb;
static const char* ROTATION_MAT_FN;
static const char* ATAN2_FN;
};
Expand Down
22 changes: 22 additions & 0 deletions src/flamegpu/visualiser/shader/PositionFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace visualiser {
, has_pos_z(tex_buffers.find(TexBufferConfig::Position_z) != tex_buffers.end())
, has_pos_xy(tex_buffers.find(TexBufferConfig::Position_xy) != tex_buffers.end())
, has_pos_xyz(tex_buffers.find(TexBufferConfig::Position_xyz) != tex_buffers.end())
, has_pos_dbl_x(tex_buffers.find(TexBufferConfig::Position_dbl_x) != tex_buffers.end())
, has_pos_dbl_y(tex_buffers.find(TexBufferConfig::Position_dbl_y) != tex_buffers.end())
, has_pos_dbl_z(tex_buffers.find(TexBufferConfig::Position_dbl_z) != tex_buffers.end())
, has_pos_dbl_xy(tex_buffers.find(TexBufferConfig::Position_dbl_xy) != tex_buffers.end())
, has_pos_dbl_xyz(tex_buffers.find(TexBufferConfig::Position_dbl_xyz) != tex_buffers.end())
{ }

std::string PositionFunction::getSrc() {
Expand Down Expand Up @@ -41,6 +46,23 @@ std::string PositionFunction::getSrc() {
ss << " texelFetch(_pos_xyz, t).x," << "\n";
ss << " texelFetch(_pos_xyz, t + 1).x," << "\n";
ss << " texelFetch(_pos_xyz, t + 2).x);" << "\n";
} else if (has_pos_dbl_x || has_pos_dbl_y || has_pos_dbl_z) {
ss << " return vec3(" <<"\n";
ss << " " << (has_pos_dbl_x ? "float(texelFetch(_pos_x, gl_InstanceID).x)" : "0") << "," << "\n";
ss << " " << (has_pos_dbl_y ? "float(texelFetch(_pos_y, gl_InstanceID).x)" : "0") << "," << "\n";
ss << " " << (has_pos_dbl_z ? "float(texelFetch(_pos_z, gl_InstanceID).x)" : "0") << ");" << "\n";
} else if (has_pos_dbl_xy) {
ss << " const int t = gl_InstanceID * 2;" << "\n";
ss << " return vec3(" << "\n";
ss << " float(texelFetch(_pos_xy, t).x)," << "\n";
ss << " float(texelFetch(_pos_xy, t + 1).x)," << "\n";
ss << " 0);" << "\n";
} else if (has_pos_dbl_xyz) {
ss << " const int t = gl_InstanceID * 3;" << "\n";
ss << " return vec3(" << "\n";
ss << " float(texelFetch(_pos_xyz, t).x)," << "\n";
ss << " float(texelFetch(_pos_xyz, t + 1).x)," << "\n";
ss << " float(texelFetch(_pos_xyz, t + 2).x));" << "\n";
} else {
ss << "return glm::vec3(0);" << "\n";
}
Expand Down
5 changes: 5 additions & 0 deletions src/flamegpu/visualiser/shader/PositionFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class PositionFunction {
bool has_pos_z;
bool has_pos_xy;
bool has_pos_xyz;
bool has_pos_dbl_x;
bool has_pos_dbl_y;
bool has_pos_dbl_z;
bool has_pos_dbl_xy;
bool has_pos_dbl_xyz;
};

} // namespace visualiser
Expand Down
Loading

0 comments on commit fcdb371

Please sign in to comment.