Commit b417815b by zmo@google.com

Interface design for user-defined name hashing.

1) We use BuiltInResources to pass the hash function to ANGLE, deciding whether we applies hash function or not. 2) We use 64 bits hashing function, because 64 bits is 16 bytes using hex representation, plus the "webgl_" prefix, we can keep the names under 128 (WebGL allows 5 levels of nesting in structures). If chooseing 128 bits, we will go beyond 128 characters, and some drivers can't handle that safely. ANGLEBUG=315 Review URL: https://codereview.appspot.com/6822077 git-svn-id: https://angleproject.googlecode.com/svn/trunk@1384 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent e37f1247
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#define COMPILER_EXPORT #define COMPILER_EXPORT
#endif #endif
#include "KHR/khrplatform.h"
// //
// This is the platform independent interface between an OGL driver // This is the platform independent interface between an OGL driver
// and the shading language compiler. // and the shading language compiler.
...@@ -110,7 +112,10 @@ typedef enum { ...@@ -110,7 +112,10 @@ typedef enum {
SH_ACTIVE_UNIFORM_MAX_LENGTH = 0x8B87, SH_ACTIVE_UNIFORM_MAX_LENGTH = 0x8B87,
SH_ACTIVE_ATTRIBUTES = 0x8B89, SH_ACTIVE_ATTRIBUTES = 0x8B89,
SH_ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8B8A, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8B8A,
SH_MAPPED_NAME_MAX_LENGTH = 0x8B8B SH_MAPPED_NAME_MAX_LENGTH = 0x6000,
SH_NAME_MAX_LENGTH = 0x6001,
SH_HASHED_NAME_MAX_LENGTH = 0x6002,
SH_HASHED_NAMES_COUNT = 0x6003
} ShShaderInfo; } ShShaderInfo;
// Compile options. // Compile options.
...@@ -145,7 +150,7 @@ typedef enum { ...@@ -145,7 +150,7 @@ typedef enum {
SH_DEPENDENCY_GRAPH = 0x0400, SH_DEPENDENCY_GRAPH = 0x0400,
// Enforce the GLSL 1.017 Appendix A section 7 packing restrictions. // Enforce the GLSL 1.017 Appendix A section 7 packing restrictions.
SH_ENFORCE_PACKING_RESTRICTIONS = 0x0800, SH_ENFORCE_PACKING_RESTRICTIONS = 0x0800
} ShCompileOptions; } ShCompileOptions;
// //
...@@ -160,6 +165,10 @@ COMPILER_EXPORT int ShInitialize(); ...@@ -160,6 +165,10 @@ COMPILER_EXPORT int ShInitialize();
// //
COMPILER_EXPORT int ShFinalize(); COMPILER_EXPORT int ShFinalize();
// The 64 bits hash function. The first parameter is the input string; the
// second parameter is the string length.
typedef khronos_uint64_t (*ShHashFunction64)(const char*, size_t);
// //
// Implementation dependent built-in resources (constants and extensions). // Implementation dependent built-in resources (constants and extensions).
// The names for these resources has been obtained by stripping gl_/GL_. // The names for these resources has been obtained by stripping gl_/GL_.
...@@ -181,6 +190,11 @@ typedef struct ...@@ -181,6 +190,11 @@ typedef struct
int OES_standard_derivatives; int OES_standard_derivatives;
int OES_EGL_image_external; int OES_EGL_image_external;
int ARB_texture_rectangle; int ARB_texture_rectangle;
// Name Hashing.
// Set a 64 bit hash function to enable user-defined name hashing.
// Default is NULL.
ShHashFunction64 HashFunction;
} ShBuiltInResources; } ShBuiltInResources;
// //
...@@ -267,6 +281,11 @@ COMPILER_EXPORT int ShCompile( ...@@ -267,6 +281,11 @@ COMPILER_EXPORT int ShCompile(
// termination character. // termination character.
// SH_MAPPED_NAME_MAX_LENGTH: the length of the mapped variable name including // SH_MAPPED_NAME_MAX_LENGTH: the length of the mapped variable name including
// the null termination character. // the null termination character.
// SH_NAME_MAX_LENGTH: the max length of a user-defined name including the
// null termination character.
// SH_HASHED_NAME_MAX_LENGTH: the max length of a hashed name including the
// null termination character.
// SH_HASHED_NAMES_COUNT: the number of hashed names from the latest compile.
// //
// params: Requested parameter // params: Requested parameter
COMPILER_EXPORT void ShGetInfo(const ShHandle handle, COMPILER_EXPORT void ShGetInfo(const ShHandle handle,
...@@ -347,6 +366,24 @@ COMPILER_EXPORT void ShGetActiveUniform(const ShHandle handle, ...@@ -347,6 +366,24 @@ COMPILER_EXPORT void ShGetActiveUniform(const ShHandle handle,
char* name, char* name,
char* mappedName); char* mappedName);
// Returns information about a name hashing entry from the latest compile.
// Parameters:
// handle: Specifies the compiler
// index: Specifies the index of the name hashing entry to be queried.
// name: Returns a null terminated string containing the user defined name.
// It is assumed that name has enough memory to accomodate the name.
// The size of the buffer required to store the user defined name can
// be obtained by calling ShGetInfo with SH_NAME_MAX_LENGTH.
// hashedName: Returns a null terminated string containing the hashed name of
// the uniform variable, It is assumed that hashedName has enough
// memory to accomodate the name. The size of the buffer required
// to store the name can be obtained by calling ShGetInfo with
// SH_HASHED_NAME_MAX_LENGTH.
COMPILER_EXPORT void ShGetNameHashingEntry(const ShHandle handle,
int index,
char* name,
char* hashedName);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -79,6 +79,7 @@ ...@@ -79,6 +79,7 @@
'compiler/glslang_lex.cpp', 'compiler/glslang_lex.cpp',
'compiler/glslang_tab.cpp', 'compiler/glslang_tab.cpp',
'compiler/glslang_tab.h', 'compiler/glslang_tab.h',
'compiler/HashNames.h',
'compiler/InfoSink.cpp', 'compiler/InfoSink.cpp',
'compiler/InfoSink.h', 'compiler/InfoSink.h',
'compiler/Initialize.cpp', 'compiler/Initialize.cpp',
......
#define MAJOR_VERSION 1 #define MAJOR_VERSION 1
#define MINOR_VERSION 0 #define MINOR_VERSION 0
#define BUILD_VERSION 0 #define BUILD_VERSION 0
#define BUILD_REVISION 1382 #define BUILD_REVISION 1384
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x) #define MACRO_STRINGIFY(x) STRINGIFY(x)
......
...@@ -124,6 +124,8 @@ bool TCompiler::Init(const ShBuiltInResources& resources) ...@@ -124,6 +124,8 @@ bool TCompiler::Init(const ShBuiltInResources& resources)
return false; return false;
InitExtensionBehavior(resources, extensionBehavior); InitExtensionBehavior(resources, extensionBehavior);
hashFunction = resources.HashFunction;
return true; return true;
} }
......
//
// Copyright (c) 2002-2012 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.
//
#ifndef COMPILER_HASH_NAMES_H_
#define COMPILER_HASH_NAMES_H_
#include <map>
#include "compiler/intermediate.h"
#include "GLSLANG/ShaderLang.h"
#define HASHED_NAME_PREFIX "webgl_"
typedef std::map<TPersistString, TPersistString> NameMap;
#endif // COMPILER_HASH_NAMES_H_
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "compiler/BuiltInFunctionEmulator.h" #include "compiler/BuiltInFunctionEmulator.h"
#include "compiler/ExtensionBehavior.h" #include "compiler/ExtensionBehavior.h"
#include "compiler/HashNames.h"
#include "compiler/InfoSink.h" #include "compiler/InfoSink.h"
#include "compiler/SymbolTable.h" #include "compiler/SymbolTable.h"
#include "compiler/VariableInfo.h" #include "compiler/VariableInfo.h"
...@@ -68,6 +69,9 @@ public: ...@@ -68,6 +69,9 @@ public:
const TVariableInfoList& getUniforms() const { return uniforms; } const TVariableInfoList& getUniforms() const { return uniforms; }
int getMappedNameMaxLength() const; int getMappedNameMaxLength() const;
ShHashFunction64 getHashFunction() const { return hashFunction; }
const NameMap& getNameMap() const { return nameMap; }
protected: protected:
ShShaderType getShaderType() const { return shaderType; } ShShaderType getShaderType() const { return shaderType; }
ShShaderSpec getShaderSpec() const { return shaderSpec; } ShShaderSpec getShaderSpec() const { return shaderSpec; }
...@@ -124,6 +128,10 @@ private: ...@@ -124,6 +128,10 @@ private:
// Cached copy of the ref-counted singleton. // Cached copy of the ref-counted singleton.
LongNameMap* longNameMap; LongNameMap* longNameMap;
// name hashing.
ShHashFunction64 hashFunction;
NameMap nameMap;
}; };
// //
......
...@@ -125,6 +125,9 @@ void ShInitBuiltInResources(ShBuiltInResources* resources) ...@@ -125,6 +125,9 @@ void ShInitBuiltInResources(ShBuiltInResources* resources)
resources->OES_standard_derivatives = 0; resources->OES_standard_derivatives = 0;
resources->OES_EGL_image_external = 0; resources->OES_EGL_image_external = 0;
resources->ARB_texture_rectangle = 0; resources->ARB_texture_rectangle = 0;
// Disable name hashing by default.
resources->HashFunction = NULL;
} }
// //
...@@ -224,6 +227,22 @@ void ShGetInfo(const ShHandle handle, ShShaderInfo pname, int* params) ...@@ -224,6 +227,22 @@ void ShGetInfo(const ShHandle handle, ShShaderInfo pname, int* params)
// handle array and struct dereferences. // handle array and struct dereferences.
*params = 1 + MAX_SYMBOL_NAME_LEN; *params = 1 + MAX_SYMBOL_NAME_LEN;
break; break;
case SH_NAME_MAX_LENGTH:
*params = 1 + MAX_SYMBOL_NAME_LEN;
break;
case SH_HASHED_NAME_MAX_LENGTH:
if (compiler->getHashFunction() == NULL) {
*params = 0;
} else {
// 64 bits hashing output requires 16 bytes for hex
// representation.
const char HashedNamePrefix[] = HASHED_NAME_PREFIX;
*params = 16 + sizeof(HashedNamePrefix);
}
break;
case SH_HASHED_NAMES_COUNT:
*params = compiler->getNameMap().size();
break;
default: UNREACHABLE(); default: UNREACHABLE();
} }
} }
...@@ -283,3 +302,46 @@ void ShGetActiveUniform(const ShHandle handle, ...@@ -283,3 +302,46 @@ void ShGetActiveUniform(const ShHandle handle,
getVariableInfo(SH_ACTIVE_UNIFORMS, getVariableInfo(SH_ACTIVE_UNIFORMS,
handle, index, length, size, type, name, mappedName); handle, index, length, size, type, name, mappedName);
} }
void ShGetNameHashingEntry(const ShHandle handle,
int index,
char* name,
char* hashedName)
{
if (!handle || !name || !hashedName || index < 0)
return;
TShHandleBase* base = static_cast<TShHandleBase*>(handle);
TCompiler* compiler = base->getAsCompiler();
if (!compiler) return;
const NameMap& nameMap = compiler->getNameMap();
if (index >= static_cast<int>(nameMap.size()))
return;
NameMap::const_iterator it = nameMap.begin();
for (int i = 0; i < index; ++i)
++it;
size_t len = it->first.length() + 1;
int max_len = 0;
ShGetInfo(handle, SH_NAME_MAX_LENGTH, &max_len);
if (static_cast<int>(len) > max_len) {
ASSERT(false);
len = max_len;
}
strncpy(name, it->first.c_str(), len);
// To be on the safe side in case the source is longer than expected.
name[len] = '\0';
len = it->second.length() + 1;
max_len = 0;
ShGetInfo(handle, SH_HASHED_NAME_MAX_LENGTH, &max_len);
if (static_cast<int>(len) > max_len) {
ASSERT(false);
len = max_len;
}
strncpy(hashedName, it->second.c_str(), len);
// To be on the safe side in case the source is longer than expected.
hashedName[len] = '\0';
}
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">
...@@ -234,6 +234,7 @@ ...@@ -234,6 +234,7 @@
<ClInclude Include="Diagnostics.h" /> <ClInclude Include="Diagnostics.h" />
<ClInclude Include="DirectiveHandler.h" /> <ClInclude Include="DirectiveHandler.h" />
<ClInclude Include="ForLoopUnroll.h" /> <ClInclude Include="ForLoopUnroll.h" />
<ClInclude Include="HashNames.h" />
<ClInclude Include="InfoSink.h" /> <ClInclude Include="InfoSink.h" />
<ClInclude Include="Initialize.h" /> <ClInclude Include="Initialize.h" />
<ClInclude Include="InitializeDll.h" /> <ClInclude Include="InitializeDll.h" />
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup> <ItemGroup>
<Filter Include="Source Files"> <Filter Include="Source Files">
...@@ -253,6 +253,9 @@ ...@@ -253,6 +253,9 @@
<ClInclude Include="depgraph\DependencyGraphOutput.h"> <ClInclude Include="depgraph\DependencyGraphOutput.h">
<Filter>Header Files\depgraph</Filter> <Filter>Header Files\depgraph</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="HashNames.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="glslang.l"> <CustomBuild Include="glslang.l">
......
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