Skip to content

Commit 2fcbcd6

Browse files
committed
Fixed per view uniform order, and added more validation.
1 parent 587518d commit 2fcbcd6

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/bgfx.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5226,11 +5226,23 @@ namespace bgfx
52265226

52275227
UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num)
52285228
{
5229+
BX_ASSERT(UniformType::End != _type && UniformType::Count > _type
5230+
, "UniformType argument is not valid (_type: %d)!"
5231+
, _type
5232+
);
52295233
return s_ctx->createUniform(_name, UniformFreq::Draw, _type, _num);
52305234
}
52315235

52325236
UniformHandle createUniform(const char* _name, UniformFreq::Enum _freq, UniformType::Enum _type, uint16_t _num)
52335237
{
5238+
BX_ASSERT(UniformType::End != _type && UniformType::Count > _type
5239+
, "UniformType argument is not valid (_type: %d)!"
5240+
, _type
5241+
);
5242+
BX_ASSERT(UniformFreq::Count > _freq
5243+
, "UniformFreq argument is not valid (_freq: %d)!"
5244+
, _freq
5245+
);
52345246
return s_ctx->createUniform(_name, _freq, _type, _num);
52355247
}
52365248

@@ -5442,11 +5454,13 @@ namespace bgfx
54425454
void setViewUniform(ViewId _id, UniformHandle _handle, const void* _value, uint16_t _num)
54435455
{
54445456
BX_ASSERT(checkView(_id), "Invalid view id: %d", _id);
5457+
BGFX_CHECK_HANDLE("setUniform", s_ctx->m_uniformHandle, _handle);
54455458
s_ctx->setViewUniform(_id, _handle, _value, _num);
54465459
}
54475460

54485461
void setFrameUniform(UniformHandle _handle, const void* _value, uint16_t _num)
54495462
{
5463+
BGFX_CHECK_HANDLE("setUniform", s_ctx->m_uniformHandle, _handle);
54505464
s_ctx->setViewUniform(UINT16_MAX, _handle, _value, _num);
54515465
}
54525466

src/bgfx_p.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3289,6 +3289,23 @@ namespace bgfx
32893289
{
32903290
const UniformRef& uniform = getUniformRef(_handle);
32913291

3292+
const UniformFreq::Enum freq = UINT16_MAX == _id
3293+
? UniformFreq::Frame
3294+
: UniformFreq::View
3295+
;
3296+
3297+
BX_ASSERT(0 < uniform.m_refCount
3298+
, "Uniform reference count it 0 (handle %3d)!"
3299+
, _handle.idx
3300+
);
3301+
BX_ASSERT(uniform.m_freq == freq
3302+
, "Setting uniform per view, but uniform is created with different bgfx::UniformFreq::Enum!"
3303+
);
3304+
BX_ASSERT(_num == UINT16_MAX || uniform.m_num >= _num
3305+
, "Truncated uniform update. %d (max: %d)"
3306+
, _num, uniform.m_num
3307+
);
3308+
32923309
UniformCacheKey key =
32933310
{
32943311
.m_offset = 0,
@@ -4667,9 +4684,10 @@ namespace bgfx
46674684
}
46684685

46694686
PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
4670-
if (PredefinedUniform::Count == predefined && UniformType::End != UniformType::Enum(type) )
4687+
if (PredefinedUniform::Count == predefined
4688+
&& UniformType::End != UniformType::Enum(type) )
46714689
{
4672-
uniforms[sr.m_num] = createUniform(name, UniformFreq::Draw, UniformType::Enum(type), num);
4690+
uniforms[sr.m_num] = createUniform(name, UniformFreq::Count, UniformType::Enum(type), num);
46734691
sr.m_num++;
46744692
}
46754693
}
@@ -5354,7 +5372,11 @@ namespace bgfx
53545372
const uint32_t oldsize = g_uniformTypeSize[uniform.m_type];
53555373
const uint32_t newsize = g_uniformTypeSize[_type];
53565374

5357-
uniform.m_freq = _freq; // Ignore shader created uniforms, and use UniformFreq when user creates uniform.
5375+
if (UniformFreq::Count != _freq)
5376+
{
5377+
// Ignore shader created uniforms, and use UniformFreq when user creates uniform.
5378+
uniform.m_freq = _freq;
5379+
}
53585380

53595381
if (oldsize < newsize
53605382
|| uniform.m_num < _num)
@@ -5390,7 +5412,10 @@ namespace bgfx
53905412
UniformRef& uniform = m_uniformRef[handle.idx];
53915413
uniform.m_name.set(_name);
53925414
uniform.m_refCount = 1;
5393-
uniform.m_freq = _freq;
5415+
uniform.m_freq = UniformFreq::Count == _freq
5416+
? UniformFreq::Draw
5417+
: _freq
5418+
;
53945419
uniform.m_type = _type;
53955420
uniform.m_num = _num;
53965421

0 commit comments

Comments
 (0)