예제 #1
0
void ToneMappingEffect::init() {
    auto blitPS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(toneMapping_frag)));

    auto blitVS = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS();
    auto blitProgram = gpu::ShaderPointer(gpu::Shader::createProgram(blitVS, blitPS));

    gpu::Shader::BindingSet slotBindings;
    slotBindings.insert(gpu::Shader::Binding(std::string("toneMappingParamsBuffer"), ToneMappingEffect_ParamsSlot));
    slotBindings.insert(gpu::Shader::Binding(std::string("colorMap"), ToneMappingEffect_LightingMapSlot));
    gpu::Shader::makeProgram(*blitProgram, slotBindings);
    auto blitState = std::make_shared<gpu::State>();
    blitState->setColorWriteMask(true, true, true, true);
    _blitLightBuffer = gpu::PipelinePointer(gpu::Pipeline::create(blitProgram, blitState));
}
예제 #2
0
void ToneMappingEffect::init() {
    const char BlitTextureGamma_frag[] = R"SCRIBE(
        //  Generated on Sat Oct 24 09:34:37 2015
        //
        //  Draw texture 0 fetched at texcoord.xy
        //
        //  Created by Sam Gateau on 6/22/2015
        //  Copyright 2015 High Fidelity, Inc.
        //
        //  Distributed under the Apache License, Version 2.0.
        //  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
        //

        struct ToneMappingParams {
            vec4 _exp_2powExp_s0_s1;
            ivec4 _toneCurve_s0_s1_s2;
        };

        const float INV_GAMMA_22 = 1.0 / 2.2;
        const int ToneCurveNone = 0;
        const int ToneCurveGamma22 = 1;
        const int ToneCurveReinhard = 2;
        const int ToneCurveFilmic = 3;

        uniform toneMappingParamsBuffer {
            ToneMappingParams params;
        };
        float getTwoPowExposure() {
            return params._exp_2powExp_s0_s1.y;
        }
        int getToneCurve() {
            return params._toneCurve_s0_s1_s2.x;
        }

        uniform sampler2D colorMap;
        
        in vec2 varTexCoord0;
        out vec4 outFragColor;
        
        void main(void) {
            vec4 fragColorRaw = texture(colorMap, varTexCoord0);
            vec3 fragColor = fragColorRaw.xyz;

            vec3 srcColor = fragColor * getTwoPowExposure();

            int toneCurve = getToneCurve();
            vec3 tonedColor = srcColor;
            if (toneCurve == ToneCurveFilmic) {
                vec3 x = max(vec3(0.0), srcColor-0.004);
                tonedColor = (x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06);
            } else if (toneCurve == ToneCurveReinhard) {
                tonedColor = srcColor/(1.0 + srcColor);
                tonedColor = pow(tonedColor, vec3(INV_GAMMA_22));
            } else if (toneCurve == ToneCurveGamma22) {
                tonedColor = pow(srcColor, vec3(INV_GAMMA_22));
            } // else None toned = src

            outFragColor = vec4(tonedColor, 1.0);
        }
        
        )SCRIBE";
    auto blitPS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(BlitTextureGamma_frag)));

    auto blitVS = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS();
    auto blitProgram = gpu::ShaderPointer(gpu::Shader::createProgram(blitVS, blitPS));

    gpu::Shader::BindingSet slotBindings;
    slotBindings.insert(gpu::Shader::Binding(std::string("toneMappingParamsBuffer"), ToneMappingEffect_ParamsSlot));
    slotBindings.insert(gpu::Shader::Binding(std::string("colorMap"), ToneMappingEffect_LightingMapSlot));
    gpu::Shader::makeProgram(*blitProgram, slotBindings);
    auto blitState = std::make_shared<gpu::State>();
    blitState->setColorWriteMask(true, true, true, true);
    _blitLightBuffer = gpu::PipelinePointer(gpu::Pipeline::create(blitProgram, blitState));
}
void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) {
    auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(simple_vert)));
    auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(simple_textured_frag)));
    auto PSEmissive = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(simple_textured_emisive_frag)));
    
    _simpleShader = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PS));
    _emissiveShader = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PSEmissive));
    
    gpu::Shader::BindingSet slotBindings;
    slotBindings.insert(gpu::Shader::Binding(std::string("normalFittingMap"), DeferredLightingEffect::NORMAL_FITTING_MAP_SLOT));
    gpu::Shader::makeProgram(*_simpleShader, slotBindings);
    gpu::Shader::makeProgram(*_emissiveShader, slotBindings);

    _viewState = viewState;
    _directionalLightLocations = std::make_shared<LightLocations>();
    _directionalLightShadowMapLocations = std::make_shared<LightLocations>();
    _directionalLightCascadedShadowMapLocations = std::make_shared<LightLocations>();
    _directionalAmbientSphereLightLocations = std::make_shared<LightLocations>();
    _directionalAmbientSphereLightShadowMapLocations = std::make_shared<LightLocations>();
    _directionalAmbientSphereLightCascadedShadowMapLocations = std::make_shared<LightLocations>();
    _directionalSkyboxLightLocations = std::make_shared<LightLocations>();
    _directionalSkyboxLightShadowMapLocations = std::make_shared<LightLocations>();
    _directionalSkyboxLightCascadedShadowMapLocations = std::make_shared<LightLocations>();
    _pointLightLocations = std::make_shared<LightLocations>();
    _spotLightLocations = std::make_shared<LightLocations>();

    loadLightProgram(deferred_light_vert, directional_light_frag, false, _directionalLight, _directionalLightLocations);
    loadLightProgram(deferred_light_vert, directional_light_shadow_map_frag, false, _directionalLightShadowMap,
        _directionalLightShadowMapLocations);
    loadLightProgram(deferred_light_vert, directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap,
        _directionalLightCascadedShadowMapLocations);

    loadLightProgram(deferred_light_vert, directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations);
    loadLightProgram(deferred_light_vert, directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap,
        _directionalAmbientSphereLightShadowMapLocations);
    loadLightProgram(deferred_light_vert, directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap,
        _directionalAmbientSphereLightCascadedShadowMapLocations);

    loadLightProgram(deferred_light_vert, directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations);
    loadLightProgram(deferred_light_vert, directional_skybox_light_shadow_map_frag, false, _directionalSkyboxLightShadowMap,
        _directionalSkyboxLightShadowMapLocations);
    loadLightProgram(deferred_light_vert, directional_skybox_light_cascaded_shadow_map_frag, false, _directionalSkyboxLightCascadedShadowMap,
        _directionalSkyboxLightCascadedShadowMapLocations);


    loadLightProgram(deferred_light_limited_vert, point_light_frag, true, _pointLight, _pointLightLocations);
    loadLightProgram(deferred_light_spot_vert, spot_light_frag, true, _spotLight, _spotLightLocations);

    {
        //auto VSFS = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS();
        //auto PSBlit = gpu::StandardShaderLib::getDrawTexturePS();
        auto blitProgram = gpu::StandardShaderLib::getProgram(gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS, gpu::StandardShaderLib::getDrawTexturePS);
        gpu::Shader::makeProgram(*blitProgram);
        auto blitState = std::make_shared<gpu::State>();
        blitState->setBlendFunction(true,
                                gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA,
                                gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
        blitState->setColorWriteMask(true, true, true, false);
        _blitLightBuffer = gpu::PipelinePointer(gpu::Pipeline::create(blitProgram, blitState));
    }

    // Allocate a global light representing the Global Directional light casting shadow (the sun) and the ambient light
    _globalLights.push_back(0);
    _allocatedLights.push_back(std::make_shared<model::Light>());

    model::LightPointer lp = _allocatedLights[0];

    lp->setDirection(-glm::vec3(1.0f, 1.0f, 1.0f));
    lp->setColor(glm::vec3(1.0f));
    lp->setIntensity(1.0f);
    lp->setType(model::Light::SUN);
    lp->setAmbientSpherePreset(gpu::SphericalHarmonics::Preset(_ambientLightMode % gpu::SphericalHarmonics::NUM_PRESET));
}