Commit cfaeaa9f by Geoff Lang

Refactor uniform array name parsing to a utility function.

BUG=angleproject:882 Change-Id: I00fd6d3cfaa107561cee5e4c82d3c60438052963 Reviewed-on: https://chromium-review.googlesource.com/265723Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarKenneth Russell <kbr@chromium.org> Tested-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent 5160ec11
...@@ -462,6 +462,37 @@ int VariableSortOrder(GLenum type) ...@@ -462,6 +462,37 @@ int VariableSortOrder(GLenum type)
} }
} }
std::string ParseUniformName(const std::string &name, size_t *outSubscript)
{
// Strip any trailing array operator and retrieve the subscript
size_t open = name.find_last_of('[');
size_t close = name.find_last_of(']');
bool hasIndex = (open != std::string::npos) && (close == name.length() - 1);
if (!hasIndex)
{
if (outSubscript)
{
*outSubscript = GL_INVALID_INDEX;
}
return name;
}
if (outSubscript)
{
int index = atoi(name.substr(open + 1).c_str());
if (index >= 0)
{
*outSubscript = index;
}
else
{
*outSubscript = GL_INVALID_INDEX;
}
}
return name.substr(0, open);
}
} }
#if !defined(ANGLE_ENABLE_WINDOWS_STORE) #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
......
...@@ -40,6 +40,10 @@ bool IsCubeMapTextureTarget(GLenum target); ...@@ -40,6 +40,10 @@ bool IsCubeMapTextureTarget(GLenum target);
size_t CubeMapTextureTargetToLayerIndex(GLenum target); size_t CubeMapTextureTargetToLayerIndex(GLenum target);
GLenum LayerIndexToCubeMapTextureTarget(size_t index); GLenum LayerIndexToCubeMapTextureTarget(size_t index);
// Parse the base uniform name and array index. Returns the base name of the uniform. outSubscript is
// set to GL_INVALID_INDEX if the provided name is not an array or the array index is invalid.
std::string ParseUniformName(const std::string &name, size_t *outSubscript);
bool IsTriangleMode(GLenum drawMode); bool IsTriangleMode(GLenum drawMode);
// [OpenGL ES 3.0.2] Section 2.3.1 page 14 // [OpenGL ES 3.0.2] Section 2.3.1 page 14
......
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// utilities_unittest.cpp: Unit tests for ANGLE's GL utility functions
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "common/utilities.h"
namespace
{
TEST(ParseUniformName, ArrayIndex)
{
size_t index;
EXPECT_EQ("foo", gl::ParseUniformName("foo[123]", &index));
EXPECT_EQ(123u, index);
EXPECT_EQ("bar", gl::ParseUniformName("bar[0]", &index));
EXPECT_EQ(0u, index);
}
TEST(ParseUniformName, NegativeArrayIndex)
{
size_t index;
EXPECT_EQ("foo", gl::ParseUniformName("foo[-1]", &index));
EXPECT_EQ(GL_INVALID_INDEX, index);
}
TEST(ParseUniformName, NoArrayIndex)
{
size_t index;
EXPECT_EQ("foo", gl::ParseUniformName("foo", &index));
EXPECT_EQ(GL_INVALID_INDEX, index);
}
TEST(ParseUniformName, NULLArrayIndex)
{
EXPECT_EQ("foo", gl::ParseUniformName("foo[10]", nullptr));
}
TEST(ParseUniformName, TrailingWhitespace)
{
size_t index;
EXPECT_EQ("foo ", gl::ParseUniformName("foo ", &index));
EXPECT_EQ(GL_INVALID_INDEX, index);
EXPECT_EQ("foo[10] ", gl::ParseUniformName("foo[10] ", &index));
EXPECT_EQ(GL_INVALID_INDEX, index);
}
}
...@@ -13,27 +13,6 @@ ...@@ -13,27 +13,6 @@
namespace rx namespace rx
{ {
namespace
{
unsigned int ParseAndStripArrayIndex(std::string* name)
{
unsigned int subscript = GL_INVALID_INDEX;
// Strip any trailing array operator and retrieve the subscript
size_t open = name->find_last_of('[');
size_t close = name->find_last_of(']');
if (open != std::string::npos && close == name->length() - 1)
{
subscript = atoi(name->substr(open + 1).c_str());
name->erase(open);
}
return subscript;
}
}
LinkResult::LinkResult(bool linkSuccess, const gl::Error &error) LinkResult::LinkResult(bool linkSuccess, const gl::Error &error)
: linkSuccess(linkSuccess), : linkSuccess(linkSuccess),
error(error) error(error)
...@@ -71,14 +50,15 @@ gl::UniformBlock *ProgramImpl::getUniformBlockByIndex(GLuint blockIndex) const ...@@ -71,14 +50,15 @@ gl::UniformBlock *ProgramImpl::getUniformBlockByIndex(GLuint blockIndex) const
return mUniformBlocks[blockIndex]; return mUniformBlocks[blockIndex];
} }
GLint ProgramImpl::getUniformLocation(std::string name) GLint ProgramImpl::getUniformLocation(const std::string &name) const
{ {
unsigned int subscript = ParseAndStripArrayIndex(&name); size_t subscript = GL_INVALID_INDEX;
std::string baseName = gl::ParseUniformName(name, &subscript);
unsigned int numUniforms = mUniformIndex.size(); unsigned int numUniforms = mUniformIndex.size();
for (unsigned int location = 0; location < numUniforms; location++) for (unsigned int location = 0; location < numUniforms; location++)
{ {
if (mUniformIndex[location].name == name) if (mUniformIndex[location].name == baseName)
{ {
const int index = mUniformIndex[location].index; const int index = mUniformIndex[location].index;
const bool isArray = mUniforms[index]->isArray(); const bool isArray = mUniforms[index]->isArray();
...@@ -94,9 +74,10 @@ GLint ProgramImpl::getUniformLocation(std::string name) ...@@ -94,9 +74,10 @@ GLint ProgramImpl::getUniformLocation(std::string name)
return -1; return -1;
} }
GLuint ProgramImpl::getUniformIndex(std::string name) GLuint ProgramImpl::getUniformIndex(const std::string &name) const
{ {
unsigned int subscript = ParseAndStripArrayIndex(&name); size_t subscript = GL_INVALID_INDEX;
std::string baseName = gl::ParseUniformName(name, &subscript);
// The app is not allowed to specify array indices other than 0 for arrays of basic types // The app is not allowed to specify array indices other than 0 for arrays of basic types
if (subscript != 0 && subscript != GL_INVALID_INDEX) if (subscript != 0 && subscript != GL_INVALID_INDEX)
...@@ -107,7 +88,7 @@ GLuint ProgramImpl::getUniformIndex(std::string name) ...@@ -107,7 +88,7 @@ GLuint ProgramImpl::getUniformIndex(std::string name)
unsigned int numUniforms = mUniforms.size(); unsigned int numUniforms = mUniforms.size();
for (unsigned int index = 0; index < numUniforms; index++) for (unsigned int index = 0; index < numUniforms; index++)
{ {
if (mUniforms[index]->name == name) if (mUniforms[index]->name == baseName)
{ {
if (mUniforms[index]->isArray() || subscript == GL_INVALID_INDEX) if (mUniforms[index]->isArray() || subscript == GL_INVALID_INDEX)
{ {
...@@ -119,15 +100,16 @@ GLuint ProgramImpl::getUniformIndex(std::string name) ...@@ -119,15 +100,16 @@ GLuint ProgramImpl::getUniformIndex(std::string name)
return GL_INVALID_INDEX; return GL_INVALID_INDEX;
} }
GLuint ProgramImpl::getUniformBlockIndex(std::string name) const GLuint ProgramImpl::getUniformBlockIndex(const std::string &name) const
{ {
unsigned int subscript = ParseAndStripArrayIndex(&name); size_t subscript = GL_INVALID_INDEX;
std::string baseName = gl::ParseUniformName(name, &subscript);
unsigned int numUniformBlocks = mUniformBlocks.size(); unsigned int numUniformBlocks = mUniformBlocks.size();
for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++) for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
{ {
const gl::UniformBlock &uniformBlock = *mUniformBlocks[blockIndex]; const gl::UniformBlock &uniformBlock = *mUniformBlocks[blockIndex];
if (uniformBlock.name == name) if (uniformBlock.name == baseName)
{ {
const bool arrayElementZero = (subscript == GL_INVALID_INDEX && uniformBlock.elementIndex == 0); const bool arrayElementZero = (subscript == GL_INVALID_INDEX && uniformBlock.elementIndex == 0);
if (subscript == uniformBlock.elementIndex || arrayElementZero) if (subscript == uniformBlock.elementIndex || arrayElementZero)
......
...@@ -116,9 +116,9 @@ class ProgramImpl : angle::NonCopyable ...@@ -116,9 +116,9 @@ class ProgramImpl : angle::NonCopyable
gl::LinkedUniform *getUniformByName(const std::string &name) const; gl::LinkedUniform *getUniformByName(const std::string &name) const;
gl::UniformBlock *getUniformBlockByIndex(GLuint blockIndex) const; gl::UniformBlock *getUniformBlockByIndex(GLuint blockIndex) const;
GLint getUniformLocation(std::string name); GLint getUniformLocation(const std::string &name) const;
GLuint getUniformIndex(std::string name); GLuint getUniformIndex(const std::string &name) const;
GLuint getUniformBlockIndex(std::string name) const; GLuint getUniformBlockIndex(const std::string &name) const;
virtual void reset(); virtual void reset();
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
'angle_unittests_sources': 'angle_unittests_sources':
[ [
'<(angle_path)/src/common/Optional_unittest.cpp', '<(angle_path)/src/common/Optional_unittest.cpp',
'<(angle_path)/src/common/utilities_unittest.cpp',
'<(angle_path)/src/libANGLE/Config_unittest.cpp', '<(angle_path)/src/libANGLE/Config_unittest.cpp',
'<(angle_path)/src/libANGLE/Fence_unittest.cpp', '<(angle_path)/src/libANGLE/Fence_unittest.cpp',
'<(angle_path)/src/libANGLE/HandleAllocator_unittest.cpp', '<(angle_path)/src/libANGLE/HandleAllocator_unittest.cpp',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment