Skip to content

Commit

Permalink
Primitives: MSVC 2015 is this what makes you throw up?
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Feb 16, 2020
1 parent 961bd26 commit e09b741
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 50 deletions.
40 changes: 29 additions & 11 deletions src/Magnum/Primitives/Crosshair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,50 @@ namespace Magnum { namespace Primitives {

namespace {

constexpr Vector2 Positions2D[]{
{-1.0f, 0.0f}, {1.0f, 0.0f},
{ 0.0f, -1.0f}, {0.0f, 1.0f}
/* Can't be just an array of Vector2 but has to be a struct, because then MSVC
2015 fails with an assertion like
Trade::MeshData: attribute 0 [0x7ffd6c4fb290:0x7ffd6c4fb338] is not contained in passed vertexData array [0x7ffd6c4fb170:0x7ffd6c4fb218]
which leads me to believe that it will make two copies of the data, one for
the MeshAttributeData constructor and one for the MeshData constructor. Same
for the Cube and Square primitive. Interestingly enough, this isn't a
problem for index arrays (maybe because those are integers and not constexpr
classes?). Also not a problem for MSVC 2017 and up. */
constexpr struct Vertex2D {
Vector2 position;
} Vertices2D[]{
{{-1.0f, 0.0f}}, {{1.0f, 0.0f}},
{{ 0.0f, -1.0f}}, {{0.0f, 1.0f}}
};
constexpr Vector3 Positions3D[]{
{-1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f},
{ 0.0f, -1.0f, 0.0f}, {0.0f, 1.0f, 0.0f},
{ 0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f}
constexpr struct Vertex3D {
Vector3 position;
} Vertices3D[]{
{{-1.0f, 0.0f, 0.0f}}, {{1.0f, 0.0f, 0.0f}},
{{ 0.0f, -1.0f, 0.0f}}, {{0.0f, 1.0f, 0.0f}},
{{ 0.0f, 0.0f, -1.0f}}, {{0.0f, 0.0f, 1.0f}}
};

constexpr Trade::MeshAttributeData Attributes2D[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position, Containers::arrayView(Positions2D)}
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Containers::stridedArrayView(Vertices2D, &Vertices2D[0].position,
Containers::arraySize(Vertices2D), sizeof(Vertex2D))}
};
constexpr Trade::MeshAttributeData Attributes3D[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position, Containers::arrayView(Positions3D)}
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Containers::stridedArrayView(Vertices3D, &Vertices3D[0].position,
Containers::arraySize(Vertices3D), sizeof(Vertex3D))}
};

}

Trade::MeshData crosshair2D() {
return Trade::MeshData{MeshPrimitive::Lines, {}, Positions2D,
return Trade::MeshData{MeshPrimitive::Lines, {}, Vertices2D,
Trade::meshAttributeDataNonOwningArray(Attributes2D)};
}

Trade::MeshData crosshair3D() {
return Trade::MeshData{MeshPrimitive::Lines, {}, Positions3D,
return Trade::MeshData{MeshPrimitive::Lines, {}, Vertices3D,
Trade::meshAttributeDataNonOwningArray(Attributes3D)};
}

Expand Down
64 changes: 37 additions & 27 deletions src/Magnum/Primitives/Cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ Trade::MeshData cubeSolid() {

namespace {

constexpr Vector3 VerticesSolidStrip[]{
/* Can't be just an array of Vector3 because MSVC 2015 is special. See
Crosshair.cpp for details. */
constexpr struct VertexSolidStrip {
Vector3 position;
} VerticesSolidStrip[]{
/* Sources:
https://twitter.com/Donzanoid/status/436843034966507520
http://www.asmcommunity.net/forums/topic/?id=6284#post-45209
Expand All @@ -119,24 +123,25 @@ constexpr Vector3 VerticesSolidStrip[]{
|F \|
2---3
*/
{ 1.0f, 1.0f, 1.0f}, /* 3 */
{-1.0f, 1.0f, 1.0f}, /* 2 */
{ 1.0f, -1.0f, 1.0f}, /* 6 */
{-1.0f, -1.0f, 1.0f}, /* 7 */
{-1.0f, -1.0f, -1.0f}, /* 4 */
{-1.0f, 1.0f, 1.0f}, /* 2 */
{-1.0f, 1.0f, -1.0f}, /* 0 */
{ 1.0f, 1.0f, 1.0f}, /* 3 */
{ 1.0f, 1.0f, -1.0f}, /* 1 */
{ 1.0f, -1.0f, 1.0f}, /* 6 */
{ 1.0f, -1.0f, -1.0f}, /* 5 */
{-1.0f, -1.0f, -1.0f}, /* 4 */
{ 1.0f, 1.0f, -1.0f}, /* 1 */
{-1.0f, 1.0f, -1.0f} /* 0 */
{{ 1.0f, 1.0f, 1.0f}}, /* 3 */
{{-1.0f, 1.0f, 1.0f}}, /* 2 */
{{ 1.0f, -1.0f, 1.0f}}, /* 6 */
{{-1.0f, -1.0f, 1.0f}}, /* 7 */
{{-1.0f, -1.0f, -1.0f}}, /* 4 */
{{-1.0f, 1.0f, 1.0f}}, /* 2 */
{{-1.0f, 1.0f, -1.0f}}, /* 0 */
{{ 1.0f, 1.0f, 1.0f}}, /* 3 */
{{ 1.0f, 1.0f, -1.0f}}, /* 1 */
{{ 1.0f, -1.0f, 1.0f}}, /* 6 */
{{ 1.0f, -1.0f, -1.0f}}, /* 5 */
{{-1.0f, -1.0f, -1.0f}}, /* 4 */
{{ 1.0f, 1.0f, -1.0f}}, /* 1 */
{{-1.0f, 1.0f, -1.0f}} /* 0 */
};
constexpr Trade::MeshAttributeData AttributesSolidStrip[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Containers::stridedArrayView(VerticesSolidStrip)}
Containers::stridedArrayView(VerticesSolidStrip, &VerticesSolidStrip[0].position,
Containers::arraySize(VerticesSolidStrip), sizeof(VertexSolidStrip))}
};

}
Expand All @@ -155,20 +160,25 @@ constexpr UnsignedShort IndicesWireframe[]{
1, 5, 2, 6, /* +X */
0, 4, 3, 7 /* -X */
};
constexpr Vector3 VerticesWireframe[]{
{-1.0f, -1.0f, 1.0f},
{ 1.0f, -1.0f, 1.0f},
{ 1.0f, 1.0f, 1.0f},
{-1.0f, 1.0f, 1.0f},

{-1.0f, -1.0f, -1.0f},
{ 1.0f, -1.0f, -1.0f},
{ 1.0f, 1.0f, -1.0f},
{-1.0f, 1.0f, -1.0f}
/* Can't be just an array of Vector3 because MSVC 2015 is special. See
Crosshair.cpp for details. */
constexpr struct VertexWireframe {
Vector3 position;
} VerticesWireframe[]{
{{-1.0f, -1.0f, 1.0f}},
{{ 1.0f, -1.0f, 1.0f}},
{{ 1.0f, 1.0f, 1.0f}},
{{-1.0f, 1.0f, 1.0f}},

{{-1.0f, -1.0f, -1.0f}},
{{ 1.0f, -1.0f, -1.0f}},
{{ 1.0f, 1.0f, -1.0f}},
{{-1.0f, 1.0f, -1.0f}}
};
constexpr Trade::MeshAttributeData AttributesWireframe[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Containers::stridedArrayView(VerticesWireframe)}
Containers::stridedArrayView(VerticesWireframe, &VerticesWireframe[0].position,
Containers::arraySize(VerticesWireframe), sizeof(VertexWireframe))}
};

}
Expand Down
34 changes: 22 additions & 12 deletions src/Magnum/Primitives/Square.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ namespace Magnum { namespace Primitives {

namespace {

constexpr Vector2 VerticesSolid[] {
{ 1.0f, -1.0f},
{ 1.0f, 1.0f},
{-1.0f, -1.0f},
{-1.0f, 1.0f}
/* Can't be just an array of Vector2 because MSVC 2015 is special. See
Crosshair.cpp for details. */
constexpr struct VertexSolid {
Vector2 position;
} VerticesSolid[] {
{{ 1.0f, -1.0f}},
{{ 1.0f, 1.0f}},
{{-1.0f, -1.0f}},
{{-1.0f, 1.0f}}
};
constexpr struct VertexSolidTextureCoords {
Vector2 position;
Expand All @@ -50,7 +54,8 @@ constexpr struct VertexSolidTextureCoords {
};
constexpr Trade::MeshAttributeData AttributesSolid[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Containers::stridedArrayView(VerticesSolid)}
Containers::stridedArrayView(VerticesSolid, &VerticesSolid[0].position,
Containers::arraySize(VerticesSolid), sizeof(VertexSolid))},
};
constexpr Trade::MeshAttributeData AttributesSolidTextureCoords[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Expand Down Expand Up @@ -78,15 +83,20 @@ Trade::MeshData squareSolid(const SquareTextureCoords textureCoords) {

namespace {

constexpr Vector2 VerticesWireframe[]{
{-1.0f, -1.0f},
{ 1.0f, -1.0f},
{ 1.0f, 1.0f},
{-1.0f, 1.0f}
/* Can't be just an array of Vector2 because MSVC 2015 is special. See
Crosshair.cpp for details. */
constexpr struct VertexWireframe {
Vector2 position;
} VerticesWireframe[]{
{{-1.0f, -1.0f}},
{{ 1.0f, -1.0f}},
{{ 1.0f, 1.0f}},
{{-1.0f, 1.0f}}
};
constexpr Trade::MeshAttributeData AttributesWireframe[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Containers::stridedArrayView(VerticesWireframe)}
Containers::stridedArrayView(VerticesWireframe, &VerticesWireframe[0].position,
Containers::arraySize(VerticesWireframe), sizeof(VertexWireframe))}
};

}
Expand Down

0 comments on commit e09b741

Please sign in to comment.