mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
239 lines
9.8 KiB
239 lines
9.8 KiB
|
14 years ago
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
6 years ago
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
|
||
|
5 years ago
|
2020, 2021 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
13 years ago
|
|
||
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||
|
|
copy of this software and associated documentation files (the "Software"),
|
||
|
|
to deal in the Software without restriction, including without limitation
|
||
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
||
|
|
Software is furnished to do so, subject to the following conditions:
|
||
|
|
|
||
|
|
The above copyright notice and this permission notice shall be included
|
||
|
|
in all copies or substantial portions of the Software.
|
||
|
|
|
||
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||
|
|
DEALINGS IN THE SOFTWARE.
|
||
|
14 years ago
|
*/
|
||
|
|
|
||
|
5 years ago
|
#include "FlatGL.h"
|
||
|
14 years ago
|
|
||
|
8 years ago
|
#include <Corrade/Containers/EnumSet.hpp>
|
||
|
7 years ago
|
#include <Corrade/Containers/Reference.h>
|
||
|
12 years ago
|
#include <Corrade/Utility/Resource.h>
|
||
|
14 years ago
|
|
||
|
8 years ago
|
#include "Magnum/GL/Context.h"
|
||
|
|
#include "Magnum/GL/Extensions.h"
|
||
|
|
#include "Magnum/GL/Shader.h"
|
||
|
|
#include "Magnum/GL/Texture.h"
|
||
|
7 years ago
|
#include "Magnum/Math/Color.h"
|
||
|
|
#include "Magnum/Math/Matrix3.h"
|
||
|
|
#include "Magnum/Math/Matrix4.h"
|
||
|
14 years ago
|
|
||
|
8 years ago
|
#include "Magnum/Shaders/Implementation/CreateCompatibilityShader.h"
|
||
|
12 years ago
|
|
||
|
14 years ago
|
namespace Magnum { namespace Shaders {
|
||
|
14 years ago
|
|
||
|
|
namespace {
|
||
|
6 years ago
|
enum: Int { TextureUnit = 0 };
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
template<UnsignedInt dimensions> FlatGL<dimensions>::FlatGL(const Flags flags): _flags(flags) {
|
||
|
6 years ago
|
CORRADE_ASSERT(!(flags & Flag::TextureTransformation) || (flags & Flag::Textured),
|
||
|
5 years ago
|
"Shaders::FlatGL: texture transformation enabled but the shader is not textured", );
|
||
|
6 years ago
|
|
||
|
11 years ago
|
#ifdef MAGNUM_BUILD_STATIC
|
||
|
|
/* Import resources on static build, if not already */
|
||
|
5 years ago
|
if(!Utility::Resource::hasGroup("MagnumShadersGL"))
|
||
|
11 years ago
|
importShaderResources();
|
||
|
|
#endif
|
||
|
5 years ago
|
Utility::Resource rs("MagnumShadersGL");
|
||
|
14 years ago
|
|
||
|
|
#ifndef MAGNUM_TARGET_GLES
|
||
|
8 years ago
|
const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GL320, GL::Version::GL310, GL::Version::GL300, GL::Version::GL210});
|
||
|
14 years ago
|
#else
|
||
|
8 years ago
|
const GL::Version version = GL::Context::current().supportedVersion({GL::Version::GLES300, GL::Version::GLES200});
|
||
|
14 years ago
|
#endif
|
||
|
|
|
||
|
8 years ago
|
GL::Shader vert = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex);
|
||
|
|
GL::Shader frag = Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Fragment);
|
||
|
12 years ago
|
|
||
|
13 years ago
|
vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "")
|
||
|
7 years ago
|
.addSource(flags & Flag::VertexColor ? "#define VERTEX_COLOR\n" : "")
|
||
|
6 years ago
|
.addSource(flags & Flag::TextureTransformation ? "#define TEXTURE_TRANSFORMATION\n" : "")
|
||
|
6 years ago
|
.addSource(dimensions == 2 ? "#define TWO_DIMENSIONS\n" : "#define THREE_DIMENSIONS\n")
|
||
|
6 years ago
|
#ifndef MAGNUM_TARGET_GLES2
|
||
|
|
.addSource(flags >= Flag::InstancedObjectId ? "#define INSTANCED_OBJECT_ID\n" : "")
|
||
|
|
#endif
|
||
|
6 years ago
|
.addSource(flags & Flag::InstancedTransformation ? "#define INSTANCED_TRANSFORMATION\n" : "")
|
||
|
|
.addSource(flags >= Flag::InstancedTextureOffset ? "#define INSTANCED_TEXTURE_OFFSET\n" : "")
|
||
|
13 years ago
|
.addSource(rs.get("generic.glsl"))
|
||
|
6 years ago
|
.addSource(rs.get("Flat.vert"));
|
||
|
13 years ago
|
frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "")
|
||
|
8 years ago
|
.addSource(flags & Flag::AlphaMask ? "#define ALPHA_MASK\n" : "")
|
||
|
7 years ago
|
.addSource(flags & Flag::VertexColor ? "#define VERTEX_COLOR\n" : "")
|
||
|
7 years ago
|
#ifndef MAGNUM_TARGET_GLES2
|
||
|
|
.addSource(flags & Flag::ObjectId ? "#define OBJECT_ID\n" : "")
|
||
|
6 years ago
|
.addSource(flags >= Flag::InstancedObjectId ? "#define INSTANCED_OBJECT_ID\n" : "")
|
||
|
7 years ago
|
#endif
|
||
|
|
.addSource(rs.get("generic.glsl"))
|
||
|
13 years ago
|
.addSource(rs.get("Flat.frag"));
|
||
|
12 years ago
|
|
||
|
8 years ago
|
CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag}));
|
||
|
12 years ago
|
|
||
|
12 years ago
|
attachShaders({vert, frag});
|
||
|
13 years ago
|
|
||
|
7 years ago
|
/* ES3 has this done in the shader directly and doesn't even provide
|
||
|
|
bindFragmentDataLocation() */
|
||
|
7 years ago
|
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES2)
|
||
|
14 years ago
|
#ifndef MAGNUM_TARGET_GLES
|
||
|
8 years ago
|
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::explicit_attrib_location>(version))
|
||
|
14 years ago
|
#endif
|
||
|
13 years ago
|
{
|
||
|
14 years ago
|
bindAttributeLocation(Position::Location, "position");
|
||
|
7 years ago
|
if(flags & Flag::Textured)
|
||
|
|
bindAttributeLocation(TextureCoordinates::Location, "textureCoordinates");
|
||
|
|
if(flags & Flag::VertexColor)
|
||
|
|
bindAttributeLocation(Color3::Location, "vertexColor"); /* Color4 is the same */
|
||
|
7 years ago
|
#ifndef MAGNUM_TARGET_GLES2
|
||
|
|
if(flags & Flag::ObjectId) {
|
||
|
|
bindFragmentDataLocation(ColorOutput, "color");
|
||
|
|
bindFragmentDataLocation(ObjectIdOutput, "objectId");
|
||
|
|
}
|
||
|
6 years ago
|
if(flags >= Flag::InstancedObjectId)
|
||
|
|
bindAttributeLocation(ObjectId::Location, "instanceObjectId");
|
||
|
6 years ago
|
#endif
|
||
|
6 years ago
|
if(flags & Flag::InstancedTransformation)
|
||
|
|
bindAttributeLocation(TransformationMatrix::Location, "instancedTransformationMatrix");
|
||
|
|
if(flags >= Flag::InstancedTextureOffset)
|
||
|
|
bindAttributeLocation(TextureOffset::Location, "instancedTextureOffset");
|
||
|
14 years ago
|
}
|
||
|
7 years ago
|
#endif
|
||
|
14 years ago
|
|
||
|
13 years ago
|
CORRADE_INTERNAL_ASSERT_OUTPUT(link());
|
||
|
14 years ago
|
|
||
|
14 years ago
|
#ifndef MAGNUM_TARGET_GLES
|
||
|
8 years ago
|
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::explicit_uniform_location>(version))
|
||
|
14 years ago
|
#endif
|
||
|
13 years ago
|
{
|
||
|
9 years ago
|
_transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");
|
||
|
6 years ago
|
if(flags & Flag::TextureTransformation)
|
||
|
|
_textureMatrixUniform = uniformLocation("textureMatrix");
|
||
|
9 years ago
|
_colorUniform = uniformLocation("color");
|
||
|
8 years ago
|
if(flags & Flag::AlphaMask) _alphaMaskUniform = uniformLocation("alphaMask");
|
||
|
7 years ago
|
#ifndef MAGNUM_TARGET_GLES2
|
||
|
|
if(flags & Flag::ObjectId) _objectIdUniform = uniformLocation("objectId");
|
||
|
|
#endif
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
|
#ifndef MAGNUM_TARGET_GLES
|
||
|
8 years ago
|
if(!GL::Context::current().isExtensionSupported<GL::Extensions::ARB::shading_language_420pack>(version))
|
||
|
13 years ago
|
#endif
|
||
|
|
{
|
||
|
6 years ago
|
if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureUnit);
|
||
|
14 years ago
|
}
|
||
|
13 years ago
|
|
||
|
|
/* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */
|
||
|
|
#ifdef MAGNUM_TARGET_GLES
|
||
|
6 years ago
|
setTransformationProjectionMatrix(MatrixTypeFor<dimensions, Float>{Math::IdentityInit});
|
||
|
|
if(flags & Flag::TextureTransformation)
|
||
|
|
setTextureMatrix(Matrix3{Math::IdentityInit});
|
||
|
7 years ago
|
setColor(Magnum::Color4{1.0f});
|
||
|
8 years ago
|
if(flags & Flag::AlphaMask) setAlphaMask(0.5f);
|
||
|
7 years ago
|
/* Object ID is zero by default */
|
||
|
13 years ago
|
#endif
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setTransformationProjectionMatrix(const MatrixTypeFor<dimensions, Float>& matrix) {
|
||
|
7 years ago
|
setUniform(_transformationProjectionMatrixUniform, matrix);
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setTextureMatrix(const Matrix3& matrix) {
|
||
|
6 years ago
|
CORRADE_ASSERT(_flags & Flag::TextureTransformation,
|
||
|
5 years ago
|
"Shaders::FlatGL::setTextureMatrix(): the shader was not created with texture transformation enabled", *this);
|
||
|
6 years ago
|
setUniform(_textureMatrixUniform, matrix);
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setColor(const Magnum::Color4& color) {
|
||
|
7 years ago
|
setUniform(_colorUniform, color);
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::bindTexture(GL::Texture2D& texture) {
|
||
|
8 years ago
|
CORRADE_ASSERT(_flags & Flag::Textured,
|
||
|
5 years ago
|
"Shaders::FlatGL::bindTexture(): the shader was not created with texturing enabled", *this);
|
||
|
6 years ago
|
texture.bind(TextureUnit);
|
||
|
12 years ago
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setAlphaMask(Float mask) {
|
||
|
8 years ago
|
CORRADE_ASSERT(_flags & Flag::AlphaMask,
|
||
|
5 years ago
|
"Shaders::FlatGL::setAlphaMask(): the shader was not created with alpha mask enabled", *this);
|
||
|
8 years ago
|
setUniform(_alphaMaskUniform, mask);
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
#ifndef MAGNUM_TARGET_GLES2
|
||
|
5 years ago
|
template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setObjectId(UnsignedInt id) {
|
||
|
7 years ago
|
CORRADE_ASSERT(_flags & Flag::ObjectId,
|
||
|
5 years ago
|
"Shaders::FlatGL::setObjectId(): the shader was not created with object ID enabled", *this);
|
||
|
7 years ago
|
setUniform(_objectIdUniform, id);
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
5 years ago
|
template class FlatGL<2>;
|
||
|
|
template class FlatGL<3>;
|
||
|
14 years ago
|
|
||
|
8 years ago
|
namespace Implementation {
|
||
|
|
|
||
|
5 years ago
|
Debug& operator<<(Debug& debug, const FlatGLFlag value) {
|
||
|
|
debug << "Shaders::FlatGL::Flag" << Debug::nospace;
|
||
|
7 years ago
|
|
||
|
8 years ago
|
switch(value) {
|
||
|
|
/* LCOV_EXCL_START */
|
||
|
5 years ago
|
#define _c(v) case FlatGLFlag::v: return debug << "::" #v;
|
||
|
8 years ago
|
_c(Textured)
|
||
|
|
_c(AlphaMask)
|
||
|
7 years ago
|
_c(VertexColor)
|
||
|
6 years ago
|
_c(TextureTransformation)
|
||
|
7 years ago
|
#ifndef MAGNUM_TARGET_GLES2
|
||
|
|
_c(ObjectId)
|
||
|
6 years ago
|
_c(InstancedObjectId)
|
||
|
7 years ago
|
#endif
|
||
|
6 years ago
|
_c(InstancedTransformation)
|
||
|
|
_c(InstancedTextureOffset)
|
||
|
8 years ago
|
#undef _c
|
||
|
|
/* LCOV_EXCL_STOP */
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
return debug << "(" << Debug::nospace << reinterpret_cast<void*>(UnsignedByte(value)) << Debug::nospace << ")";
|
||
|
8 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
Debug& operator<<(Debug& debug, const FlatGLFlags value) {
|
||
|
|
return Containers::enumSetDebugOutput(debug, value, "Shaders::FlatGL::Flags{}", {
|
||
|
|
FlatGLFlag::Textured,
|
||
|
|
FlatGLFlag::AlphaMask,
|
||
|
|
FlatGLFlag::VertexColor,
|
||
|
|
FlatGLFlag::InstancedTextureOffset, /* Superset of TextureTransformation */
|
||
|
|
FlatGLFlag::TextureTransformation,
|
||
|
7 years ago
|
#ifndef MAGNUM_TARGET_GLES2
|
||
|
5 years ago
|
FlatGLFlag::InstancedObjectId, /* Superset of ObjectId */
|
||
|
|
FlatGLFlag::ObjectId,
|
||
|
7 years ago
|
#endif
|
||
|
5 years ago
|
FlatGLFlag::InstancedTransformation});
|
||
|
8 years ago
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
}}
|