Commit e01a9bc8 by John Kessenich

HLSL: Plumb in HLSL parse context and keywords, and most basic HLSL parser and test.

parent b3dc3acd
......@@ -24,3 +24,4 @@ add_subdirectory(glslang)
add_subdirectory(OGLCompilersDLL)
add_subdirectory(StandAlone)
add_subdirectory(SPIRV)
add_subdirectory(hlsl)
......@@ -10,6 +10,7 @@ set(LIBRARIES
glslang
OGLCompiler
OSDependent
HLSL
SPIRV)
if(WIN32)
......
hlsl.frag
Linked fragment stage:
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 6
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "PixelShaderFunction"
ExecutionMode 4 OriginUpperLeft
Source HLSL 100
Name 4 "PixelShaderFunction"
2: TypeVoid
3: TypeFunction 2
4(PixelShaderFunction): 2 Function None 3
5: Label
FunctionEnd
//float4x4 World;
//float4x4 View;
//float4x4 Projection;
//
//float4 AmbientColor = float4(1, 1, 1, 1);
//float AmbientIntensity = 0.1;
//
//float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
//{
// return AmbientColor * AmbientIntensity;
//}
......@@ -56,6 +56,24 @@ done < test-spirv-list
rm -f comp.spv frag.spv geom.spv tesc.spv tese.spv vert.spv
#
# HLSL -> SPIR-V code generation tests
#
while read t; do
case $t in
\#*)
# Skip comment lines in the test list file.
;;
*)
echo Running HLSL-to-SPIR-V $t...
b=`basename $t`
$EXE -D -e PixelShaderFunction -H $t > $TARGETDIR/$b.out
diff -b $BASEDIR/$b.out $TARGETDIR/$b.out || HASERROR=1
;;
esac
done < test-hlsl-spirv-list
rm -f comp.spv frag.spv geom.spv tesc.spv tese.spv vert.spv
#
# Preprocessor tests
#
while read t; do
......
# Test looping constructs.
# No tests yet for making sure break and continue from a nested loop
# goes to the innermost target.
hlsl.frag
......@@ -46,6 +46,7 @@
#include <sstream>
#include "SymbolTable.h"
#include "ParseHelper.h"
#include "../../hlsl/hlslParseHelper.h"
#include "Scan.h"
#include "ScanContext.h"
......@@ -598,26 +599,37 @@ bool ProcessDeferred(
// Now we can process the full shader under proper symbols and rules.
//
intermediate.setEntryPoint("main");
TParseContext parseContext(symbolTable, intermediate, false, version, profile, spv, vulkan, compiler->getLanguage(), compiler->infoSink, forwardCompatible, messages);
glslang::TScanContext scanContext(parseContext);
TPpContext ppContext(parseContext, includer);
parseContext.setScanContext(&scanContext);
parseContext.setPpContext(&ppContext);
parseContext.setLimits(*resources);
TParseContextBase* parseContext;
if (source == EShSourceHlsl) {
parseContext = new HlslParseContext(symbolTable, intermediate, false, version, profile, spv, vulkan,
compiler->getLanguage(), compiler->infoSink, forwardCompatible, messages);
}
else {
intermediate.setEntryPoint("main");
parseContext = new TParseContext(symbolTable, intermediate, false, version, profile, spv, vulkan,
compiler->getLanguage(), compiler->infoSink, forwardCompatible, messages);
}
TPpContext ppContext(*parseContext, includer);
// only GLSL (bison triggered, really) needs an externally set scan context
glslang::TScanContext scanContext(*parseContext);
if ((messages & EShMsgReadHlsl) == 0)
parseContext->setScanContext(&scanContext);
parseContext->setPpContext(&ppContext);
parseContext->setLimits(*resources);
if (! goodVersion)
parseContext.addError();
parseContext->addError();
if (warnVersionNotFirst) {
TSourceLoc loc;
loc.init();
parseContext.warn(loc, "Illegal to have non-comment, non-whitespace tokens before #version", "#version", "");
parseContext->warn(loc, "Illegal to have non-comment, non-whitespace tokens before #version", "#version", "");
}
parseContext.initializeExtensionBehavior();
parseContext->initializeExtensionBehavior();
// Fill in the strings as outlined above.
strings[0] = parseContext.getPreamble();
strings[0] = parseContext->getPreamble();
lengths[0] = strlen(strings[0]);
names[0] = nullptr;
strings[1] = customPreamble;
......@@ -635,14 +647,14 @@ bool ProcessDeferred(
// Push a new symbol allocation scope that will get used for the shader's globals.
symbolTable.push();
bool success = processingContext(parseContext, ppContext, fullInput,
bool success = processingContext(*parseContext, ppContext, fullInput,
versionWillBeError, symbolTable,
intermediate, optLevel, messages);
// Clean up the symbol table. The AST is self-sufficient now.
delete symbolTableMemory;
delete parseContext;
delete [] lengths;
delete [] strings;
delete [] names;
......
......@@ -69,6 +69,18 @@ void TIntermediate::error(TInfoSink& infoSink, const char* message)
//
void TIntermediate::merge(TInfoSink& infoSink, TIntermediate& unit)
{
if (source == EShSourceNone)
source = unit.source;
if (source != unit.source)
error(infoSink, "can't link compilation units from different source languages");
if (source == EShSourceHlsl && unit.entryPoint.size() > 0) {
if (entryPoint.size() > 0)
error(infoSink, "can't handle multiple entry points per stage");
else
entryPoint = unit.entryPoint;
}
numMains += unit.numMains;
numErrors += unit.numErrors;
numPushConstants += unit.numPushConstants;
......@@ -355,8 +367,8 @@ void TIntermediate::mergeErrorCheck(TInfoSink& infoSink, const TIntermSymbol& sy
// Also, lock in defaults of things not set, including array sizes.
//
void TIntermediate::finalCheck(TInfoSink& infoSink)
{
if (numMains < 1)
{
if (source == EShSourceGlsl && numMains < 1)
error(infoSink, "Missing entry point: Each stage requires one \"void main()\" entry point");
if (numPushConstants > 1)
......
cmake_minimum_required(VERSION 2.8)
set(SOURCES
hlslParseHelper.cpp
hlslScanContext.cpp
hlslGrammar.cpp)
set(HEADERS
hlslParseHelper.h
hlslTokens.h
hlslScanContext.h
hlslGrammar.h)
add_library(HLSL STATIC ${SOURCES} ${HEADERS})
if(WIN32)
source_group("Source" FILES ${SOURCES} ${HEADERS})
endif(WIN32)
install(TARGETS HLSL
ARCHIVE DESTINATION lib)
//
//Copyright (C) 2016 Google, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of Google, Inc., nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
#include "hlslTokens.h"
#include "hlslGrammar.h"
namespace glslang {
// Root entry point to this recursive decent parser.
// Return true if compilation unit was successfully accepted.
bool HlslGrammar::parse()
{
advanceToken();
return acceptCompilationUnit();
}
void HlslGrammar::expected(const char* syntax)
{
parseContext.error(token.loc, "Expected", syntax, "");
}
// Load 'token' with the next token in the stream of tokens.
void HlslGrammar::advanceToken()
{
scanContext.tokenize(token);
}
// Return true and advance to the next token if the current token is the
// expected (passed in) token class.
bool HlslGrammar::acceptTokenClass(EHlslTokenClass tokenClass)
{
if (token.tokenClass == tokenClass) {
advanceToken();
return true;
}
return false;
}
// compilationUnit
// : list of externalDeclaration
//
bool HlslGrammar::acceptCompilationUnit()
{
while (token.tokenClass != EHTokNone) {
if (! acceptDeclaration())
return false;
}
return true;
}
// declaration
// : dummy stub
bool HlslGrammar::acceptDeclaration()
{
advanceToken();
return true;
}
} // end namespace glslang
//
//Copyright (C) 2016 Google, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of Google, Inc., nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
#ifndef HLSLGRAMMAR_H_
#define HLSLGRAMMAR_H_
#include "hlslScanContext.h"
#include "hlslParseHelper.h"
namespace glslang {
class HlslGrammar {
public:
HlslGrammar(HlslScanContext& scanContext, HlslParseContext& parseContext)
: scanContext(scanContext), parseContext(parseContext) { }
virtual ~HlslGrammar() { }
bool parse();
protected:
void expected(const char*);
void advanceToken();
bool acceptTokenClass(EHlslTokenClass);
bool acceptCompilationUnit();
bool acceptDeclaration();
HlslScanContext& scanContext;
HlslParseContext& parseContext;
HlslToken token;
};
} // end namespace glslang
#endif // HLSLGRAMMAR_H_
This source diff could not be displayed because it is too large. You can view the blob instead.
//
//Copyright (C) 2016 Google, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of Google, Inc., nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
//
// This holds context specific to the GLSL scanner, which
// sits between the preprocessor scanner and parser.
//
#ifndef HLSLSCANCONTEXT_H_
#define HLSLSCANCONTEXT_H_
#include "../glslang/MachineIndependent/ParseHelper.h"
#include "hlslTokens.h"
namespace glslang {
class TPpContext;
class TPpToken;
struct HlslToken {
TSourceLoc loc;
EHlslTokenClass tokenClass;
bool isType;
union {
glslang::TString *string;
int i;
unsigned int u;
bool b;
double d;
};
glslang::TSymbol* symbol;
};
class HlslScanContext {
public:
HlslScanContext(TParseContextBase& parseContext, TPpContext& ppContext)
: parseContext(parseContext), ppContext(ppContext), afterType(false), field(false) { }
virtual ~HlslScanContext() { }
static void fillInKeywordMap();
static void deleteKeywordMap();
void tokenize(HlslToken&);
protected:
HlslScanContext(HlslScanContext&);
HlslScanContext& operator=(HlslScanContext&);
EHlslTokenClass tokenizeClass(HlslToken&);
EHlslTokenClass tokenizeIdentifier();
EHlslTokenClass identifierOrType();
EHlslTokenClass reservedWord();
EHlslTokenClass identifierOrReserved(bool reserved);
EHlslTokenClass nonreservedKeyword(int version);
TParseContextBase& parseContext;
TPpContext& ppContext;
bool afterType; // true if we've recognized a type, so can only be looking for an identifier
bool field; // true if we're on a field, right after a '.'
TSourceLoc loc;
TPpToken* ppToken;
HlslToken* parserToken;
const char* tokenText;
EHlslTokenClass keyword;
};
} // end namespace glslang
#endif // HLSLSCANCONTEXT_H_
//
//Copyright (C) 2016 Google, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of Google, Inc., nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
#ifndef EHLSLTOKENS_H_
#define EHLSLTOKENS_H_
namespace glslang {
enum EHlslTokenClass {
EHTokNone = 0,
// qualifiers
EHTokStatic,
EHTokConst,
EHTokSNorm,
EHTokUnorm,
EHTokExtern,
EHTokUniform,
EHTokVolatile,
EHTokShared,
EHTokGroupShared,
EHTokLinear,
EHTokCentroid,
EHTokNointerpolation,
EHTokNoperspective,
EHTokSample,
EHTokRowMajor,
EHTokColumnMajor,
EHTokPackOffset,
// template types
EHTokBuffer,
EHTokVector,
EHTokMatrix,
// scalar types
EHTokVoid,
EHTokBool,
EHTokInt,
EHTokUint,
EHTokDword,
EHTokHalf,
EHTokFloat,
EHTokDouble,
EHTokMin16float,
EHTokMin10float,
EHTokMin16int,
EHTokMin12int,
EHTokMin16uint,
// vector types
EHTokBool1,
EHTokBool2,
EHTokBool3,
EHTokBool4,
EHTokFloat1,
EHTokFloat2,
EHTokFloat3,
EHTokFloat4,
EHTokInt1,
EHTokInt2,
EHTokInt3,
EHTokInt4,
EHTokDouble1,
EHTokDouble2,
EHTokDouble3,
EHTokDouble4,
EHTokUint1,
EHTokUint2,
EHTokUint3,
EHTokUint4,
// matrix types
EHTokInt1x1,
EHTokInt1x2,
EHTokInt1x3,
EHTokInt1x4,
EHTokInt2x1,
EHTokInt2x2,
EHTokInt2x3,
EHTokInt2x4,
EHTokInt3x1,
EHTokInt3x2,
EHTokInt3x3,
EHTokInt3x4,
EHTokInt4x1,
EHTokInt4x2,
EHTokInt4x3,
EHTokInt4x4,
EHTokFloat1x1,
EHTokFloat1x2,
EHTokFloat1x3,
EHTokFloat1x4,
EHTokFloat2x1,
EHTokFloat2x2,
EHTokFloat2x3,
EHTokFloat2x4,
EHTokFloat3x1,
EHTokFloat3x2,
EHTokFloat3x3,
EHTokFloat3x4,
EHTokFloat4x1,
EHTokFloat4x2,
EHTokFloat4x3,
EHTokFloat4x4,
EHTokDouble1x1,
EHTokDouble1x2,
EHTokDouble1x3,
EHTokDouble1x4,
EHTokDouble2x1,
EHTokDouble2x2,
EHTokDouble2x3,
EHTokDouble2x4,
EHTokDouble3x1,
EHTokDouble3x2,
EHTokDouble3x3,
EHTokDouble3x4,
EHTokDouble4x1,
EHTokDouble4x2,
EHTokDouble4x3,
EHTokDouble4x4,
// texturing types
EHTokSampler,
EHTokSampler1d,
EHTokSampler2d,
EHTokSampler3d,
EHTokSamplerCube,
EHTokSamplerState,
EHTokSamplerComparisonState,
EHTokTexture,
EHTokTexture1d,
EHTokTexture1darray,
EHTokTexture2d,
EHTokTexture2darray,
EHTokTexture3d,
EHTokTextureCube,
// variable, user type, ...
EHTokIdentifier,
EHTokTypeName,
EHTokStruct,
EHTokTypedef,
// constant
EHTokFloatConstant,
EHTokDoubleConstant,
EHTokIntConstant,
EHTokUintConstant,
EHTokBoolConstant,
// control flow
EHTokFor,
EHTokDo,
EHTokWhile,
EHTokBreak,
EHTokContinue,
EHTokIf,
EHTokElse,
EHTokDiscard,
EHTokReturn,
EHTokSwitch,
EHTokCase,
EHTokDefault,
// expressions
EHTokLeftOp,
EHTokRightOp,
EHTokIncOp,
EHTokDecOp,
EHTokLeOp,
EHTokGeOp,
EHTokEqOp,
EHTokNeOp,
EHTokAndOp,
EHTokOrOp,
EHTokXorOp,
EHTokMulAssign,
EHTokDivAssign,
EHTokAddAssign,
EHTokModAssign,
EHTokLeftAssign,
EHTokRightAssign,
EHTokAndAssign,
EHTokXorAssign,
EHTokOrAssign,
EHTokSubAssign,
EHTokLeftParen,
EHTokRightParen,
EHTokLeftBracket,
EHTokRightBracket,
EHTokLeftBrace,
EHTokRightBrace,
EHTokDot,
EHTokComma,
EHTokColon,
EHTokEqual,
EHTokSemicolon,
EHTokBang,
EHTokDash,
EHTokTilde,
EHTokPlus,
EHTokStar,
EHTokSlash,
EHTokPercent,
EHTokLeftAngle,
EHTokRightAngle,
EHTokVerticalBar,
EHTokCaret,
EHTokAmpersand,
EHTokQuestion,
};
} // end namespace glslang
#endif // EHLSLTOKENS_H_
\ No newline at end of file
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