summaryrefslogtreecommitdiff
path: root/src/graphics/StandardShader.cpp
blob: 3fcd93a21d90b380f06b301cab1fc9a305d2246f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "StandardShader.h"

#include "GLContext.h"
#include "ShaderRegistry.h"

#include "../base/Logger.h"
#include "../base/Exception.h"

#include <iostream>

using namespace std;

#define STANDARD_SHADER "standard"
#define MINIMAL_SHADER "minimal"

namespace avg {

StandardShaderPtr StandardShader::get() 
{
    return GLContext::getMain()->getStandardShader();
}

StandardShader::StandardShader()
{
    avg::createShader(STANDARD_SHADER);
    m_pShader = avg::getShader(STANDARD_SHADER);
    m_pColorModelParam = m_pShader->getParam<int>("u_ColorModel");
    m_pAlphaParam = m_pShader->getParam<float>("u_Alpha");
    m_pColorCoeff0Param = m_pShader->getParam<glm::vec4>("u_ColorCoeff0");
    m_pColorCoeff1Param = m_pShader->getParam<glm::vec4>("u_ColorCoeff1");
    m_pColorCoeff2Param = m_pShader->getParam<glm::vec4>("u_ColorCoeff2");
    m_pColorCoeff3Param = m_pShader->getParam<glm::vec4>("u_ColorCoeff3");
    m_pGammaParam = m_pShader->getParam<glm::vec4>("u_Gamma");
 
    m_pUseColorCoeffParam = m_pShader->getParam<int>("u_bUseColorCoeff");
    m_pPremultipliedAlphaParam = m_pShader->getParam<int>("u_bPremultipliedAlpha");
    m_pUseMaskParam = m_pShader->getParam<int>("u_bUseMask");
    m_pMaskPosParam = m_pShader->getParam<glm::vec2>("u_MaskPos");
    m_pMaskSizeParam = m_pShader->getParam<glm::vec2>("u_MaskSize");

    m_pShader->activate();
    m_pShader->getParam<int>("u_Texture")->set(0);
    if (GLContext::getMain()->useGPUYUVConversion()) {
        m_pShader->getParam<int>("u_CBTexture")->set(1);
        m_pShader->getParam<int>("u_CRTexture")->set(2);
        m_pShader->getParam<int>("u_ATexture")->set(3);
    }
    m_pShader->getParam<int>("u_MaskTexture")->set(4);

    if (GLContext::getMain()->getShaderUsage() != GLConfig::FULL) {
        avg::createShader(MINIMAL_SHADER);
        m_pMinimalShader = avg::getShader(MINIMAL_SHADER);
        m_pMinimalShader->activate();
        m_pMinimalShader->getParam<int>("u_Texture")->set(0);
        m_pMinimalAlphaParam = m_pMinimalShader->getParam<float>("u_Alpha");
    }
    
    generateWhiteTexture(); 
}

StandardShader::~StandardShader()
{
}

void StandardShader::activate()
{
    if (useMinimalShader()) {
        m_pMinimalShader->activate();
        m_pMinimalShader->setTransform(m_Transform);
        m_pMinimalAlphaParam->set(m_Alpha);
    } else {
        m_pShader->activate();
        m_pShader->setTransform(m_Transform);
        m_pColorModelParam->set(m_ColorModel);
        m_pAlphaParam->set(m_Alpha);

        m_pUseColorCoeffParam->set(m_bUseColorCoeff);
        const glm::mat4& mat = m_ColorMatrix;
        m_pColorCoeff0Param->set(glm::vec4(mat[0][0], mat[0][1], mat[0][2], 0));
        m_pColorCoeff1Param->set(glm::vec4(mat[1][0], mat[1][1], mat[1][2], 0));
        m_pColorCoeff2Param->set(glm::vec4(mat[2][0], mat[2][1], mat[2][2], 0));
        m_pColorCoeff3Param->set(glm::vec4(mat[3][0], mat[3][1], mat[3][2], 1));
        m_pGammaParam->set(m_Gamma);

        m_pPremultipliedAlphaParam->set(m_bPremultipliedAlpha);

        m_pUseMaskParam->set(m_bUseMask);
        if (m_bUseMask) {
            m_pMaskPosParam->set(m_MaskPos);
            m_pMaskSizeParam->set(m_MaskSize);
        }
    }
}

void StandardShader::setTransform(const glm::mat4& transform)
{
    m_Transform = transform;
}

void StandardShader::setColorModel(int model)
{
    m_ColorModel = model;
}

void StandardShader::setAlpha(float alpha)
{
    m_Alpha = alpha;
}
    
void StandardShader::setUntextured()
{
    // Activate an internal 1x1 A8 texture.
    m_ColorModel = 2;
    m_pWhiteTex->activate(GL_TEXTURE0);
    disableColorspaceMatrix();
    setGamma(glm::vec4(1.f,1.f,1.f,1.f));
    setPremultipliedAlpha(false);
    setMask(false);
}

void StandardShader::setColorspaceMatrix(const glm::mat4& mat)
{
    m_bUseColorCoeff = true;
    m_ColorMatrix = mat;
}

void StandardShader::disableColorspaceMatrix()
{
    m_bUseColorCoeff = false;
}

void StandardShader::setGamma(const glm::vec4& gamma)
{
    m_Gamma = gamma;
}

void StandardShader::setPremultipliedAlpha(bool bPremultipliedAlpha)
{
    m_bPremultipliedAlpha = bPremultipliedAlpha;
}

void StandardShader::setMask(bool bUseMask, const glm::vec2& maskPos,
        const glm::vec2& maskSize)
{
    m_bUseMask = bUseMask;
    m_MaskPos = maskPos;
    m_MaskSize = maskSize;
}

const OGLShaderPtr& StandardShader::getShader() const
{
    if (useMinimalShader()) {
        return m_pMinimalShader;
    } else {
        return m_pShader;
    }
}

void StandardShader::dump() const
{
    cerr << "---------Standard shader--------" << endl;
    cerr << "  m_Transform: " << m_Transform << endl;
    cerr << "  m_ColorModel: " << m_ColorModel << endl;
    cerr << "  m_Alpha: " << m_Alpha << endl;
    cerr << "  m_bUseColorCoeff: " << m_bUseColorCoeff << endl;
    cerr << "  m_ColorMatrix: " << m_ColorMatrix << endl;
    cerr << "  m_Gamma: " << m_Gamma << endl;
    cerr << "  m_bPremultipliedAlpha: " << m_bPremultipliedAlpha << endl;
    cerr << "  m_bUseMask: " << m_bUseMask << endl;
    cerr << "  m_MaskPos: " << m_MaskPos << endl;
    cerr << "  m_MaskSize: " << m_MaskSize << endl;
    cerr << endl;
}

void StandardShader::generateWhiteTexture()
{
    BitmapPtr pBmp(new Bitmap(glm::vec2(1,1), I8));
    *(pBmp->getPixels()) = 255;
    m_pWhiteTex = GLTexturePtr(new GLTexture(IntPoint(1,1), I8));
    m_pWhiteTex->moveBmpToTexture(pBmp);
}

bool StandardShader::useMinimalShader() const
{
    bool bActivateMinimal = false;
    if (GLContext::getMain()->getShaderUsage() != GLConfig::FULL) {
        bool bGammaIsModified = (!almostEqual(m_Gamma, glm::vec4(1.0f,1.0f,1.0f,1.0f)));
        if (m_ColorModel == 0 && !m_bUseColorCoeff && !bGammaIsModified && !m_bUseMask) {
            bActivateMinimal = true;
        }
    }
    return bActivateMinimal;
}

}