Added uniform frequency. (#3485)

* Added uniform frequency.

* Cleanup.

* Cleanup.
This commit is contained in:
Branimir Karadžić
2025-11-10 15:41:33 -08:00
committed by GitHub
parent 8a9bc3ed65
commit 35911ac2d9
25 changed files with 1294 additions and 117 deletions

View File

@@ -49,7 +49,7 @@ public:
, 0
);
u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
u_time = bgfx::createUniform("u_time", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
// Create program from shaders.
m_program = loadProgram("vs_mesh", "fs_mesh");
@@ -104,7 +104,7 @@ public:
bgfx::touch(0);
float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
bgfx::setUniform(u_time, &time);
bgfx::setFrameUniform(u_time, &time);
const bx::Vec3 at = { 0.0f, 1.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 1.0f, -2.5f };

View File

@@ -367,7 +367,7 @@ public:
s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
// Create time uniform.
u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
u_time = bgfx::createUniform("u_time", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
for(uint32_t ii = 0; ii<BX_COUNTOF( m_textureCube ); ++ii)
{
@@ -592,7 +592,7 @@ public:
int64_t now = bx::getHPCounter();
float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
bgfx::setUniform(u_time, &time);
bgfx::setFrameUniform(u_time, &time);
if (now > m_updateTime)
{

View File

@@ -187,9 +187,11 @@ public:
s_texLum = bgfx::createUniform("s_texLum", bgfx::UniformType::Sampler);
s_texBlur = bgfx::createUniform("s_texBlur", bgfx::UniformType::Sampler);
u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
u_tonemap = bgfx::createUniform("u_tonemap", bgfx::UniformType::Vec4);
u_offset = bgfx::createUniform("u_offset", bgfx::UniformType::Vec4, 16);
// Tonemap value will be updated once per frame.
u_tonemap = bgfx::createUniform("u_tonemap", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
m_mesh = meshLoad("meshes/bunny.bin");
m_fbh.idx = bgfx::kInvalidHandle;
@@ -515,7 +517,8 @@ public:
// Set view and projection matrix for view hdrMesh.
bgfx::setViewTransform(hdrMesh, view, proj);
float tonemap[4] = { m_middleGray, bx::square(m_white), m_threshold, m_time };
const float tonemap[4] = { m_middleGray, bx::square(m_white), m_threshold, m_time };
bgfx::setFrameUniform(u_tonemap, tonemap);
// Render skybox into view hdrSkybox.
bgfx::setTexture(0, s_texCube, m_uffizi);
@@ -526,7 +529,6 @@ public:
// Render m_mesh into view hdrMesh.
bgfx::setTexture(0, s_texCube, m_uffizi);
bgfx::setUniform(u_tonemap, tonemap);
meshSubmit(m_mesh, hdrMesh, m_meshProgram, NULL);
// Calculate luminance.
@@ -569,14 +571,12 @@ public:
bgfx::setTexture(0, s_texColor, m_fbtextures[0]);
bgfx::setTexture(1, s_texLum, bgfx::getTexture(m_lum[4]) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
bgfx::setUniform(u_tonemap, tonemap);
screenSpaceQuad(m_caps->originBottomLeft);
bgfx::submit(hdrBrightness, m_brightProgram);
// m_blur m_bright pass vertically.
bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_bright) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
bgfx::setUniform(u_tonemap, tonemap);
screenSpaceQuad(m_caps->originBottomLeft);
bgfx::submit(hdrVBlur, m_blurProgram);

View File

@@ -288,21 +288,22 @@ struct Uniforms
m_lightRgbInnerR[ii][3] = 1.0f;
}
u_ambient = bgfx::createUniform("u_ambient", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_diffuse = bgfx::createUniform("u_diffuse", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_specular_shininess = bgfx::createUniform("u_specular_shininess", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
u_ambient = bgfx::createUniform("u_ambient", bgfx::UniformType::Vec4);
u_diffuse = bgfx::createUniform("u_diffuse", bgfx::UniformType::Vec4);
u_specular_shininess = bgfx::createUniform("u_specular_shininess", bgfx::UniformType::Vec4);
u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4, MAX_NUM_LIGHTS);
u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4, MAX_NUM_LIGHTS);
}
//call this once at initialization
void submitConstUniforms()
void submitFrameUniforms()
{
bgfx::setUniform(u_ambient, &m_ambient);
bgfx::setUniform(u_diffuse, &m_diffuse);
bgfx::setUniform(u_specular_shininess, &m_specular_shininess);
bgfx::setFrameUniform(u_ambient, &m_ambient);
bgfx::setFrameUniform(u_diffuse, &m_diffuse);
bgfx::setFrameUniform(u_specular_shininess, &m_specular_shininess);
}
//call this before each draw call
@@ -979,7 +980,7 @@ public:
imguiEndFrame();
s_uniforms.submitConstUniforms();
s_uniforms.submitFrameUniforms();
// Update settings.
uint8_t numLights = (uint8_t)m_numLights;

View File

@@ -385,55 +385,49 @@ struct Uniforms
m_csmFarDistances[2] = 180.0f;
m_csmFarDistances[3] = 1000.0f;
m_tetraNormalGreen[0] = 0.0f;
m_tetraNormalGreen[1] = -0.57735026f;
m_tetraNormalGreen[2] = 0.81649661f;
m_tetraNormalYellow[0] = 0.0f;
m_tetraNormalYellow[1] = -0.57735026f;
m_tetraNormalYellow[2] = -0.81649661f;
m_tetraNormalBlue[0] = -0.81649661f;
m_tetraNormalBlue[1] = 0.57735026f;
m_tetraNormalBlue[2] = 0.0f;
m_tetraNormalRed[0] = 0.81649661f;
m_tetraNormalRed[1] = 0.57735026f;
m_tetraNormalRed[2] = 0.0f;
m_XNum = 2.0f;
m_YNum = 2.0f;
m_XOffset = 10.0f/512.0f;
m_YOffset = 10.0f/512.0f;
u_params0 = bgfx::createUniform("u_params0", bgfx::UniformType::Vec4);
u_params1 = bgfx::createUniform("u_params1", bgfx::UniformType::Vec4);
u_params2 = bgfx::createUniform("u_params2", bgfx::UniformType::Vec4);
u_params1 = bgfx::createUniform("u_params1", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_params2 = bgfx::createUniform("u_params2", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
u_smSamplingParams = bgfx::createUniform("u_smSamplingParams", bgfx::UniformType::Vec4);
u_csmFarDistances = bgfx::createUniform("u_csmFarDistances", bgfx::UniformType::Vec4);
u_smSamplingParams = bgfx::createUniform("u_smSamplingParams", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_csmFarDistances = bgfx::createUniform("u_csmFarDistances", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4);
u_tetraNormalGreen = bgfx::createUniform("u_tetraNormalGreen", bgfx::UniformType::Vec4);
u_tetraNormalYellow = bgfx::createUniform("u_tetraNormalYellow", bgfx::UniformType::Vec4);
u_tetraNormalBlue = bgfx::createUniform("u_tetraNormalBlue", bgfx::UniformType::Vec4);
u_tetraNormalRed = bgfx::createUniform("u_tetraNormalRed", bgfx::UniformType::Vec4);
u_tetraNormalGreen = bgfx::createUniform("u_tetraNormalGreen", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_tetraNormalYellow = bgfx::createUniform("u_tetraNormalYellow", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_tetraNormalBlue = bgfx::createUniform("u_tetraNormalBlue", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_tetraNormalRed = bgfx::createUniform("u_tetraNormalRed", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
const float tetraNormalGreen[] = { 0.0f, -0.57735026f, 0.81649661f };
const float tetraNormalYellow[] = { 0.0f, -0.57735026f, -0.81649661f };
const float tetraNormalBlue[] = { -0.81649661f, 0.57735026f, 0.0f };
const float tetraNormalRed[] = { 0.81649661f, 0.57735026f, 0.0f };
bgfx::setFrameUniform(u_tetraNormalGreen, tetraNormalGreen);
bgfx::setFrameUniform(u_tetraNormalYellow, tetraNormalYellow);
bgfx::setFrameUniform(u_tetraNormalBlue, tetraNormalBlue);
bgfx::setFrameUniform(u_tetraNormalRed, tetraNormalRed);
u_shadowMapMtx0 = bgfx::createUniform("u_shadowMapMtx0", bgfx::UniformType::Mat4);
u_shadowMapMtx1 = bgfx::createUniform("u_shadowMapMtx1", bgfx::UniformType::Mat4);
u_shadowMapMtx2 = bgfx::createUniform("u_shadowMapMtx2", bgfx::UniformType::Mat4);
u_shadowMapMtx3 = bgfx::createUniform("u_shadowMapMtx3", bgfx::UniformType::Mat4);
u_lightPosition = bgfx::createUniform("u_lightPosition", bgfx::UniformType::Vec4);
u_lightAmbientPower = bgfx::createUniform("u_lightAmbientPower", bgfx::UniformType::Vec4);
u_lightDiffusePower = bgfx::createUniform("u_lightDiffusePower", bgfx::UniformType::Vec4);
u_lightSpecularPower = bgfx::createUniform("u_lightSpecularPower", bgfx::UniformType::Vec4);
u_lightSpotDirectionInner = bgfx::createUniform("u_lightSpotDirectionInner", bgfx::UniformType::Vec4);
u_lightAttenuationSpotOuter = bgfx::createUniform("u_lightAttenuationSpotOuter", bgfx::UniformType::Vec4);
u_lightPosition = bgfx::createUniform("u_lightPosition", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_lightAmbientPower = bgfx::createUniform("u_lightAmbientPower", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_lightDiffusePower = bgfx::createUniform("u_lightDiffusePower", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_lightSpecularPower = bgfx::createUniform("u_lightSpecularPower", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_lightSpotDirectionInner = bgfx::createUniform("u_lightSpotDirectionInner", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_lightAttenuationSpotOuter = bgfx::createUniform("u_lightAttenuationSpotOuter", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_materialKa = bgfx::createUniform("u_materialKa", bgfx::UniformType::Vec4);
u_materialKd = bgfx::createUniform("u_materialKd", bgfx::UniformType::Vec4);
u_materialKs = bgfx::createUniform("u_materialKs", bgfx::UniformType::Vec4);
u_materialKa = bgfx::createUniform("u_materialKa", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_materialKd = bgfx::createUniform("u_materialKd", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
u_materialKs = bgfx::createUniform("u_materialKs", bgfx::UniformFreq::Frame, bgfx::UniformType::Vec4);
}
@@ -450,33 +444,24 @@ struct Uniforms
m_shadowMapMtx3 = _shadowMapMtx3;
}
// Call this once at initialization.
void submitConstUniforms()
{
bgfx::setUniform(u_tetraNormalGreen, m_tetraNormalGreen);
bgfx::setUniform(u_tetraNormalYellow, m_tetraNormalYellow);
bgfx::setUniform(u_tetraNormalBlue, m_tetraNormalBlue);
bgfx::setUniform(u_tetraNormalRed, m_tetraNormalRed);
}
// Call this once per frame.
void submitPerFrameUniforms()
{
bgfx::setUniform(u_params1, m_params1);
bgfx::setUniform(u_params2, m_params2);
bgfx::setUniform(u_smSamplingParams, m_paramsBlur);
bgfx::setUniform(u_csmFarDistances, m_csmFarDistances);
bgfx::setFrameUniform(u_params1, m_params1);
bgfx::setFrameUniform(u_params2, m_params2);
bgfx::setFrameUniform(u_smSamplingParams, m_paramsBlur);
bgfx::setFrameUniform(u_csmFarDistances, m_csmFarDistances);
bgfx::setUniform(u_materialKa, &m_materialPtr->m_ka);
bgfx::setUniform(u_materialKd, &m_materialPtr->m_kd);
bgfx::setUniform(u_materialKs, &m_materialPtr->m_ks);
bgfx::setFrameUniform(u_materialKa, &m_materialPtr->m_ka);
bgfx::setFrameUniform(u_materialKd, &m_materialPtr->m_kd);
bgfx::setFrameUniform(u_materialKs, &m_materialPtr->m_ks);
bgfx::setUniform(u_lightPosition, &m_lightPtr->m_position_viewSpace);
bgfx::setUniform(u_lightAmbientPower, &m_lightPtr->m_ambientPower);
bgfx::setUniform(u_lightDiffusePower, &m_lightPtr->m_diffusePower);
bgfx::setUniform(u_lightSpecularPower, &m_lightPtr->m_specularPower);
bgfx::setUniform(u_lightSpotDirectionInner, &m_lightPtr->m_spotDirectionInner_viewSpace);
bgfx::setUniform(u_lightAttenuationSpotOuter, &m_lightPtr->m_attenuationSpotOuter);
bgfx::setFrameUniform(u_lightPosition, &m_lightPtr->m_position_viewSpace);
bgfx::setFrameUniform(u_lightAmbientPower, &m_lightPtr->m_ambientPower);
bgfx::setFrameUniform(u_lightDiffusePower, &m_lightPtr->m_diffusePower);
bgfx::setFrameUniform(u_lightSpecularPower, &m_lightPtr->m_specularPower);
bgfx::setFrameUniform(u_lightSpotDirectionInner, &m_lightPtr->m_spotDirectionInner_viewSpace);
bgfx::setFrameUniform(u_lightAttenuationSpotOuter, &m_lightPtr->m_attenuationSpotOuter);
}
// Call this before each draw call.
@@ -576,10 +561,6 @@ struct Uniforms
float m_paramsBlur[4];
};
float m_tetraNormalGreen[3];
float m_tetraNormalYellow[3];
float m_tetraNormalBlue[3];
float m_tetraNormalRed[3];
float m_csmFarDistances[4];
float* m_lightMtxPtr;
@@ -1252,7 +1233,6 @@ public:
, &m_shadowMapMtx[ShadowMapRenderTargets::Third][0]
, &m_shadowMapMtx[ShadowMapRenderTargets::Fourth][0]
);
s_uniforms.submitConstUniforms();
// Settings.
ShadowMapSettings smSettings[LightType::Count][DepthImpl::Count][SmImpl::Count] =
@@ -1807,10 +1787,6 @@ public:
float currentShadowMapSizef = float(int16_t(m_currentShadowMapSize) );
s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
s_uniforms.submitConstUniforms();
// s_uniforms.submitConstUniforms();
// Imgui.
imguiBeginFrame(m_mouseState.m_mx
, m_mouseState.m_my

View File

@@ -285,8 +285,6 @@ public:
, 0
);
u_tint = bgfx::createUniform("u_tint", bgfx::UniformType::Vec4);
// Create program from shaders.
m_program = loadProgram("vs_bunnylod", "fs_bunnylod");
@@ -309,7 +307,6 @@ public:
bgfx::destroy(m_program);
bgfx::destroy(m_vb);
bgfx::destroy(m_ib);
bgfx::destroy(u_tint);
bx::free(entry::getAllocator(), m_map);
bx::free(entry::getAllocator(), m_triangle);
@@ -432,8 +429,6 @@ public:
bgfx::touch(0);
float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
const float BasicColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
bgfx::setUniform(u_tint, BasicColor);
const bx::Vec3 at = { 0.0f, 1.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 1.0f, -2.5f };
@@ -493,7 +488,6 @@ public:
bgfx::VertexBufferHandle m_vb;
bgfx::DynamicIndexBufferHandle m_ib;
bgfx::ProgramHandle m_program;
bgfx::UniformHandle u_tint;
};
} // namespace