Exemple #1
0
void ModelRender::pickPrograms(gpu::Batch& batch, RenderArgs::RenderMode mode, bool translucent, float alphaThreshold,
    bool hasLightmap, bool hasTangents, bool hasSpecular, bool isSkinned, bool isWireframe, RenderArgs* args,
    Locations*& locations) {

    PerformanceTimer perfTimer("Model::pickPrograms");
    getRenderPipelineLib();

    RenderKey key(mode, translucent, alphaThreshold, hasLightmap, hasTangents, hasSpecular, isSkinned, isWireframe);
    auto pipeline = _renderPipelineLib.find(key.getRaw());
    if (pipeline == _renderPipelineLib.end()) {
        qDebug() << "No good, couldn't find a pipeline from the key ?" << key.getRaw();
        locations = 0;
        return;
    }

    gpu::ShaderPointer program = (*pipeline).second._pipeline->getProgram();
    locations = (*pipeline).second._locations.get();


    // Setup the One pipeline
    batch.setPipeline((*pipeline).second._pipeline);

    if ((locations->alphaThreshold > -1) && (mode != RenderArgs::SHADOW_RENDER_MODE)) {
        batch._glUniform1f(locations->alphaThreshold, alphaThreshold);
    }

    if ((locations->glowIntensity > -1) && (mode != RenderArgs::SHADOW_RENDER_MODE)) {
        const float DEFAULT_GLOW_INTENSITY = 1.0f; // FIXME - glow is removed
        batch._glUniform1f(locations->glowIntensity, DEFAULT_GLOW_INTENSITY);
    }

    if ((locations->normalFittingMapUnit > -1)) {
        batch.setResourceTexture(locations->normalFittingMapUnit,
            DependencyManager::get<TextureCache>()->getNormalFittingTexture());
    }
}
gpu::PipelinePointer DeferredLightingEffect::bindSimpleProgram(gpu::Batch& batch, bool textured, bool culled,
                                               bool emmisive, bool depthBias) {
    SimpleProgramKey config{textured, culled, emmisive, depthBias};
    gpu::PipelinePointer pipeline = getPipeline(config);
    batch.setPipeline(pipeline);

    gpu::ShaderPointer program = (config.isEmissive()) ? _emissiveShader : _simpleShader;
    int glowIntensity = program->getUniforms().findLocation("glowIntensity");
    batch._glUniform1f(glowIntensity, 1.0f);
    
    if (!config.isTextured()) {
        // If it is not textured, bind white texture and keep using textured pipeline
        batch.setResourceTexture(0, DependencyManager::get<TextureCache>()->getWhiteTexture());
    }

    batch.setResourceTexture(NORMAL_FITTING_MAP_SLOT, DependencyManager::get<TextureCache>()->getNormalFittingTexture());
    return pipeline;
}
void RenderableProceduralItem::ProceduralInfo::prepare(gpu::Batch& batch) {
    if (_shaderUrl.isLocalFile()) {
        auto lastModified = QFileInfo(_shaderPath).lastModified().toMSecsSinceEpoch();
        if (lastModified > _shaderModified) {
            QFile file(_shaderPath);
            file.open(QIODevice::ReadOnly);
            _shaderSource = QTextStream(&file).readAll();
            _pipelineDirty = true;
            _shaderModified = lastModified;
        }
    } else if (_networkShader && _networkShader->isLoaded()) {
        _shaderSource = _networkShader->_source;
    }

    if (!_pipeline || _pipelineDirty) {
        _pipelineDirty = false;
        if (!_vertexShader) {
            _vertexShader = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(simple_vert)));
        }
        QString framentShaderSource = SHADER_TEMPLATE.arg(_shaderSource);
        _fragmentShader = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(framentShaderSource.toLocal8Bit().data())));
        _shader = gpu::ShaderPointer(gpu::Shader::createProgram(_vertexShader, _fragmentShader));
        gpu::Shader::BindingSet slotBindings;
        slotBindings.insert(gpu::Shader::Binding(std::string("normalFittingMap"), DeferredLightingEffect::NORMAL_FITTING_MAP_SLOT));
        gpu::Shader::makeProgram(*_shader, slotBindings);
        auto state = std::make_shared<gpu::State>();
        state->setCullMode(gpu::State::CULL_NONE);
        state->setDepthTest(true, true, gpu::LESS_EQUAL);
        state->setBlendFunction(false,
            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);
        _pipeline = gpu::PipelinePointer(gpu::Pipeline::create(_shader, state));
        _timeSlot = _shader->getUniforms().findLocation(UNIFORM_TIME_NAME);
        _scaleSlot = _shader->getUniforms().findLocation(UNIFORM_SCALE_NAME);
        _start = usecTimestampNow();
    }

    batch.setPipeline(_pipeline);
    float time = (float)((usecTimestampNow() - _start) / USECS_PER_MSEC) / MSECS_PER_SECOND;
    batch._glUniform1f(_timeSlot, time);
    auto scale = _entity->getDimensions();
    batch._glUniform3f(_scaleSlot, scale.x, scale.y, scale.z);
    batch.setResourceTexture(DeferredLightingEffect::NORMAL_FITTING_MAP_SLOT, DependencyManager::get<TextureCache>()->getNormalFittingTexture());
}