Explorar o código

Fixed per view uniform order, and added more validation. (#3497)

Branimir Karadžić hai 1 mes
pai
achega
f888f9d9a4
Modificáronse 2 ficheiros con 43 adicións e 4 borrados
  1. 14 0
      src/bgfx.cpp
  2. 29 4
      src/bgfx_p.h

+ 14 - 0
src/bgfx.cpp

@@ -5226,11 +5226,23 @@ namespace bgfx
 
 	UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num)
 	{
+		BX_ASSERT(UniformType::End != _type && UniformType::Count > _type
+			, "UniformType argument is not valid (_type: %d)!"
+			, _type
+			);
 		return s_ctx->createUniform(_name, UniformFreq::Draw, _type, _num);
 	}
 
 	UniformHandle createUniform(const char* _name, UniformFreq::Enum _freq, UniformType::Enum _type, uint16_t _num)
 	{
+		BX_ASSERT(UniformType::End != _type && UniformType::Count > _type
+			, "UniformType argument is not valid (_type: %d)!"
+			, _type
+			);
+		BX_ASSERT(UniformFreq::Count > _freq
+			, "UniformFreq argument is not valid (_freq: %d)!"
+			, _freq
+			);
 		return s_ctx->createUniform(_name, _freq, _type, _num);
 	}
 
@@ -5442,11 +5454,13 @@ namespace bgfx
 	void setViewUniform(ViewId _id, UniformHandle _handle, const void* _value, uint16_t _num)
 	{
 		BX_ASSERT(checkView(_id), "Invalid view id: %d", _id);
+		BGFX_CHECK_HANDLE("setUniform", s_ctx->m_uniformHandle, _handle);
 		s_ctx->setViewUniform(_id, _handle, _value, _num);
 	}
 
 	void setFrameUniform(UniformHandle _handle, const void* _value, uint16_t _num)
 	{
+		BGFX_CHECK_HANDLE("setUniform", s_ctx->m_uniformHandle, _handle);
 		s_ctx->setViewUniform(UINT16_MAX, _handle, _value, _num);
 	}
 

+ 29 - 4
src/bgfx_p.h

@@ -3289,6 +3289,23 @@ namespace bgfx
 		{
 			const UniformRef& uniform = getUniformRef(_handle);
 
+			const UniformFreq::Enum freq = UINT16_MAX == _id
+				? UniformFreq::Frame
+				: UniformFreq::View
+				;
+
+			BX_ASSERT(0 < uniform.m_refCount
+				, "Uniform reference count it 0 (handle %3d)!"
+				, _handle.idx
+				);
+			BX_ASSERT(uniform.m_freq == freq
+				, "Setting uniform per view, but uniform is created with different bgfx::UniformFreq::Enum!"
+				);
+			BX_ASSERT(_num == UINT16_MAX || uniform.m_num >= _num
+				, "Truncated uniform update. %d (max: %d)"
+				, _num, uniform.m_num
+				);
+
 			UniformCacheKey key =
 			{
 				.m_offset = 0,
@@ -4667,9 +4684,10 @@ namespace bgfx
 				}
 
 				PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
-				if (PredefinedUniform::Count == predefined && UniformType::End != UniformType::Enum(type) )
+				if (PredefinedUniform::Count == predefined
+				&&  UniformType::End != UniformType::Enum(type) )
 				{
-					uniforms[sr.m_num] = createUniform(name, UniformFreq::Draw, UniformType::Enum(type), num);
+					uniforms[sr.m_num] = createUniform(name, UniformFreq::Count, UniformType::Enum(type), num);
 					sr.m_num++;
 				}
 			}
@@ -5354,7 +5372,11 @@ namespace bgfx
 				const uint32_t oldsize = g_uniformTypeSize[uniform.m_type];
 				const uint32_t newsize = g_uniformTypeSize[_type];
 
-				uniform.m_freq = _freq; // Ignore shader created uniforms, and use UniformFreq when user creates uniform.
+				if (UniformFreq::Count != _freq)
+				{
+					// Ignore shader created uniforms, and use UniformFreq when user creates uniform.
+					uniform.m_freq = _freq;
+				}
 
 				if (oldsize < newsize
 				||  uniform.m_num < _num)
@@ -5390,7 +5412,10 @@ namespace bgfx
 			UniformRef& uniform = m_uniformRef[handle.idx];
 			uniform.m_name.set(_name);
 			uniform.m_refCount = 1;
-			uniform.m_freq = _freq;
+			uniform.m_freq = UniformFreq::Count == _freq
+				? UniformFreq::Draw
+				: _freq
+				;
 			uniform.m_type = _type;
 			uniform.m_num  = _num;