Skip to content

Commit 7855d09

Browse files
committed
minor refactore
1 parent 74b3535 commit 7855d09

7 files changed

Lines changed: 94 additions & 118 deletions

File tree

gamedata/shaders/city_mesh.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ uniform isamplerBuffer tex_2; // palette indices table
1010
// attributes
1111
in vec3 in_pos0;
1212
in vec3 in_texcoord0;
13-
in float in_color0; // remap index
14-
in float in_color1; // transparency flag
13+
in int in_color0; // remap index
14+
in int in_color1; // transparency flag
1515

1616
// pass to fragment shader
1717
out vec3 Texcoord;

gamedata/shaders/sprites.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ uniform mat4 view_projection_matrix;
88
// attributes
99
in vec3 in_pos0;
1010
in vec2 in_texcoord0;
11-
in float in_color0; // palette index
11+
in int in_color0; // palette index
1212

1313
// pass to fragment shader
1414
out vec2 Texcoord;
1515
out vec3 Position;
16-
flat out float PaletteIndex;
16+
flat out int PaletteIndex;
1717

1818
// entry point
1919
void main()
@@ -37,7 +37,7 @@ uniform sampler2D tex_3; // palettes table
3737
// passed from vertex shader
3838
in vec2 Texcoord;
3939
in vec3 Position;
40-
flat in float PaletteIndex;
40+
flat in int PaletteIndex;
4141

4242
// result
4343
out vec4 FinalColor;
@@ -53,7 +53,7 @@ void main()
5353
discard;
5454

5555
// fetch pixel color
56-
vec4 pixelColor = texelFetch(tex_3, ivec2(int(pal_color), int(PaletteIndex)), 0);
56+
vec4 pixelColor = texelFetch(tex_3, ivec2(int(pal_color), PaletteIndex), 0);
5757
pixelColor.a = 1.0f;
5858
FinalColor = clamp(pixelColor, 0.0f, 1.0f);
5959
}

src/GameDefs.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ struct CityVertex3D_Format: public VertexFormat
212212
inline void Setup()
213213
{
214214
this->mDataStride = Sizeof_CityVertex3D;
215-
this->SetAttribute(eVertexAttribute_Position0, offsetof(TVertexType, mPosition));
216-
this->SetAttribute(eVertexAttribute_Color0, eVertexAttributeSemantics_PaletteIndex, offsetof(TVertexType, mRemap));
217-
this->SetAttribute(eVertexAttribute_Color1, eVertexAttributeSemantics_PaletteIndex, offsetof(TVertexType, mTransparency));
218-
this->SetAttribute(eVertexAttribute_Texcoord0, eVertexAttributeSemantics_Texcoord3d, offsetof(TVertexType, mTexcoord));
215+
this->SetAttribute(eVertexAttribute_Position0, eVertexAttributeFormat_3F, offsetof(TVertexType, mPosition));
216+
this->SetAttribute(eVertexAttribute_Color0, eVertexAttributeFormat_1US, offsetof(TVertexType, mRemap));
217+
this->SetAttribute(eVertexAttribute_Color1, eVertexAttributeFormat_1US, offsetof(TVertexType, mTransparency));
218+
this->SetAttribute(eVertexAttribute_Texcoord0, eVertexAttributeFormat_3F, offsetof(TVertexType, mTexcoord));
219219
}
220220
};
221221

@@ -264,9 +264,9 @@ struct SpriteVertex3D_Format: public VertexFormat
264264
inline void Setup()
265265
{
266266
this->mDataStride = Sizeof_SpriteVertex3D;
267-
this->SetAttribute(eVertexAttribute_Position0, offsetof(TVertexType, mPosition));
268-
this->SetAttribute(eVertexAttribute_Texcoord0, offsetof(TVertexType, mTexcoord));
269-
this->SetAttribute(eVertexAttribute_Color0, eVertexAttributeSemantics_PaletteIndex, offsetof(TVertexType, mClutIndex));
267+
this->SetAttribute(eVertexAttribute_Position0, eVertexAttributeFormat_3F, offsetof(TVertexType, mPosition));
268+
this->SetAttribute(eVertexAttribute_Texcoord0, eVertexAttributeFormat_2F, offsetof(TVertexType, mTexcoord));
269+
this->SetAttribute(eVertexAttribute_Color0, eVertexAttributeFormat_1US, offsetof(TVertexType, mClutIndex));
270270
}
271271
};
272272

src/GraphicsDefs.h

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,17 @@ enum eTextureUnit
324324
decl_enum_strings(eTextureUnit);
325325

326326
// standard vertex attributes
327-
enum eVertexAttributeSemantics
328-
{
329-
eVertexAttributeSemantics_Position, // 3 floats
330-
eVertexAttributeSemantics_Normal, // 3 floats
331-
eVertexAttributeSemantics_Color, // 4 unsigned bytes
332-
eVertexAttributeSemantics_Texcoord, // 2 floats
333-
eVertexAttributeSemantics_Position2d, // 2 floats
334-
eVertexAttributeSemantics_Texcoord3d, // 3 floats
335-
eVertexAttributeSemantics_PaletteIndex, // 1 unsigned short
336-
eVertexAttributeSemantics_Unknown
327+
enum eVertexAttributeFormat
328+
{
329+
eVertexAttributeFormat_2F, // 2 floats
330+
eVertexAttributeFormat_3F, // 3 floats
331+
eVertexAttributeFormat_4UB, // 4 unsigned bytes
332+
eVertexAttributeFormat_1US, // 1 unsigned short
333+
eVertexAttributeFormat_2US, // 2 unsigned shorts
334+
eVertexAttributeFormat_Unknown
337335
};
338336

339-
decl_enum_strings(eVertexAttributeSemantics);
337+
decl_enum_strings(eVertexAttributeFormat);
340338

341339
enum eVertexAttribute
342340
{
@@ -354,63 +352,33 @@ enum eVertexAttribute
354352

355353
decl_enum_strings(eVertexAttribute);
356354

357-
// Get semantics of vertex attribute
358-
// @param attribute: Attribute identifier
359-
inline eVertexAttributeSemantics GetAttributeSemantics(eVertexAttribute attribute)
360-
{
361-
switch (attribute)
362-
{
363-
case eVertexAttribute_Position0:
364-
case eVertexAttribute_Position1:
365-
return eVertexAttributeSemantics_Position;
366-
367-
case eVertexAttribute_Normal0:
368-
case eVertexAttribute_Normal1:
369-
return eVertexAttributeSemantics_Normal;
370-
371-
case eVertexAttribute_Texcoord0:
372-
case eVertexAttribute_Texcoord1:
373-
return eVertexAttributeSemantics_Texcoord;
374-
375-
case eVertexAttribute_Color0:
376-
case eVertexAttribute_Color1:
377-
return eVertexAttributeSemantics_Color;
378-
}
379-
debug_assert(false);
380-
return eVertexAttributeSemantics_Unknown;
381-
}
382-
383355
// Get number of component for vertex attribute
384-
// @param attributeSemantics: Attribute semantics
385-
inline unsigned int GetAttributeComponentCount(eVertexAttributeSemantics attributeSemantics)
356+
// @param attributeFormat: Format identifier
357+
inline unsigned int GetAttributeComponentCount(eVertexAttributeFormat attributeFormat)
386358
{
387-
switch (attributeSemantics)
359+
switch (attributeFormat)
388360
{
389-
case eVertexAttributeSemantics_Normal: return 3;
390-
case eVertexAttributeSemantics_Position: return 3;
391-
case eVertexAttributeSemantics_Color: return 4;
392-
case eVertexAttributeSemantics_Texcoord: return 2;
393-
case eVertexAttributeSemantics_Position2d: return 2;
394-
case eVertexAttributeSemantics_Texcoord3d: return 3;
395-
case eVertexAttributeSemantics_PaletteIndex: return 1;
361+
case eVertexAttributeFormat_2F: return 2;
362+
case eVertexAttributeFormat_3F: return 3;
363+
case eVertexAttributeFormat_4UB: return 4;
364+
case eVertexAttributeFormat_1US: return 1;
365+
case eVertexAttributeFormat_2US: return 2;
396366
}
397367
debug_assert(false);
398368
return 0;
399369
}
400370

401371
// Get vertex attribute size in bytes
402-
// @param attributeSemantics: Attribute semantics
403-
inline unsigned int GetAttributeSizeBytes(eVertexAttributeSemantics attributeSemantics)
372+
// @param attributeFormat: Format identifier
373+
inline unsigned int GetAttributeSizeBytes(eVertexAttributeFormat attributeFormat)
404374
{
405-
switch (attributeSemantics)
375+
switch (attributeFormat)
406376
{
407-
case eVertexAttributeSemantics_Normal: return sizeof(float) * 3;
408-
case eVertexAttributeSemantics_Position: return sizeof(float) * 3;
409-
case eVertexAttributeSemantics_Color: return sizeof(unsigned int);
410-
case eVertexAttributeSemantics_Texcoord: return sizeof(float) * 2;
411-
case eVertexAttributeSemantics_Position2d: return sizeof(float) * 2;
412-
case eVertexAttributeSemantics_Texcoord3d: return sizeof(float) * 3;
413-
case eVertexAttributeSemantics_PaletteIndex: return sizeof(unsigned short);
377+
case eVertexAttributeFormat_2F: return 2 * sizeof(float);
378+
case eVertexAttributeFormat_3F: return 3 * sizeof(float);
379+
case eVertexAttributeFormat_4UB: return 4 * sizeof(unsigned char);
380+
case eVertexAttributeFormat_1US: return 1 * sizeof(unsigned short);
381+
case eVertexAttributeFormat_2US: return 2 * sizeof(unsigned short);
414382
}
415383
debug_assert(false);
416384
return 0;
@@ -424,28 +392,34 @@ struct VertexFormat
424392

425393
// Enable attribute or modify data offset for enabled attribute
426394
// @param attribute: Attribute identifier
395+
// @param attributeFormat: Attribute format format
427396
// @param dataOffset: Attribute data offset in bytes within buffer
428-
inline void SetAttribute(eVertexAttribute attribute, unsigned int dataOffset)
397+
inline void SetAttribute(eVertexAttribute attribute, eVertexAttributeFormat attributeFormat, unsigned int dataOffset)
429398
{
430399
debug_assert(attribute < eVertexAttribute_COUNT);
400+
debug_assert(attributeFormat != eVertexAttributeFormat_Unknown);
431401
mAttributes[attribute].mDataOffset = dataOffset;
432-
mAttributes[attribute].mSemantics = GetAttributeSemantics(attribute);
402+
mAttributes[attribute].mFormat = attributeFormat;
403+
mAttributes[attribute].mNormalized = false;
433404
}
434-
// @param forceSemantics: Override default semantics for specified attribute
435-
inline void SetAttribute(eVertexAttribute attribute, eVertexAttributeSemantics forceSemantics, unsigned int dataOffset)
405+
inline void SetAttributeNormalized(eVertexAttribute attribute, bool isNormalized = true)
436406
{
437407
debug_assert(attribute < eVertexAttribute_COUNT);
438-
mAttributes[attribute].mDataOffset = dataOffset;
439-
mAttributes[attribute].mSemantics = forceSemantics;
408+
mAttributes[attribute].mNormalized = isNormalized;
440409
}
441410
public:
442411
struct SingleAttribute
443412
{
444413
public:
445414
SingleAttribute() = default;
446415
public:
416+
eVertexAttributeFormat mFormat = eVertexAttributeFormat_Unknown;
447417
unsigned int mDataOffset = 0;
448-
eVertexAttributeSemantics mSemantics = eVertexAttributeSemantics_Unknown;
418+
419+
// attribute normalization - opengl specific
420+
// if set to true, it indicates that values stored in an integer format are
421+
// to be mapped to the range [-1,1] (for signed values) or [0,1] (for unsigned values) when they are accessed and converted to floating point
422+
bool mNormalized = false;
449423
};
450424
SingleAttribute mAttributes[eVertexAttribute_COUNT];
451425
unsigned int mDataStride = 0; // common to all attributes
@@ -471,10 +445,11 @@ struct Vertex3D_Format: public VertexFormat
471445
inline void Setup()
472446
{
473447
this->mDataStride = Sizeof_Vertex3D;
474-
this->SetAttribute(eVertexAttribute_Position0, offsetof(TVertexType, mPosition));
475-
this->SetAttribute(eVertexAttribute_Normal0, offsetof(TVertexType, mNormal));
476-
this->SetAttribute(eVertexAttribute_Texcoord0, offsetof(TVertexType, mTexcoord));
477-
this->SetAttribute(eVertexAttribute_Color0, offsetof(TVertexType, mColor));
448+
this->SetAttribute(eVertexAttribute_Position0, eVertexAttributeFormat_3F, offsetof(TVertexType, mPosition));
449+
this->SetAttribute(eVertexAttribute_Normal0, eVertexAttributeFormat_3F, offsetof(TVertexType, mNormal));
450+
this->SetAttribute(eVertexAttribute_Texcoord0, eVertexAttributeFormat_2F, offsetof(TVertexType, mTexcoord));
451+
this->SetAttribute(eVertexAttribute_Color0, eVertexAttributeFormat_4UB, offsetof(TVertexType, mColor));
452+
this->SetAttributeNormalized(eVertexAttribute_Color0);
478453
}
479454
};
480455

@@ -497,10 +472,11 @@ struct Vertex2D_Format: public VertexFormat
497472
inline void Setup()
498473
{
499474
this->mDataStride = Sizeof_Vertex2D;
500-
this->SetAttribute(eVertexAttribute_Texcoord0, offsetof(TVertexType, mTexcoord));
501-
this->SetAttribute(eVertexAttribute_Color0, offsetof(TVertexType, mColor));
475+
this->SetAttribute(eVertexAttribute_Texcoord0, eVertexAttributeFormat_2F, offsetof(TVertexType, mTexcoord));
476+
this->SetAttribute(eVertexAttribute_Color0, eVertexAttributeFormat_4UB, offsetof(TVertexType, mColor));
477+
this->SetAttributeNormalized(eVertexAttribute_Color0);
502478
// force semantics for pos0 attribute - expect 2 floats per vertex
503-
this->SetAttribute(eVertexAttribute_Position0, eVertexAttributeSemantics_Position2d, offsetof(TVertexType, mPosition));
479+
this->SetAttribute(eVertexAttribute_Position0, eVertexAttributeFormat_2F, offsetof(TVertexType, mPosition));
504480
}
505481
};
506482

@@ -523,8 +499,9 @@ struct Vertex3D_Debug_Format: public VertexFormat
523499
inline void Setup()
524500
{
525501
this->mDataStride = Sizeof_Vertex3D_Debug;
526-
this->SetAttribute(eVertexAttribute_Position0, offsetof(TVertexType, mPosition));
527-
this->SetAttribute(eVertexAttribute_Color0, offsetof(TVertexType, mColor));
502+
this->SetAttribute(eVertexAttribute_Position0, eVertexAttributeFormat_3F, offsetof(TVertexType, mPosition));
503+
this->SetAttribute(eVertexAttribute_Color0, eVertexAttributeFormat_4UB, offsetof(TVertexType, mColor));
504+
this->SetAttributeNormalized(eVertexAttribute_Color0);
528505
}
529506
};
530507

src/GraphicsDevice.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -949,29 +949,35 @@ void GraphicsDevice::SetupVertexAttributes(const VertexFormat& streamDefinition)
949949
}
950950

951951
const auto& attribute = streamDefinition.mAttributes[iattribute];
952-
if (attribute.mSemantics == eVertexAttributeSemantics_Unknown)
952+
if (attribute.mFormat == eVertexAttributeFormat_Unknown)
953953
{
954954
debug_assert(false);
955955
continue;
956956
}
957957

958-
unsigned int numComponents = GetAttributeComponentCount(attribute.mSemantics);
958+
unsigned int numComponents = GetAttributeComponentCount(attribute.mFormat);
959959
if (numComponents == 0)
960960
{
961961
debug_assert(numComponents > 0);
962962
continue;
963963
}
964964

965-
GLboolean normalizeData = GL_FALSE;
966-
if (attribute.mSemantics == eVertexAttributeSemantics_Color)
965+
GLenum dataType = GetAttributeDataTypeGL(attribute.mFormat);
966+
967+
if (dataType == GL_FLOAT || attribute.mNormalized)
967968
{
968-
normalizeData = GL_TRUE;
969+
// set attribute location
970+
::glVertexAttribPointer(currentProgram->mAttributes[iattribute], numComponents, dataType,
971+
attribute.mNormalized ? GL_TRUE : GL_FALSE,
972+
streamDefinition.mDataStride, BUFFER_OFFSET(attribute.mDataOffset + streamDefinition.mBaseOffset));
973+
glCheckError();
974+
}
975+
else
976+
{
977+
::glVertexAttribIPointer(currentProgram->mAttributes[iattribute], numComponents, dataType,
978+
streamDefinition.mDataStride, BUFFER_OFFSET(attribute.mDataOffset + streamDefinition.mBaseOffset));
979+
glCheckError();
969980
}
970-
GLenum dataType = GetAttributeDataTypeGL(attribute.mSemantics);
971-
// set attribute location
972-
::glVertexAttribPointer(currentProgram->mAttributes[iattribute], numComponents, dataType, normalizeData,
973-
streamDefinition.mDataStride, BUFFER_OFFSET(attribute.mDataOffset + streamDefinition.mBaseOffset));
974-
glCheckError();
975981
}
976982
}
977983

src/OpenGLDefs.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,15 @@ inline GLenum GetTextureDataTypeGL(eTextureFormat textureFormat)
135135
return 0;
136136
}
137137

138-
inline GLenum GetAttributeDataTypeGL(eVertexAttributeSemantics attributeSemantics)
138+
inline GLenum GetAttributeDataTypeGL(eVertexAttributeFormat attributeFormat)
139139
{
140-
switch (attributeSemantics)
140+
switch (attributeFormat)
141141
{
142-
case eVertexAttributeSemantics_Position:
143-
case eVertexAttributeSemantics_Normal:
144-
case eVertexAttributeSemantics_Texcoord:
145-
case eVertexAttributeSemantics_Position2d:
146-
case eVertexAttributeSemantics_Texcoord3d:
147-
return GL_FLOAT;
148-
case eVertexAttributeSemantics_Color:
149-
return GL_UNSIGNED_BYTE;
150-
case eVertexAttributeSemantics_PaletteIndex:
151-
return GL_UNSIGNED_SHORT;
142+
case eVertexAttributeFormat_2F: return GL_FLOAT;
143+
case eVertexAttributeFormat_3F: return GL_FLOAT;
144+
case eVertexAttributeFormat_4UB: return GL_UNSIGNED_BYTE;
145+
case eVertexAttributeFormat_1US: return GL_UNSIGNED_SHORT;
146+
case eVertexAttributeFormat_2US: return GL_UNSIGNED_SHORT;
152147
}
153148
debug_assert(false);
154149
return GL_UNSIGNED_BYTE;

src/enums_impl.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,14 @@ impl_enum_strings(eTextureUnit)
174174
{eTextureUnit_15, "tex_15"},
175175
};
176176

177-
impl_enum_strings(eVertexAttributeSemantics)
178-
{
179-
{eVertexAttributeSemantics_Position, "position"},
180-
{eVertexAttributeSemantics_Normal, "normal"},
181-
{eVertexAttributeSemantics_Color, "color"},
182-
{eVertexAttributeSemantics_Texcoord, "texcoord"},
183-
{eVertexAttributeSemantics_Position2d, "position2d"},
184-
{eVertexAttributeSemantics_Texcoord3d, "texcoord3d"},
185-
{eVertexAttributeSemantics_PaletteIndex, "paletteIndex"},
186-
{eVertexAttributeSemantics_Unknown, "unknown"},
177+
impl_enum_strings(eVertexAttributeFormat)
178+
{
179+
{eVertexAttributeFormat_2F, "2f"},
180+
{eVertexAttributeFormat_3F, "3f"},
181+
{eVertexAttributeFormat_4UB, "4ub"},
182+
{eVertexAttributeFormat_1US, "1us"},
183+
{eVertexAttributeFormat_2US, "2us"},
184+
{eVertexAttributeFormat_Unknown, "unknown"},
187185
};
188186

189187
impl_enum_strings(eVertexAttribute)

0 commit comments

Comments
 (0)