Edit

kc3-lang/angle/src/compiler/translator/TranslatorHLSL.cpp

Branch :

  • Show log

    Commit

  • Author : Olli Etuaho
    Date : 2016-03-21 11:54:33
    Hash : 9696316d
    Message : Support ESSL structs containing samplers on D3D Since HLSL can't natively handle samplers in structs, samplers need to be extracted out of structs into separate variables in the translated shader code. In HLSL 4.1, samplers that were in structs go into the normal sampler arrays and are identified by index constants. In other HLSL versions, samplers that were in structs are translated as uniform variables. These transformations are done inside the HLSL output classes, not as tree transformations. This helps to keep the uniform API provided by the shader translator intact. Wherever a struct containing samplers is passed into a user-defined function, the translated HLSL code passes the separate sampler variables alongside a struct where the samplers have been removed. The D3D backend in libANGLE queries the uniform registers of any samplers that were in uniform structs, and adds them to the register maps, so that correct sampler state gets assigned to them. The extracted sampler variables are prefixed with "angle_" instead of the usual "_" to prevent any name conflicts between them and regular variables. BUG=angleproject:504 TEST=angle_end2end_tests, dEQP-GLES*.functional.shaders.struct.uniform.* (all pass), dEQP-GLES*.functional.uniform_api.* (most now pass) Change-Id: Ib79cba2fa0ff8257a973d70dfd917a64f0ca1efb Reviewed-on: https://chromium-review.googlesource.com/333743 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>

  • src/compiler/translator/TranslatorHLSL.cpp
  • //
    // Copyright (c) 2002-2013 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.
    //
    
    #include "compiler/translator/TranslatorHLSL.h"
    
    #include "compiler/translator/ArrayReturnValueToOutParameter.h"
    #include "compiler/translator/OutputHLSL.h"
    #include "compiler/translator/RemoveDynamicIndexing.h"
    #include "compiler/translator/RewriteElseBlocks.h"
    #include "compiler/translator/SeparateArrayInitialization.h"
    #include "compiler/translator/SeparateDeclarations.h"
    #include "compiler/translator/SeparateExpressionsReturningArrays.h"
    #include "compiler/translator/UnfoldShortCircuitToIf.h"
    
    TranslatorHLSL::TranslatorHLSL(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
        : TCompiler(type, spec, output)
    {
    }
    
    void TranslatorHLSL::translate(TIntermNode *root, int compileOptions)
    {
        const ShBuiltInResources &resources = getResources();
        int numRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
    
        SeparateDeclarations(root);
    
        // Note that SeparateDeclarations needs to be run before UnfoldShortCircuitToIf.
        UnfoldShortCircuitToIf(root, getTemporaryIndex());
    
        SeparateExpressionsReturningArrays(root, getTemporaryIndex());
    
        // Note that SeparateDeclarations needs to be run before SeparateArrayInitialization.
        SeparateArrayInitialization(root);
    
        // HLSL doesn't support arrays as return values, we'll need to make functions that have an array
        // as a return value to use an out parameter to transfer the array data instead.
        ArrayReturnValueToOutParameter(root, getTemporaryIndex());
    
        if (!shouldRunLoopAndIndexingValidation(compileOptions))
        {
            // HLSL doesn't support dynamic indexing of vectors and matrices.
            RemoveDynamicIndexing(root, getTemporaryIndex(), getSymbolTable(), getShaderVersion());
        }
    
        // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
        // use a vertex attribute as a condition, and some related computation in the else block.
        if (getOutputType() == SH_HLSL_3_0_OUTPUT && getShaderType() == GL_VERTEX_SHADER)
        {
            sh::RewriteElseBlocks(root, getTemporaryIndex());
        }
    
        sh::OutputHLSL outputHLSL(getShaderType(), getShaderVersion(), getExtensionBehavior(),
            getSourcePath(), getOutputType(), numRenderTargets, getUniforms(), compileOptions);
    
        outputHLSL.output(root, getInfoSink().obj);
    
        mInterfaceBlockRegisterMap = outputHLSL.getInterfaceBlockRegisterMap();
        mUniformRegisterMap = outputHLSL.getUniformRegisterMap();
    }
    
    bool TranslatorHLSL::hasInterfaceBlock(const std::string &interfaceBlockName) const
    {
        return (mInterfaceBlockRegisterMap.count(interfaceBlockName) > 0);
    }
    
    unsigned int TranslatorHLSL::getInterfaceBlockRegister(const std::string &interfaceBlockName) const
    {
        ASSERT(hasInterfaceBlock(interfaceBlockName));
        return mInterfaceBlockRegisterMap.find(interfaceBlockName)->second;
    }
    
    const std::map<std::string, unsigned int> *TranslatorHLSL::getUniformRegisterMap() const
    {
        return &mUniformRegisterMap;
    }