|
| 1 | +#include "stdafx.h" |
| 2 | +#include "glBufferUtils.h" |
| 3 | + |
| 4 | +namespace glBufferUtils |
| 5 | +{ |
| 6 | + |
| 7 | + void CreateBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable, bool bIndexBuffer) |
| 8 | +{ |
| 9 | + GLenum usage = bImmutable ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW; |
| 10 | + GLenum target = bIndexBuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER; |
| 11 | + |
| 12 | + glGenBuffers(1, pBuffer); |
| 13 | + glBindBuffer(target, *pBuffer); |
| 14 | + CHK_GL(glBufferData(target, DataSize, pData, usage)); |
| 15 | +} |
| 16 | + |
| 17 | +void CreateVertexBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable) |
| 18 | +{ |
| 19 | + return CreateBuffer(pBuffer, pData, DataSize, bImmutable, false); |
| 20 | +} |
| 21 | + |
| 22 | +void CreateIndexBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable) |
| 23 | +{ |
| 24 | + return CreateBuffer(pBuffer, pData, DataSize, bImmutable, true); |
| 25 | +} |
| 26 | + |
| 27 | +GLenum VertexSizeList[] = |
| 28 | +{ |
| 29 | + 1, // D3DDECLTYPE_FLOAT1 |
| 30 | + 2, // D3DDECLTYPE_FLOAT2 |
| 31 | + 3, // D3DDECLTYPE_FLOAT3 |
| 32 | + 4, // D3DDECLTYPE_FLOAT4 |
| 33 | + 4, // D3DDECLTYPE_D3DCOLOR |
| 34 | + 4, // D3DDECLTYPE_UBYTE4 |
| 35 | + 2, // D3DDECLTYPE_SHORT2 |
| 36 | + 4, // D3DDECLTYPE_SHORT4 |
| 37 | + 4, // D3DDECLTYPE_UBYTE4N |
| 38 | + 2, // D3DDECLTYPE_SHORT2N |
| 39 | + 4, // D3DDECLTYPE_SHORT4N |
| 40 | + 2, // D3DDECLTYPE_USHORT2N |
| 41 | + 4, // D3DDECLTYPE_USHORT4N |
| 42 | + 1, // D3DDECLTYPE_UDEC3 |
| 43 | + 1, // D3DDECLTYPE_DEC3N |
| 44 | + 2, // D3DDECLTYPE_FLOAT16_2 |
| 45 | + 4 // D3DDECLTYPE_FLOAT16_4 |
| 46 | +}; |
| 47 | + |
| 48 | +GLenum VertexTypeList[] = |
| 49 | +{ |
| 50 | + GL_FLOAT, // D3DDECLTYPE_FLOAT1 |
| 51 | + GL_FLOAT, // D3DDECLTYPE_FLOAT2 |
| 52 | + GL_FLOAT, // D3DDECLTYPE_FLOAT3 |
| 53 | + GL_FLOAT, // D3DDECLTYPE_FLOAT4 |
| 54 | + GL_UNSIGNED_BYTE, // D3DDECLTYPE_D3DCOLOR |
| 55 | + GL_UNSIGNED_BYTE, // D3DDECLTYPE_UBYTE4 |
| 56 | + GL_SHORT, // D3DDECLTYPE_SHORT2 |
| 57 | + GL_SHORT, // D3DDECLTYPE_SHORT4 |
| 58 | + GL_UNSIGNED_BYTE, // D3DDECLTYPE_UBYTE4N |
| 59 | + GL_SHORT, // D3DDECLTYPE_SHORT2N |
| 60 | + GL_SHORT, // D3DDECLTYPE_SHORT4N |
| 61 | + GL_UNSIGNED_SHORT, // D3DDECLTYPE_USHORT2N |
| 62 | + GL_UNSIGNED_SHORT, // D3DDECLTYPE_USHORT4N |
| 63 | + GL_INT_2_10_10_10_REV, // D3DDECLTYPE_UDEC3 |
| 64 | + GL_INT_2_10_10_10_REV, // D3DDECLTYPE_DEC3N |
| 65 | + GL_HALF_FLOAT, // D3DDECLTYPE_FLOAT16_2 |
| 66 | + GL_HALF_FLOAT // D3DDECLTYPE_FLOAT16_4 |
| 67 | +}; |
| 68 | + |
| 69 | +GLenum VertexNormalizedList[] = |
| 70 | +{ |
| 71 | + GL_FALSE, // D3DDECLTYPE_FLOAT1 |
| 72 | + GL_FALSE, // D3DDECLTYPE_FLOAT2 |
| 73 | + GL_FALSE, // D3DDECLTYPE_FLOAT3 |
| 74 | + GL_FALSE, // D3DDECLTYPE_FLOAT4 |
| 75 | + GL_TRUE, // D3DDECLTYPE_D3DCOLOR |
| 76 | + GL_FALSE, // D3DDECLTYPE_UBYTE4 |
| 77 | + GL_FALSE, // D3DDECLTYPE_SHORT2 |
| 78 | + GL_FALSE, // D3DDECLTYPE_SHORT4 |
| 79 | + GL_TRUE, // D3DDECLTYPE_UBYTE4N |
| 80 | + GL_TRUE, // D3DDECLTYPE_SHORT2N |
| 81 | + GL_TRUE, // D3DDECLTYPE_SHORT4N |
| 82 | + GL_TRUE, // D3DDECLTYPE_USHORT2N |
| 83 | + GL_TRUE, // D3DDECLTYPE_USHORT4N |
| 84 | + GL_FALSE, // D3DDECLTYPE_UDEC3 |
| 85 | + GL_TRUE, // D3DDECLTYPE_DEC3N |
| 86 | + GL_FALSE, // D3DDECLTYPE_FLOAT16_2 |
| 87 | + GL_FALSE // D3DDECLTYPE_FLOAT16_4 |
| 88 | +}; |
| 89 | + |
| 90 | +GLenum VertexTypeSizeList[] = |
| 91 | +{ |
| 92 | + sizeof(GLfloat), // D3DDECLTYPE_FLOAT1 |
| 93 | + sizeof(GLfloat), // D3DDECLTYPE_FLOAT2 |
| 94 | + sizeof(GLfloat), // D3DDECLTYPE_FLOAT3 |
| 95 | + sizeof(GLfloat), // D3DDECLTYPE_FLOAT4 |
| 96 | + sizeof(GLubyte), // D3DDECLTYPE_D3DCOLOR |
| 97 | + sizeof(GLubyte), // D3DDECLTYPE_UBYTE4 |
| 98 | + sizeof(GLshort), // D3DDECLTYPE_SHORT2 |
| 99 | + sizeof(GLshort), // D3DDECLTYPE_SHORT4 |
| 100 | + sizeof(GLubyte), // D3DDECLTYPE_UBYTE4N |
| 101 | + sizeof(GLshort), // D3DDECLTYPE_SHORT2N |
| 102 | + sizeof(GLshort), // D3DDECLTYPE_SHORT4N |
| 103 | + sizeof(GLushort), // D3DDECLTYPE_USHORT2N |
| 104 | + sizeof(GLushort), // D3DDECLTYPE_USHORT4N |
| 105 | + sizeof(GLuint), // D3DDECLTYPE_UDEC3 |
| 106 | + sizeof(GLint), // D3DDECLTYPE_DEC3N |
| 107 | + sizeof(GLhalf), // D3DDECLTYPE_FLOAT16_2 |
| 108 | + sizeof(GLhalf) // D3DDECLTYPE_FLOAT16_4 |
| 109 | +}; |
| 110 | + |
| 111 | +GLuint VertexUsageList[] = |
| 112 | +{ |
| 113 | + 3, // D3DDECLUSAGE_POSITION |
| 114 | + -1, // D3DDECLUSAGE_BLENDWEIGHT |
| 115 | + -1, // D3DDECLUSAGE_BLENDINDICES |
| 116 | + 5, // D3DDECLUSAGE_NORMAL |
| 117 | + -1, // D3DDECLUSAGE_PSIZE |
| 118 | + 8, // D3DDECLUSAGE_TEXCOORD |
| 119 | + 4, // D3DDECLUSAGE_TANGENT |
| 120 | + 6, // D3DDECLUSAGE_BINORMAL |
| 121 | + -1, // D3DDECLUSAGE_TESSFACTOR |
| 122 | + -1, // D3DDECLUSAGE_POSITIONT |
| 123 | + 0, // D3DDECLUSAGE_COLOR |
| 124 | + 7, // D3DDECLUSAGE_FOG |
| 125 | + -1, // D3DDECLUSAGE_DEPTH |
| 126 | + -1, // D3DDECLUSAGE_SAMPLE |
| 127 | +}; |
| 128 | + |
| 129 | +GLsizei GetDeclVertexSize(const D3DVERTEXELEMENT9* decl) |
| 130 | +{ |
| 131 | + GLsizei size = 0; |
| 132 | + for (int i = 0; i < MAXD3DDECLLENGTH; ++i) |
| 133 | + { |
| 134 | + const D3DVERTEXELEMENT9 &desc = decl[i]; |
| 135 | + |
| 136 | + if (desc.Stream == 0xFF) |
| 137 | + break; |
| 138 | + |
| 139 | + size += VertexSizeList[desc.Type] * VertexTypeSizeList[desc.Type]; |
| 140 | + } |
| 141 | + return size; |
| 142 | +} |
| 143 | + |
| 144 | +void ConvertVertexDeclaration(const D3DVERTEXELEMENT9* decl, GLuint vao) |
| 145 | +{ |
| 146 | + CHK_GL(glBindVertexArray(vao)); |
| 147 | + |
| 148 | + GLsizei stride = GetDeclVertexSize(decl); |
| 149 | + for (int i = 0; i < MAXD3DDECLLENGTH; ++i) |
| 150 | + { |
| 151 | + const D3DVERTEXELEMENT9 &desc = decl[i]; |
| 152 | + |
| 153 | + if (desc.Stream == 0xFF) |
| 154 | + break; |
| 155 | + |
| 156 | + GLuint location = VertexUsageList[desc.Usage]; |
| 157 | + GLint size = VertexSizeList[desc.Type]; |
| 158 | + GLenum type = VertexTypeList[desc.Type]; |
| 159 | + GLboolean normalized = VertexNormalizedList[desc.Type]; |
| 160 | + |
| 161 | + if (location < 0) |
| 162 | + continue; // Unsupported |
| 163 | + |
| 164 | + location += desc.UsageIndex; |
| 165 | + CHK_GL(glVertexAttribFormat(location, size, type, normalized, desc.Offset)); |
| 166 | + CHK_GL(glVertexAttribBinding(location, desc.Stream)); |
| 167 | + CHK_GL(glEnableVertexAttribArray(location)); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +GLsizei GetFVFVertexSize(u32 FVF) |
| 172 | +{ |
| 173 | + GLsizei offset = 0; |
| 174 | + |
| 175 | + // Position attribute |
| 176 | + if (FVF & D3DFVF_XYZRHW) |
| 177 | + offset += sizeof(Fvector4); |
| 178 | + else if (FVF & D3DFVF_XYZ) |
| 179 | + offset += sizeof(Fvector); |
| 180 | + |
| 181 | + // Diffuse color attribute |
| 182 | + if (FVF & D3DFVF_DIFFUSE) |
| 183 | + offset += sizeof(u32); |
| 184 | + |
| 185 | + // Specular color attribute |
| 186 | + if (FVF & D3DFVF_SPECULAR) |
| 187 | + offset += sizeof(u32); |
| 188 | + |
| 189 | + // Texture coordinates |
| 190 | + for (u32 i = 0; i < (FVF & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT; i++) |
| 191 | + { |
| 192 | + u32 size = 2; |
| 193 | + if (FVF & D3DFVF_TEXCOORDSIZE1(i)) |
| 194 | + size = 1; |
| 195 | + if (FVF & D3DFVF_TEXCOORDSIZE3(i)) |
| 196 | + size = 3; |
| 197 | + if (FVF & D3DFVF_TEXCOORDSIZE4(i)) |
| 198 | + size = 4; |
| 199 | + |
| 200 | + offset += size * sizeof(float); |
| 201 | + } |
| 202 | + |
| 203 | + return offset; |
| 204 | +} |
| 205 | + |
| 206 | +void ConvertVertexDeclaration(u32 FVF, GLuint vao) |
| 207 | +{ |
| 208 | + CHK_GL(glBindVertexArray(vao)); |
| 209 | + |
| 210 | + GLsizei stride = GetFVFVertexSize(FVF); |
| 211 | + u32 offset = 0; |
| 212 | + |
| 213 | + // Position attribute |
| 214 | + if (FVF & D3DFVF_XYZRHW) |
| 215 | + { |
| 216 | + GLuint attrib = VertexUsageList[D3DDECLUSAGE_POSITION]; |
| 217 | + CHK_GL(glVertexAttribFormat(attrib, 4, GL_FLOAT, GL_FALSE, offset)); |
| 218 | + CHK_GL(glVertexAttribBinding(attrib, 0)); |
| 219 | + CHK_GL(glEnableVertexAttribArray(attrib)); |
| 220 | + offset += sizeof(Fvector4); |
| 221 | + } |
| 222 | + else if (FVF & D3DFVF_XYZ) |
| 223 | + { |
| 224 | + GLuint attrib = VertexUsageList[D3DDECLUSAGE_POSITION]; |
| 225 | + CHK_GL(glVertexAttribFormat(attrib, 3, GL_FLOAT, GL_FALSE, offset)); |
| 226 | + CHK_GL(glVertexAttribBinding(attrib, 0)); |
| 227 | + CHK_GL(glEnableVertexAttribArray(attrib)); |
| 228 | + offset += sizeof(Fvector); |
| 229 | + } |
| 230 | + |
| 231 | + // Diffuse color attribute |
| 232 | + if (FVF & D3DFVF_DIFFUSE) |
| 233 | + { |
| 234 | + GLuint attrib = VertexUsageList[D3DDECLUSAGE_COLOR]; |
| 235 | + CHK_GL(glVertexAttribFormat(attrib, 4, GL_UNSIGNED_BYTE, GL_TRUE, offset)); |
| 236 | + CHK_GL(glVertexAttribBinding(attrib, 0)); |
| 237 | + CHK_GL(glEnableVertexAttribArray(attrib)); |
| 238 | + offset += sizeof(u32); |
| 239 | + } |
| 240 | + |
| 241 | + // Specular color attribute |
| 242 | + if (FVF & D3DFVF_SPECULAR) |
| 243 | + { |
| 244 | + GLuint attrib = VertexUsageList[D3DDECLUSAGE_COLOR] + 1; |
| 245 | + CHK_GL(glVertexAttribFormat(attrib, 4, GL_UNSIGNED_BYTE, GL_TRUE, offset)); |
| 246 | + CHK_GL(glVertexAttribBinding(attrib, 0)); |
| 247 | + CHK_GL(glEnableVertexAttribArray(attrib)); |
| 248 | + offset += sizeof(u32); |
| 249 | + } |
| 250 | + |
| 251 | + // Texture coordinates |
| 252 | + for (u32 i = 0; i < (FVF & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT; i++) |
| 253 | + { |
| 254 | + GLuint attrib = VertexUsageList[D3DDECLUSAGE_TEXCOORD] + i; |
| 255 | + |
| 256 | + u32 size = 2; |
| 257 | + if (FVF & D3DFVF_TEXCOORDSIZE1(i)) |
| 258 | + size = 1; |
| 259 | + if (FVF & D3DFVF_TEXCOORDSIZE3(i)) |
| 260 | + size = 3; |
| 261 | + if (FVF & D3DFVF_TEXCOORDSIZE4(i)) |
| 262 | + size = 4; |
| 263 | + |
| 264 | + CHK_GL(glVertexAttribFormat(attrib, size, GL_FLOAT, GL_TRUE, offset)); |
| 265 | + CHK_GL(glVertexAttribBinding(attrib, 0)); |
| 266 | + CHK_GL(glEnableVertexAttribArray(attrib)); |
| 267 | + offset += size * sizeof(float); |
| 268 | + } |
| 269 | + |
| 270 | + VERIFY(stride == offset); |
| 271 | +} |
| 272 | + |
| 273 | +u32 GetDeclLength(const D3DVERTEXELEMENT9 *decl) |
| 274 | +{ |
| 275 | + const D3DVERTEXELEMENT9 *element; |
| 276 | + |
| 277 | + for (element = decl; element->Stream != 0xff; ++element); |
| 278 | + |
| 279 | + return element - decl; |
| 280 | +} |
| 281 | + |
| 282 | +}; |
0 commit comments