opened
LMCrashy
username_0
Therefore, the values aren't uploaded at the right place.

Maybe the GpuConstantDefinitionMap should use an unordered map ?
Shared params & Uniform buffer
issue
created
LMCrashy
username_0
Actually, there is another issue, at least in GL3+.

Of course if one wants to share the same uniform buffer across different shaders it nees the buffer to use the "shared" layout to have the same offsets everywhere.

The offset for each uniform is not "guessable", one need to retrieve it from the program handle.
This pic from nSight show that 

![image](https://user-images.githubusercontent.com/28775797/75606617-e641a700-5aee-11ea-85b1-d1a40c368928.png)

Here, BetaRay is at offset 48 which is not 4*4(sunDirection) + 4*4 (sunColor) + 4(sunPower) = 36. "sunPower" is having a 4*4bytes size, but other floats from my constant buffer( I've only displayed a small part of it) are indeed using a 4bytes size.

Previously, there was this code in the GL3+RS to retrieve the offsets

```
// Get active block parameter properties.
GpuConstantDefinitionIterator sharedParamDef = blockSharedParams->getConstantDefinitionIterator();
std::vector<const char*> sharedParamNames;
for (; sharedParamDef.current() != sharedParamDef.end(); sharedParamDef.moveNext())
{
	sharedParamNames.push_back(sharedParamDef.current()->first.c_str());
}

std::vector<GLuint> uniformParamIndices(sharedParamNames.size());
std::vector<GLint> uniformParamOffsets(sharedParamNames.size());

OGRE_CHECK_GL_ERROR(glGetUniformIndices(mGLProgramHandle, sharedParamNames.size(), &sharedParamNames[0], &uniformParamIndices[0]));
//FIXME debug this (see stdout)
OGRE_CHECK_GL_ERROR(glGetActiveUniformsiv(mGLProgramHandle, uniformParamIndices.size(), &uniformParamIndices[0], GL_UNIFORM_OFFSET, &uniformParamOffsets[0]));

GpuNamedConstants& consts = const_cast<GpuNamedConstants&>(blockSharedParams->getConstantDefinitions());
MapIterator<GpuConstantDefinitionMap> sharedParamDefMut(consts.map);
```

But in current implementation, nothing retrieves the actual location of uniforms in the buffer.
comment
created
paroj
username_1
you are required to use `std140` for shared_params  at the moment

https://www.khronos.org/opengl/wiki/Interface_Block_(GLSL)#Memory_layout
comment
created
LMCrashy
username_0
I've also tried with std140 layout qualifier before, this doesn't help with the offset issue:

![image](https://user-images.githubusercontent.com/28775797/75607523-50ab1500-5af8-11ea-9f50-9c4fe0f59d3b.png)

Sometimes a float uses 4bytes, sometimes 16bytes, vec3 uses 16bytes here and not 9.
This is not predictable and should be retrieved from the gpu program
comment
created
paroj
username_1
no, it is specified as per the link above. e.g. vec3 is required to be 16byte aligned. We just dont adhere to the rules in ogre.

The real question is whether its easier to implement the rules or query the driver.
comment
created
paroj
username_1
#1477 should yield the above offsets and fix the upload iteration
comment
created
LMCrashy
username_0
Thanks for your time and clarification. I was suspecting an aligment issue per uniform but not this way, it makes sense now.

Unfortunatly, this patch gives the same logical index for BetaMie and BetaRay: 48

Wouln't it be easier to accumulate offset in a member value when adding a new constant and calculate the next aligned position ? Using the size of the mXXXConstants lists doesn't give an accurate information of previous values alignment.

I'm working on a fix done this way, I'll tell if it works.
comment
closed
paroj
username_1
issue
created
paroj
username_1
yeah, you are right. As of now, we are discarding any previous padding when calculating the offset.
comment
reopened
paroj
username_1
Therefore, the values aren't uploaded at the right place.

Maybe the GpuConstantDefinitionMap should use an unordered map, or the offset should be calculated using the physical index instead.
Shared params & Uniform buffer
issue
closed
paroj
username_1
issue
created
LMCrashy
username_0
Woops, 
My fix proposal wasn't properly handling matrices.

I reworked it (again) in this commit : https://github.com/username_0/ogre/commit/ab74f5c931b585c52518d3adbbe036d0a653e234

Tested it against this Uniform block 
```
layout(std140) uniform TestAlignment
{
	mat3 _mat33 ; 	//0
	mat4x3 _mat43 ; //48
	float _float ; 	//112
	mat3x4 _mat34 ; //128
	vec3 _vec3;	//176
	vec2 _vec2; 	//192
	mat4 _mat44; 	//208
	float _float1; 	//272
	float _float2; 	//276
	mat3 _mat33_2; 	//288
	mat2 _mat22 ;	//336
};
```
comment
created
paroj
username_1
can you create a PR with this fix?
comment