From 49dde269a6435bc0c0e72177984284cd879c3465 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 22 Jan 2015 16:27:17 -0800 Subject: [PATCH] experimenting with linear space vs srgb space --- libraries/fbx/src/FBXReader.cpp | 3 +- libraries/gpu/src/gpu/Format.h | 8 +++-- libraries/gpu/src/gpu/GLBackend.cpp | 2 +- libraries/gpu/src/gpu/GLBackend.h | 2 +- libraries/gpu/src/gpu/GLBackendTexture.cpp | 37 ++++++++++++++++---- libraries/gpu/src/gpu/Texture.cpp | 6 +++- libraries/gpu/src/gpu/Texture.h | 11 +++--- libraries/render-utils/src/GeometryCache.cpp | 4 +-- libraries/render-utils/src/TextRenderer.cpp | 2 +- libraries/render-utils/src/TextureCache.cpp | 35 ++++++++++-------- libraries/render-utils/src/TextureCache.h | 3 +- 11 files changed, 75 insertions(+), 38 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 89a46f2be9..3015de52ff 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -272,8 +272,7 @@ FBXNode parseBinaryFBXNode(QDataStream& in, int& position) { position += nameLength; for (quint32 i = 0; i < propertyCount; i++) { - QVariant var = parseBinaryFBXProperty(in, position); - node.properties.append(var); + node.properties.append(parseBinaryFBXProperty(in, position)); } while (endOffset > position) { diff --git a/libraries/gpu/src/gpu/Format.h b/libraries/gpu/src/gpu/Format.h index 35876ffffa..6274c294ec 100644 --- a/libraries/gpu/src/gpu/Format.h +++ b/libraries/gpu/src/gpu/Format.h @@ -93,15 +93,13 @@ static const int DIMENSION_COUNT[NUM_DIMENSIONS] = { // Provide information on how to use the element enum Semantic { RAW = 0, // used as RAW memory + RGB, RGBA, BGRA, XYZ, XYZW, - POS_XYZ, - POS_XYZW, QUAT, - DIR_XYZ, UV, INDEX, //used by index buffer of a mesh PART, // used by part buffer of a mesh @@ -109,6 +107,10 @@ enum Semantic { DEPTH, // Depth buffer DEPTH_STENCIL, // Depth Stencil buffer + SRGB, + SRGBA, + SBGRA, + NUM_SEMANTICS, }; diff --git a/libraries/gpu/src/gpu/GLBackend.cpp b/libraries/gpu/src/gpu/GLBackend.cpp index e2f923f369..519b20eca7 100644 --- a/libraries/gpu/src/gpu/GLBackend.cpp +++ b/libraries/gpu/src/gpu/GLBackend.cpp @@ -472,7 +472,7 @@ void GLBackend::do_setUniformTexture(Batch& batch, uint32 paramOffset) { GLuint slot = batch._params[paramOffset + 1]._uint; TexturePointer uniformTexture = batch._textures.get(batch._params[paramOffset + 0]._uint); - GLuint to = getTextureID(*uniformTexture); + GLuint to = getTextureID(uniformTexture); glActiveTexture(GL_TEXTURE0 + slot); glBindTexture(GL_TEXTURE_2D, to); diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index a15de87afd..a3f3495853 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -55,7 +55,7 @@ public: ~GLTexture(); }; static void syncGPUObject(const Texture& texture); - static GLuint getTextureID(const Texture& texture); + static GLuint getTextureID(const TexturePointer& texture); static const int MAX_NUM_ATTRIBUTES = Stream::NUM_INPUT_SLOTS; static const int MAX_NUM_INPUT_BUFFERS = 16; diff --git a/libraries/gpu/src/gpu/GLBackendTexture.cpp b/libraries/gpu/src/gpu/GLBackendTexture.cpp index d416b5b7c9..075fd7ad71 100755 --- a/libraries/gpu/src/gpu/GLBackendTexture.cpp +++ b/libraries/gpu/src/gpu/GLBackendTexture.cpp @@ -94,10 +94,13 @@ public: switch(srcFormat.getSemantic()) { case gpu::BGRA: + case gpu::SBGRA: texel.format = GL_BGRA; break; case gpu::RGB: case gpu::RGBA: + case gpu::SRGB: + case gpu::SRGBA: default: break; }; @@ -109,6 +112,12 @@ public: case gpu::RGBA: texel.internalFormat = GL_RGBA; break; + case gpu::SRGB: + texel.internalFormat = GL_SRGB; + break; + case gpu::SRGBA: + texel.internalFormat = GL_SRGB_ALPHA; + break; default: qDebug() << "Unknown combination of texel format"; } @@ -170,6 +179,10 @@ public: case gpu::RGBA: texel.internalFormat = GL_RGB; break; + case gpu::SRGB: + case gpu::SRGBA: + texel.internalFormat = GL_SRGB; // standard 2.2 gamma correction color + break; default: qDebug() << "Unknown combination of texel format"; } @@ -187,6 +200,12 @@ public: case gpu::RGBA: texel.internalFormat = GL_RGBA; break; + case gpu::SRGB: + texel.internalFormat = GL_SRGB; + break; + case gpu::SRGBA: + texel.internalFormat = GL_SRGB_ALPHA; // standard 2.2 gamma correction color + break; default: qDebug() << "Unknown combination of texel format"; } @@ -295,13 +314,17 @@ void GLBackend::syncGPUObject(const Texture& texture) { -GLuint GLBackend::getTextureID(const Texture& texture) { - GLBackend::syncGPUObject(texture); - GLTexture* object = Backend::getGPUObject(texture); - if (object) { - return object->_texture; - } else { +GLuint GLBackend::getTextureID(const TexturePointer& texture) { + if (texture) { + GLBackend::syncGPUObject(*texture); + GLTexture* object = Backend::getGPUObject(*texture); + if (object) { + return object->_texture; + } else { + return 0; + } + } else { return 0; - } + } } diff --git a/libraries/gpu/src/gpu/Texture.cpp b/libraries/gpu/src/gpu/Texture.cpp index 736545e758..1ba0e831f4 100755 --- a/libraries/gpu/src/gpu/Texture.cpp +++ b/libraries/gpu/src/gpu/Texture.cpp @@ -23,6 +23,10 @@ Texture::Pixels::Pixels(const Element& format, Size size, const Byte* bytes) : Texture::Pixels::~Pixels() { } +void Texture::Storage::assignTexture(Texture* texture) { + _texture = texture; +} + Stamp Texture::Storage::getStamp(uint16 level) const { PixelsPointer mip = getMip(level); if (mip) { @@ -111,7 +115,7 @@ Texture* Texture::create(Type type, const Element& texelFormat, uint16 width, ui Texture* Texture::createFromStorage(Storage* storage) { Texture* tex = new Texture(); tex->_storage.reset(storage); - storage->_texture = tex; + storage->assignTexture(tex); return tex; } diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index 8b8fcf6c04..f50be9e14e 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -32,10 +32,7 @@ public: typedef QSharedPointer< Pixels > PixelsPointer; class Storage { - Texture* _texture; - std::vector _mips; public: - Storage() {} virtual ~Storage() {} virtual void reset(); @@ -45,7 +42,13 @@ public: virtual bool allocateMip(uint16 level); virtual bool assignMipData(uint16 level, const Element& format, Size size, const Byte* bytes); virtual bool isMipAvailable(uint16 level) const; - + + protected: + Texture* _texture; + std::vector _mips; + + virtual void assignTexture(Texture* tex); + friend class Texture; }; diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index 3806599cbf..0d9fb6f60e 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -2068,7 +2068,7 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) { int channelNum = 0; networkMesh._vertexFormat = gpu::Stream::FormatPointer(new gpu::Stream::Format()); - networkMesh._vertexFormat->setAttribute(gpu::Stream::POSITION, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::POS_XYZ), 0); + networkMesh._vertexFormat->setAttribute(gpu::Stream::POSITION, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); if (mesh.normals.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::NORMAL, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); if (mesh.tangents.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::TANGENT, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); if (mesh.colors.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::COLOR, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::RGB)); @@ -2102,7 +2102,7 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) { int channelNum = 0; networkMesh._vertexFormat = gpu::Stream::FormatPointer(new gpu::Stream::Format()); - networkMesh._vertexFormat->setAttribute(gpu::Stream::POSITION, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::POS_XYZ)); + networkMesh._vertexFormat->setAttribute(gpu::Stream::POSITION, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); if (mesh.normals.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::NORMAL, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); if (mesh.tangents.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::TANGENT, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); if (mesh.colors.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::COLOR, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::RGB)); diff --git a/libraries/render-utils/src/TextRenderer.cpp b/libraries/render-utils/src/TextRenderer.cpp index 2c677a330a..ae7523a817 100644 --- a/libraries/render-utils/src/TextRenderer.cpp +++ b/libraries/render-utils/src/TextRenderer.cpp @@ -167,7 +167,7 @@ TextRenderer::TextRenderer(const Properties& properties) : _glyphsStream(new gpu::BufferStream()), _numGlyphsBatched(0) { - _glyphsStreamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::POS_XYZ), 0); + _glyphsStreamFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0); const int NUM_POS_COORDS = 2; const int VERTEX_TEXCOORD_OFFSET = NUM_POS_COORDS * sizeof(float); _glyphsStreamFormat->setAttribute(gpu::Stream::TEXCOORD, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV), VERTEX_TEXCOORD_OFFSET); diff --git a/libraries/render-utils/src/TextureCache.cpp b/libraries/render-utils/src/TextureCache.cpp index cfd3218e5c..2de40143d5 100644 --- a/libraries/render-utils/src/TextureCache.cpp +++ b/libraries/render-utils/src/TextureCache.cpp @@ -531,13 +531,17 @@ void NetworkTexture::setImage(const QImage& image, bool translucent, const QColo finishedLoading(true); imageLoaded(image); - if (image.hasAlphaChannel()) { - _gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA), image.width(), image.height())); - _gpuTexture->assignStoredMip(0, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::BGRA), image.byteCount(), image.constBits()); - _gpuTexture->autoGenerateMips(-1); - } else { - _gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(gpu::Element(gpu::VEC3, gpu::UINT8, gpu::RGB), image.width(), image.height())); - _gpuTexture->assignStoredMip(0, gpu::Element(gpu::VEC3, gpu::UINT8, gpu::RGB), image.byteCount(), image.constBits()); + if ((_width > 0) && (_height > 0)) { + bool isLinearRGB = (_type == NORMAL_TEXTURE) || (_type == EMISSIVE_TEXTURE); + + gpu::Element formatGPU = gpu::Element(gpu::VEC3, gpu::UINT8, (isLinearRGB ? gpu::RGB : gpu::SRGB)); + gpu::Element formatMip = gpu::Element(gpu::VEC3, gpu::UINT8, (isLinearRGB ? gpu::RGB : gpu::SRGB)); + if (image.hasAlphaChannel()) { + formatGPU = gpu::Element(gpu::VEC4, gpu::UINT8, (isLinearRGB ? gpu::RGBA : gpu::SRGBA)); + formatMip = gpu::Element(gpu::VEC4, gpu::UINT8, (isLinearRGB ? gpu::BGRA : gpu::SBGRA)); + } + _gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(formatGPU, image.width(), image.height())); + _gpuTexture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits()); _gpuTexture->autoGenerateMips(-1); } } @@ -567,16 +571,17 @@ QSharedPointer DilatableNetworkTexture::getDilatedTexture(float dilatio path.addEllipse(QPointF(_image.width() / 2.0, _image.height() / 2.0), radius, radius); painter.fillPath(path, Qt::black); painter.end(); - + + gpu::Element formatGPU = gpu::Element(gpu::VEC3, gpu::UINT8, (_type == NORMAL_TEXTURE ? gpu::RGB : gpu::SRGB)); + gpu::Element formatMip = gpu::Element(gpu::VEC3, gpu::UINT8, (_type == NORMAL_TEXTURE ? gpu::RGB : gpu::SRGB)); if (dilatedImage.hasAlphaChannel()) { - texture->_gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA), dilatedImage.width(), dilatedImage.height())); - texture->_gpuTexture->assignStoredMip(0, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::BGRA), dilatedImage.byteCount(), dilatedImage.constBits()); - texture->_gpuTexture->autoGenerateMips(-1); - } else { - texture->_gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(gpu::Element(gpu::VEC3, gpu::UINT8, gpu::RGB), dilatedImage.width(), dilatedImage.height())); - texture->_gpuTexture->assignStoredMip(0, gpu::Element(gpu::VEC3, gpu::UINT8, gpu::RGB), dilatedImage.byteCount(), dilatedImage.constBits()); - texture->_gpuTexture->autoGenerateMips(-1); + formatGPU = gpu::Element(gpu::VEC4, gpu::UINT8, (_type == NORMAL_TEXTURE ? gpu::RGBA : gpu::SRGBA)); + formatMip = gpu::Element(gpu::VEC4, gpu::UINT8, (_type == NORMAL_TEXTURE ? gpu::BGRA : gpu::BGRA)); } + texture->_gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(formatGPU, dilatedImage.width(), dilatedImage.height())); + texture->_gpuTexture->assignStoredMip(0, formatMip, dilatedImage.byteCount(), dilatedImage.constBits()); + texture->_gpuTexture->autoGenerateMips(-1); + } _dilatedTextures.insert(dilation, texture); diff --git a/libraries/render-utils/src/TextureCache.h b/libraries/render-utils/src/TextureCache.h index 6ce1fc80fc..4754991a11 100644 --- a/libraries/render-utils/src/TextureCache.h +++ b/libraries/render-utils/src/TextureCache.h @@ -168,8 +168,9 @@ protected: virtual void imageLoaded(const QImage& image); -private: TextureType _type; + +private: bool _translucent; QColor _averageColor; int _width;