CustomFilterCompiledProgram.cpp [plain text]
#include "config.h"
#if ENABLE(CSS_SHADERS) && USE(3D_GRAPHICS)
#include "CustomFilterCompiledProgram.h"
#include "CustomFilterGlobalContext.h"
namespace WebCore {
CustomFilterCompiledProgram::CustomFilterCompiledProgram(PassRefPtr<GraphicsContext3D> context, const String& validatedVertexShader, const String& validatedFragmentShader, CustomFilterProgramType programType)
: m_context(context)
, m_program(0)
, m_positionAttribLocation(-1)
, m_texAttribLocation(-1)
, m_meshAttribLocation(-1)
, m_triangleAttribLocation(-1)
, m_meshBoxLocation(-1)
, m_projectionMatrixLocation(-1)
, m_tileSizeLocation(-1)
, m_meshSizeLocation(-1)
, m_samplerLocation(-1)
, m_samplerSizeLocation(-1)
, m_contentSamplerLocation(-1)
, m_isInitialized(false)
{
ASSERT(!validatedVertexShader.isNull() && !validatedFragmentShader.isNull());
m_context->makeContextCurrent();
Platform3DObject vertexShader = compileShader(GraphicsContext3D::VERTEX_SHADER, validatedVertexShader);
if (!vertexShader)
return;
Platform3DObject fragmentShader = compileShader(GraphicsContext3D::FRAGMENT_SHADER, validatedFragmentShader);
if (!fragmentShader) {
m_context->deleteShader(vertexShader);
return;
}
m_program = linkProgram(vertexShader, fragmentShader);
m_context->deleteShader(vertexShader);
m_context->deleteShader(fragmentShader);
if (!m_program)
return;
initializeParameterLocations(programType);
m_isInitialized = true;
}
Platform3DObject CustomFilterCompiledProgram::compileShader(GC3Denum shaderType, const String& shaderString)
{
ASSERT(!shaderString.isNull());
Platform3DObject shader = m_context->createShader(shaderType);
m_context->shaderSource(shader, shaderString);
m_context->compileShader(shader);
int compiled = 0;
m_context->getShaderiv(shader, GraphicsContext3D::COMPILE_STATUS, &compiled);
if (!compiled) {
m_context->deleteShader(shader);
return 0;
}
return shader;
}
Platform3DObject CustomFilterCompiledProgram::linkProgram(Platform3DObject vertexShader, Platform3DObject fragmentShader)
{
Platform3DObject program = m_context->createProgram();
m_context->attachShader(program, vertexShader);
m_context->attachShader(program, fragmentShader);
m_context->linkProgram(program);
int linked = 0;
m_context->getProgramiv(program, GraphicsContext3D::LINK_STATUS, &linked);
if (!linked) {
m_context->deleteProgram(program);
return 0;
}
return program;
}
void CustomFilterCompiledProgram::initializeParameterLocations(CustomFilterProgramType programType)
{
m_positionAttribLocation = m_context->getAttribLocation(m_program, "a_position");
m_texAttribLocation = m_context->getAttribLocation(m_program, "a_texCoord");
m_meshAttribLocation = m_context->getAttribLocation(m_program, "a_meshCoord");
m_triangleAttribLocation = m_context->getAttribLocation(m_program, "a_triangleCoord");
m_meshBoxLocation = m_context->getUniformLocation(m_program, "u_meshBox");
m_tileSizeLocation = m_context->getUniformLocation(m_program, "u_tileSize");
m_meshSizeLocation = m_context->getUniformLocation(m_program, "u_meshSize");
m_projectionMatrixLocation = m_context->getUniformLocation(m_program, "u_projectionMatrix");
m_samplerSizeLocation = m_context->getUniformLocation(m_program, "u_textureSize");
m_contentSamplerLocation = m_context->getUniformLocation(m_program, "u_contentTexture");
if (programType == PROGRAM_TYPE_BLENDS_ELEMENT_TEXTURE) {
m_samplerLocation = m_context->getUniformLocation(m_program, "css_u_texture");
}
}
int CustomFilterCompiledProgram::uniformLocationByName(const String& name)
{
ASSERT(m_isInitialized);
return m_context->getUniformLocation(m_program, name);
}
CustomFilterCompiledProgram::~CustomFilterCompiledProgram()
{
if (m_program) {
m_context->makeContextCurrent();
m_context->deleteProgram(m_program);
}
}
} #endif // ENABLE(CSS_SHADERS) && USE(3D_GRAPHICS)