2012-10-05 00:35:54 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-12-09 00:41:02 +04:00
|
|
|
#include "WebGLProgram.h"
|
2012-10-05 00:35:54 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "GLContext.h"
|
|
|
|
#include "mozilla/dom/WebGLRenderingContextBinding.h"
|
|
|
|
#include "WebGLContext.h"
|
|
|
|
#include "WebGLShader.h"
|
2015-01-16 02:40:39 +03:00
|
|
|
#include "WebGLUniformLocation.h"
|
|
|
|
#include "WebGLValidateStrings.h"
|
2014-05-14 04:15:19 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
namespace mozilla {
|
2012-10-05 00:35:54 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
/* If `name`: "foo[3]"
|
|
|
|
* Then returns true, with
|
|
|
|
* `out_baseName`: "foo"
|
|
|
|
* `out_isArray`: true
|
|
|
|
* `out_index`: 3
|
|
|
|
*
|
|
|
|
* If `name`: "foo"
|
|
|
|
* Then returns true, with
|
|
|
|
* `out_baseName`: "foo"
|
|
|
|
* `out_isArray`: false
|
|
|
|
* `out_index`: <not written>
|
|
|
|
*/
|
2014-11-14 07:03:50 +03:00
|
|
|
static bool
|
2015-01-16 02:40:39 +03:00
|
|
|
ParseName(const nsCString& name, nsCString* const out_baseName,
|
|
|
|
bool* const out_isArray, size_t* const out_arrayIndex)
|
2012-12-09 00:41:02 +04:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
int32_t indexEnd = name.RFind("]");
|
|
|
|
if (indexEnd == -1 ||
|
|
|
|
(uint32_t)indexEnd != name.Length() - 1)
|
|
|
|
{
|
|
|
|
*out_baseName = name;
|
|
|
|
*out_isArray = false;
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-09 00:41:02 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
int32_t indexOpenBracket = name.RFind("[");
|
|
|
|
if (indexOpenBracket == -1)
|
2015-01-10 05:40:56 +03:00
|
|
|
return false;
|
2012-12-09 00:41:02 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
uint32_t indexStart = indexOpenBracket + 1;
|
|
|
|
uint32_t indexLen = indexEnd - indexStart;
|
|
|
|
if (indexLen == 0)
|
2015-01-13 06:51:20 +03:00
|
|
|
return false;
|
2015-01-10 08:03:54 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
const nsAutoCString indexStr(Substring(name, indexStart, indexLen));
|
|
|
|
|
|
|
|
nsresult errorcode;
|
|
|
|
int32_t indexNum = indexStr.ToInteger(&errorcode);
|
|
|
|
if (NS_FAILED(errorcode))
|
|
|
|
return false;
|
2015-01-13 11:07:26 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (indexNum < 0)
|
2012-12-09 00:41:02 +04:00
|
|
|
return false;
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
*out_baseName = StringHead(name, indexOpenBracket);
|
|
|
|
*out_isArray = true;
|
|
|
|
*out_arrayIndex = indexNum;
|
2012-12-09 00:41:02 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
static void
|
|
|
|
AddActiveInfo(WebGLContext* webgl, GLint elemCount, GLenum elemType, bool isArray,
|
|
|
|
const nsACString& baseUserName, const nsACString& baseMappedName,
|
|
|
|
std::vector<nsRefPtr<WebGLActiveInfo>>* activeInfoList,
|
|
|
|
std::map<nsCString, const WebGLActiveInfo*>* infoLocMap)
|
|
|
|
{
|
|
|
|
nsRefPtr<WebGLActiveInfo> info = new WebGLActiveInfo(webgl, elemCount, elemType,
|
|
|
|
isArray, baseUserName,
|
|
|
|
baseMappedName);
|
|
|
|
activeInfoList->push_back(info);
|
|
|
|
|
|
|
|
infoLocMap->insert(std::make_pair(info->mBaseUserName, info.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
//#define DUMP_SHADERVAR_MAPPINGS
|
|
|
|
|
|
|
|
static TemporaryRef<const webgl::LinkedProgramInfo>
|
|
|
|
QueryProgramInfo(WebGLProgram* prog, gl::GLContext* gl)
|
|
|
|
{
|
|
|
|
RefPtr<webgl::LinkedProgramInfo> info(new webgl::LinkedProgramInfo(prog));
|
|
|
|
|
|
|
|
GLuint maxAttribLenWithNull = 0;
|
|
|
|
gl->fGetProgramiv(prog->mGLName, LOCAL_GL_ACTIVE_ATTRIBUTE_MAX_LENGTH,
|
|
|
|
(GLint*)&maxAttribLenWithNull);
|
|
|
|
if (maxAttribLenWithNull < 1)
|
|
|
|
maxAttribLenWithNull = 1;
|
|
|
|
|
|
|
|
GLuint maxUniformLenWithNull = 0;
|
|
|
|
gl->fGetProgramiv(prog->mGLName, LOCAL_GL_ACTIVE_UNIFORM_MAX_LENGTH,
|
|
|
|
(GLint*)&maxUniformLenWithNull);
|
|
|
|
if (maxUniformLenWithNull < 1)
|
|
|
|
maxUniformLenWithNull = 1;
|
|
|
|
|
|
|
|
#ifdef DUMP_SHADERVAR_MAPPINGS
|
|
|
|
printf_stderr("maxAttribLenWithNull: %d\n", maxAttribLenWithNull);
|
|
|
|
printf_stderr("maxUniformLenWithNull: %d\n", maxUniformLenWithNull);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Attribs
|
|
|
|
|
|
|
|
GLuint numActiveAttribs = 0;
|
|
|
|
gl->fGetProgramiv(prog->mGLName, LOCAL_GL_ACTIVE_ATTRIBUTES,
|
|
|
|
(GLint*)&numActiveAttribs);
|
|
|
|
|
|
|
|
for (GLuint i = 0; i < numActiveAttribs; i++) {
|
|
|
|
nsAutoCString mappedName;
|
|
|
|
mappedName.SetLength(maxAttribLenWithNull - 1);
|
|
|
|
|
|
|
|
GLsizei lengthWithoutNull = 0;
|
|
|
|
GLint elemCount = 0; // `size`
|
|
|
|
GLenum elemType = 0; // `type`
|
|
|
|
gl->fGetActiveAttrib(prog->mGLName, i, mappedName.Length()+1, &lengthWithoutNull,
|
|
|
|
&elemCount, &elemType, mappedName.BeginWriting());
|
|
|
|
|
|
|
|
mappedName.SetLength(lengthWithoutNull);
|
|
|
|
|
|
|
|
// Collect ActiveInfos:
|
|
|
|
|
|
|
|
// Attribs can't be arrays, so we can skip some of the mess we have in the Uniform
|
|
|
|
// path.
|
|
|
|
nsDependentCString userName;
|
|
|
|
if (!prog->FindAttribUserNameByMappedName(mappedName, &userName))
|
|
|
|
userName.Rebind(mappedName, 0);
|
|
|
|
|
|
|
|
#ifdef DUMP_SHADERVAR_MAPPINGS
|
|
|
|
printf_stderr("[attrib %i] %s/%s\n", i, mappedName.BeginReading(),
|
|
|
|
userName.BeginReading());
|
|
|
|
printf_stderr(" lengthWithoutNull: %d\n", lengthWithoutNull);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const bool isArray = false;
|
|
|
|
AddActiveInfo(prog->Context(), elemCount, elemType, isArray, userName, mappedName,
|
|
|
|
&info->activeAttribs, &info->attribMap);
|
|
|
|
|
|
|
|
// Collect active locations:
|
|
|
|
GLint loc = gl->fGetAttribLocation(prog->mGLName, mappedName.BeginReading());
|
|
|
|
if (loc == -1)
|
|
|
|
MOZ_CRASH("Active attrib has no location.");
|
|
|
|
|
|
|
|
info->activeAttribLocs.insert(loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uniforms
|
|
|
|
|
|
|
|
const bool needsCheckForArrays = true;
|
|
|
|
|
|
|
|
GLuint numActiveUniforms = 0;
|
|
|
|
gl->fGetProgramiv(prog->mGLName, LOCAL_GL_ACTIVE_UNIFORMS,
|
|
|
|
(GLint*)&numActiveUniforms);
|
|
|
|
|
|
|
|
for (GLuint i = 0; i < numActiveUniforms; i++) {
|
|
|
|
nsAutoCString mappedName;
|
|
|
|
mappedName.SetLength(maxUniformLenWithNull - 1);
|
|
|
|
|
|
|
|
GLsizei lengthWithoutNull = 0;
|
|
|
|
GLint elemCount = 0; // `size`
|
|
|
|
GLenum elemType = 0; // `type`
|
|
|
|
gl->fGetActiveUniform(prog->mGLName, i, mappedName.Length()+1, &lengthWithoutNull,
|
|
|
|
&elemCount, &elemType, mappedName.BeginWriting());
|
|
|
|
|
|
|
|
mappedName.SetLength(lengthWithoutNull);
|
|
|
|
|
|
|
|
nsAutoCString baseMappedName;
|
|
|
|
bool isArray;
|
|
|
|
size_t arrayIndex;
|
|
|
|
if (!ParseName(mappedName, &baseMappedName, &isArray, &arrayIndex))
|
|
|
|
MOZ_CRASH("Failed to parse `mappedName` received from driver.");
|
|
|
|
|
|
|
|
// Note that for good drivers, `isArray` should already be correct.
|
|
|
|
// However, if FindUniform succeeds, it will be validator-guaranteed correct.
|
|
|
|
|
|
|
|
nsAutoCString baseUserName;
|
|
|
|
if (!prog->FindUniformByMappedName(baseMappedName, &baseUserName, &isArray)) {
|
|
|
|
baseUserName = baseMappedName;
|
|
|
|
|
|
|
|
if (needsCheckForArrays && !isArray) {
|
|
|
|
// By GLES 3, GetUniformLocation("foo[0]") should return -1 if `foo` is
|
|
|
|
// not an array. Our current linux Try slaves return the location of `foo`
|
|
|
|
// anyways, though.
|
|
|
|
std::string mappedName = baseMappedName.BeginReading();
|
|
|
|
mappedName += "[0]";
|
|
|
|
|
|
|
|
GLint loc = gl->fGetUniformLocation(prog->mGLName, mappedName.c_str());
|
|
|
|
if (loc != -1)
|
|
|
|
isArray = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DUMP_SHADERVAR_MAPPINGS
|
|
|
|
printf_stderr("[uniform %i] %s/%i/%s/%s\n", i, mappedName.BeginReading(),
|
|
|
|
(int)isArray, baseMappedName.BeginReading(),
|
|
|
|
baseUserName.BeginReading());
|
|
|
|
printf_stderr(" lengthWithoutNull: %d\n", lengthWithoutNull);
|
|
|
|
printf_stderr(" isArray: %d\n", (int)isArray);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
AddActiveInfo(prog->Context(), elemCount, elemType, isArray, baseUserName,
|
|
|
|
baseMappedName, &info->activeUniforms, &info->uniformMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return info.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
webgl::LinkedProgramInfo::LinkedProgramInfo(WebGLProgram* aProg)
|
|
|
|
: prog(aProg)
|
2015-01-27 04:10:25 +03:00
|
|
|
, fragDataMap(nullptr)
|
2015-01-16 02:40:39 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WebGLProgram
|
|
|
|
|
|
|
|
static GLuint
|
|
|
|
CreateProgram(gl::GLContext* gl)
|
|
|
|
{
|
|
|
|
gl->MakeCurrent();
|
|
|
|
return gl->fCreateProgram();
|
2012-10-05 00:35:54 +04:00
|
|
|
}
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLProgram::WebGLProgram(WebGLContext* webgl)
|
|
|
|
: WebGLContextBoundObject(webgl)
|
2015-01-16 02:40:39 +03:00
|
|
|
, mGLName(CreateProgram(webgl->GL()))
|
2012-12-09 00:41:02 +04:00
|
|
|
{
|
|
|
|
mContext->mPrograms.insertBack(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLProgram::Delete()
|
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
gl::GLContext* gl = mContext->GL();
|
|
|
|
|
|
|
|
gl->MakeCurrent();
|
|
|
|
gl->fDeleteProgram(mGLName);
|
|
|
|
|
|
|
|
mVertShader = nullptr;
|
|
|
|
mFragShader = nullptr;
|
|
|
|
|
|
|
|
mMostRecentLinkInfo = nullptr;
|
|
|
|
|
2012-12-09 00:41:02 +04:00
|
|
|
LinkedListElement<WebGLProgram>::removeFrom(mContext->mPrograms);
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// GL funcs
|
|
|
|
|
|
|
|
void
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLProgram::AttachShader(WebGLShader* shader)
|
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
WebGLRefPtr<WebGLShader>* shaderSlot;
|
|
|
|
switch (shader->mType) {
|
|
|
|
case LOCAL_GL_VERTEX_SHADER:
|
|
|
|
shaderSlot = &mVertShader;
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_FRAGMENT_SHADER:
|
|
|
|
shaderSlot = &mFragShader;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mContext->ErrorInvalidOperation("attachShader: Bad type for shader.");
|
|
|
|
return;
|
|
|
|
}
|
2012-12-09 00:41:02 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (*shaderSlot) {
|
|
|
|
if (shader == *shaderSlot) {
|
|
|
|
mContext->ErrorInvalidOperation("attachShader: `shader` is already attached.");
|
|
|
|
} else {
|
|
|
|
mContext->ErrorInvalidOperation("attachShader: Only one of each type of"
|
|
|
|
" shader may be attached to a program.");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-01-10 05:40:56 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
*shaderSlot = shader;
|
2015-01-13 06:51:20 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
mContext->MakeContextCurrent();
|
|
|
|
mContext->gl->fAttachShader(mGLName, shader->mGLName);
|
2012-12-09 00:41:02 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
void
|
|
|
|
WebGLProgram::BindAttribLocation(GLuint loc, const nsAString& name)
|
2014-11-14 07:03:50 +03:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
if (!ValidateGLSLVariableName(name, mContext, "bindAttribLocation"))
|
|
|
|
return;
|
2015-01-13 06:51:20 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (loc >= mContext->MaxVertexAttribs()) {
|
|
|
|
mContext->ErrorInvalidValue("bindAttribLocation: `location` must be less than"
|
|
|
|
" MAX_VERTEX_ATTRIBS.");
|
|
|
|
return;
|
|
|
|
}
|
2015-01-13 11:07:26 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (StringBeginsWith(name, NS_LITERAL_STRING("gl_"))) {
|
|
|
|
mContext->ErrorInvalidOperation("bindAttribLocation: Can't set the location of a"
|
|
|
|
" name that starts with 'gl_'.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_LossyConvertUTF16toASCII asciiName(name);
|
|
|
|
|
|
|
|
auto res = mBoundAttribLocs.insert(std::pair<nsCString, GLuint>(asciiName, loc));
|
|
|
|
|
|
|
|
const bool wasInserted = res.second;
|
|
|
|
if (!wasInserted) {
|
|
|
|
auto itr = res.first;
|
|
|
|
itr->second = loc;
|
|
|
|
}
|
2012-12-09 00:41:02 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
void
|
|
|
|
WebGLProgram::DetachShader(WebGLShader* shader)
|
2014-11-14 07:03:50 +03:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
MOZ_ASSERT(shader);
|
|
|
|
|
|
|
|
WebGLRefPtr<WebGLShader>* shaderSlot;
|
|
|
|
switch (shader->mType) {
|
|
|
|
case LOCAL_GL_VERTEX_SHADER:
|
|
|
|
shaderSlot = &mVertShader;
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_FRAGMENT_SHADER:
|
|
|
|
shaderSlot = &mFragShader;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mContext->ErrorInvalidOperation("attachShader: Bad type for shader.");
|
|
|
|
return;
|
2015-01-13 06:51:20 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (*shaderSlot != shader) {
|
|
|
|
mContext->ErrorInvalidOperation("detachShader: `shader` is not attached.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*shaderSlot = nullptr;
|
|
|
|
|
|
|
|
mContext->MakeContextCurrent();
|
|
|
|
mContext->gl->fDetachShader(mGLName, shader->mGLName);
|
2012-12-09 00:41:02 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
already_AddRefed<WebGLActiveInfo>
|
|
|
|
WebGLProgram::GetActiveAttrib(GLuint index) const
|
2014-11-14 07:03:50 +03:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
if (!mMostRecentLinkInfo) {
|
|
|
|
nsRefPtr<WebGLActiveInfo> ret = WebGLActiveInfo::CreateInvalid(mContext);
|
|
|
|
return ret.forget();
|
2015-01-13 06:51:20 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
const auto& activeList = mMostRecentLinkInfo->activeAttribs;
|
|
|
|
|
|
|
|
if (index >= activeList.size()) {
|
|
|
|
mContext->ErrorInvalidValue("`index` (%i) must be less than %s (%i).",
|
|
|
|
index, "ACTIVE_ATTRIBS", activeList.size());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<WebGLActiveInfo> ret = activeList[index];
|
|
|
|
return ret.forget();
|
2015-01-10 05:40:56 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
already_AddRefed<WebGLActiveInfo>
|
|
|
|
WebGLProgram::GetActiveUniform(GLuint index) const
|
2015-01-10 05:40:56 +03:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
if (!mMostRecentLinkInfo) {
|
|
|
|
nsRefPtr<WebGLActiveInfo> ret = WebGLActiveInfo::CreateInvalid(mContext);
|
|
|
|
return ret.forget();
|
2015-01-13 11:07:26 +03:00
|
|
|
}
|
2015-01-13 06:51:20 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
const auto& activeList = mMostRecentLinkInfo->activeUniforms;
|
|
|
|
|
|
|
|
if (index >= activeList.size()) {
|
|
|
|
mContext->ErrorInvalidValue("`index` (%i) must be less than %s (%i).",
|
|
|
|
index, "ACTIVE_UNIFORMS", activeList.size());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<WebGLActiveInfo> ret = activeList[index];
|
|
|
|
return ret.forget();
|
2015-01-13 06:51:20 +03:00
|
|
|
}
|
|
|
|
|
2015-01-13 11:07:26 +03:00
|
|
|
void
|
2015-01-16 02:40:39 +03:00
|
|
|
WebGLProgram::GetAttachedShaders(nsTArray<nsRefPtr<WebGLShader>>* const out) const
|
2015-01-13 06:51:20 +03:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
out->TruncateLength(0);
|
|
|
|
|
|
|
|
if (mVertShader)
|
|
|
|
out->AppendElement(mVertShader);
|
|
|
|
|
|
|
|
if (mFragShader)
|
|
|
|
out->AppendElement(mFragShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLint
|
|
|
|
WebGLProgram::GetAttribLocation(const nsAString& userName_wide) const
|
|
|
|
{
|
|
|
|
if (!ValidateGLSLVariableName(userName_wide, mContext, "getAttribLocation"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!IsLinked()) {
|
|
|
|
mContext->ErrorInvalidOperation("getAttribLocation: `program` must be linked.");
|
|
|
|
return -1;
|
2012-12-09 00:41:02 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
const NS_LossyConvertUTF16toASCII userName(userName_wide);
|
|
|
|
|
|
|
|
const WebGLActiveInfo* info;
|
|
|
|
if (!LinkInfo()->FindAttrib(userName, &info))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
const nsCString& mappedName = info->mBaseMappedName;
|
2015-01-13 06:51:20 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
gl::GLContext* gl = mContext->GL();
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
return gl->fGetAttribLocation(mGLName, mappedName.BeginReading());
|
2012-12-09 00:41:02 +04:00
|
|
|
}
|
|
|
|
|
2015-01-27 04:10:25 +03:00
|
|
|
GLint
|
|
|
|
WebGLProgram::GetFragDataLocation(const nsAString& userName_wide) const
|
|
|
|
{
|
|
|
|
if (!ValidateGLSLVariableName(userName_wide, mContext, "getFragDataLocation"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!IsLinked()) {
|
|
|
|
mContext->ErrorInvalidOperation("getFragDataLocation: `program` must be linked.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NS_LossyConvertUTF16toASCII userName(userName_wide);
|
|
|
|
|
|
|
|
nsCString mappedName;
|
|
|
|
if (!LinkInfo()->FindFragData(userName, &mappedName))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
gl::GLContext* gl = mContext->GL();
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
return gl->fGetFragDataLocation(mGLName, mappedName.BeginReading());
|
|
|
|
}
|
|
|
|
|
2012-12-09 00:41:02 +04:00
|
|
|
void
|
2015-01-16 02:40:39 +03:00
|
|
|
WebGLProgram::GetProgramInfoLog(nsAString* const out) const
|
2014-11-14 07:03:50 +03:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
CopyASCIItoUTF16(mLinkLog, *out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLint
|
|
|
|
GetProgramiv(gl::GLContext* gl, GLuint program, GLenum pname)
|
|
|
|
{
|
|
|
|
GLint ret = 0;
|
|
|
|
gl->fGetProgramiv(program, pname, &ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value
|
|
|
|
WebGLProgram::GetProgramParameter(GLenum pname) const
|
|
|
|
{
|
|
|
|
gl::GLContext* gl = mContext->gl;
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
if (mContext->IsWebGL2()) {
|
|
|
|
switch (pname) {
|
|
|
|
case LOCAL_GL_ACTIVE_UNIFORM_BLOCKS:
|
|
|
|
return JS::Int32Value(GetProgramiv(gl, mGLName, pname));
|
2012-12-09 00:41:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
switch (pname) {
|
|
|
|
case LOCAL_GL_ATTACHED_SHADERS:
|
|
|
|
case LOCAL_GL_ACTIVE_UNIFORMS:
|
|
|
|
case LOCAL_GL_ACTIVE_ATTRIBUTES:
|
|
|
|
return JS::Int32Value(GetProgramiv(gl, mGLName, pname));
|
|
|
|
|
|
|
|
case LOCAL_GL_DELETE_STATUS:
|
|
|
|
return JS::BooleanValue(IsDeleteRequested());
|
|
|
|
|
|
|
|
case LOCAL_GL_LINK_STATUS:
|
|
|
|
return JS::BooleanValue(IsLinked());
|
|
|
|
|
|
|
|
case LOCAL_GL_VALIDATE_STATUS:
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// See comment in ValidateProgram.
|
|
|
|
if (gl->WorkAroundDriverBugs())
|
|
|
|
return JS::BooleanValue(true);
|
|
|
|
#endif
|
|
|
|
return JS::BooleanValue(bool(GetProgramiv(gl, mGLName, pname)));
|
|
|
|
|
|
|
|
default:
|
|
|
|
mContext->ErrorInvalidEnumInfo("getProgramParameter: `pname`",
|
|
|
|
pname);
|
|
|
|
return JS::NullValue();
|
|
|
|
}
|
2012-12-09 00:41:02 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
already_AddRefed<WebGLUniformLocation>
|
|
|
|
WebGLProgram::GetUniformLocation(const nsAString& userName_wide) const
|
2014-11-14 07:03:50 +03:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
if (!ValidateGLSLVariableName(userName_wide, mContext, "getUniformLocation"))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (!IsLinked()) {
|
|
|
|
mContext->ErrorInvalidOperation("getUniformLocation: `program` must be linked.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NS_LossyConvertUTF16toASCII userName(userName_wide);
|
|
|
|
|
|
|
|
nsDependentCString baseUserName;
|
|
|
|
bool isArray;
|
|
|
|
size_t arrayIndex;
|
|
|
|
if (!ParseName(userName, &baseUserName, &isArray, &arrayIndex))
|
|
|
|
return nullptr;
|
2014-08-12 01:36:30 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
const WebGLActiveInfo* activeInfo;
|
|
|
|
if (!LinkInfo()->FindUniform(baseUserName, &activeInfo))
|
|
|
|
return nullptr;
|
2012-12-09 00:41:02 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
const nsCString& baseMappedName = activeInfo->mBaseMappedName;
|
2012-12-09 00:41:02 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
nsAutoCString mappedName(baseMappedName);
|
|
|
|
if (isArray) {
|
|
|
|
mappedName.AppendLiteral("[");
|
|
|
|
mappedName.AppendInt(uint32_t(arrayIndex));
|
|
|
|
mappedName.AppendLiteral("]");
|
|
|
|
}
|
|
|
|
|
|
|
|
gl::GLContext* gl = mContext->GL();
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
GLint loc = gl->fGetUniformLocation(mGLName, mappedName.BeginReading());
|
|
|
|
if (loc == -1)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
nsRefPtr<WebGLUniformLocation> locObj = new WebGLUniformLocation(mContext, LinkInfo(),
|
|
|
|
loc, activeInfo);
|
|
|
|
return locObj.forget();
|
2012-12-09 00:41:02 +04:00
|
|
|
}
|
|
|
|
|
2014-10-04 01:36:57 +04:00
|
|
|
bool
|
2015-01-16 02:40:39 +03:00
|
|
|
WebGLProgram::LinkProgram()
|
2014-10-04 01:36:57 +04:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
mContext->InvalidateBufferFetching(); // we do it early in this function
|
|
|
|
// as some of the validation below changes program state
|
|
|
|
|
|
|
|
mLinkLog.Truncate();
|
|
|
|
mMostRecentLinkInfo = nullptr;
|
|
|
|
|
|
|
|
if (!mVertShader || !mVertShader->IsCompiled()) {
|
|
|
|
mLinkLog.AssignLiteral("Must have a compiled vertex shader attached.");
|
|
|
|
mContext->GenerateWarning("linkProgram: %s", mLinkLog.BeginReading());
|
|
|
|
return false;
|
2015-01-13 06:51:20 +03:00
|
|
|
}
|
2014-10-04 01:36:57 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (!mFragShader || !mFragShader->IsCompiled()) {
|
|
|
|
mLinkLog.AssignLiteral("Must have an compiled fragment shader attached.");
|
|
|
|
mContext->GenerateWarning("linkProgram: %s", mLinkLog.BeginReading());
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-10 05:40:56 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (!mFragShader->CanLinkTo(mVertShader, &mLinkLog)) {
|
|
|
|
mContext->GenerateWarning("linkProgram: %s", mLinkLog.BeginReading());
|
2015-01-13 06:51:20 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
gl::GLContext* gl = mContext->gl;
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
// Bug 777028: Mesa can't handle more than 16 samplers per program,
|
|
|
|
// counting each array entry.
|
|
|
|
size_t numSamplerUniforms_upperBound = mVertShader->CalcNumSamplerUniforms() +
|
|
|
|
mFragShader->CalcNumSamplerUniforms();
|
|
|
|
if (gl->WorkAroundDriverBugs() &&
|
|
|
|
mContext->mIsMesa &&
|
|
|
|
numSamplerUniforms_upperBound > 16)
|
|
|
|
{
|
|
|
|
mLinkLog.AssignLiteral("Programs with more than 16 samplers are disallowed on"
|
|
|
|
" Mesa drivers to avoid crashing.");
|
|
|
|
mContext->GenerateWarning("linkProgram: %s", mLinkLog.BeginReading());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind the attrib locations.
|
|
|
|
// This can't be done trivially, because we have to deal with mapped attrib names.
|
|
|
|
for (auto itr = mBoundAttribLocs.begin(); itr != mBoundAttribLocs.end(); ++itr) {
|
|
|
|
const nsCString& name = itr->first;
|
|
|
|
GLuint index = itr->second;
|
|
|
|
|
|
|
|
mVertShader->BindAttribLocation(mGLName, name, index);
|
2015-01-13 06:51:20 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (LinkAndUpdate())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Failed link.
|
|
|
|
if (mContext->ShouldGenerateWarnings()) {
|
|
|
|
// report shader/program infoLogs as warnings.
|
|
|
|
// note that shader compilation errors can be deferred to linkProgram,
|
|
|
|
// which is why we can't do anything in compileShader. In practice we could
|
|
|
|
// report in compileShader the translation errors generated by ANGLE,
|
|
|
|
// but it seems saner to keep a single way of obtaining shader infologs.
|
|
|
|
if (!mLinkLog.IsEmpty()) {
|
|
|
|
mContext->GenerateWarning("linkProgram: Failed to link, leaving the following"
|
|
|
|
" log:\n%s\n",
|
|
|
|
mLinkLog.BeginReading());
|
2015-01-13 11:07:26 +03:00
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebGLProgram::UseProgram() const
|
|
|
|
{
|
|
|
|
if (!mMostRecentLinkInfo) {
|
|
|
|
mContext->ErrorInvalidOperation("useProgram: Program has not been successfully"
|
|
|
|
" linked.");
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-04 01:36:57 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
mContext->MakeContextCurrent();
|
2015-01-10 05:40:56 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
mContext->InvalidateBufferFetching();
|
2015-01-13 06:51:20 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
mContext->gl->fUseProgram(mGLName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLProgram::ValidateProgram() const
|
|
|
|
{
|
|
|
|
mContext->MakeContextCurrent();
|
|
|
|
gl::GLContext* gl = mContext->gl;
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// See bug 593867 for NVIDIA and bug 657201 for ATI. The latter is confirmed
|
|
|
|
// with Mac OS 10.6.7.
|
|
|
|
if (gl->WorkAroundDriverBugs()) {
|
|
|
|
mContext->GenerateWarning("validateProgram: Implemented as a no-op on"
|
|
|
|
" Mac to work around crashes.");
|
|
|
|
return;
|
2014-10-04 01:36:57 +04:00
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
#endif
|
2014-10-04 01:36:57 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
gl->fValidateProgram(mGLName);
|
|
|
|
}
|
2014-10-04 01:36:57 +04:00
|
|
|
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-10-04 01:36:57 +04:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
bool
|
|
|
|
WebGLProgram::LinkAndUpdate()
|
|
|
|
{
|
|
|
|
mMostRecentLinkInfo = nullptr;
|
|
|
|
|
|
|
|
gl::GLContext* gl = mContext->gl;
|
|
|
|
gl->fLinkProgram(mGLName);
|
|
|
|
|
|
|
|
// Grab the program log.
|
|
|
|
GLuint logLenWithNull = 0;
|
|
|
|
gl->fGetProgramiv(mGLName, LOCAL_GL_INFO_LOG_LENGTH, (GLint*)&logLenWithNull);
|
|
|
|
if (logLenWithNull > 1) {
|
|
|
|
mLinkLog.SetLength(logLenWithNull - 1);
|
|
|
|
gl->fGetProgramInfoLog(mGLName, logLenWithNull, nullptr, mLinkLog.BeginWriting());
|
|
|
|
} else {
|
|
|
|
mLinkLog.SetLength(0);
|
2014-10-04 01:36:57 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
GLint ok = 0;
|
|
|
|
gl->fGetProgramiv(mGLName, LOCAL_GL_LINK_STATUS, &ok);
|
|
|
|
if (!ok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mMostRecentLinkInfo = QueryProgramInfo(this, gl);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mMostRecentLinkInfo);
|
|
|
|
if (!mMostRecentLinkInfo)
|
|
|
|
mLinkLog.AssignLiteral("Failed to gather program info.");
|
|
|
|
|
|
|
|
return mMostRecentLinkInfo;
|
2015-01-13 06:51:20 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
bool
|
|
|
|
WebGLProgram::FindAttribUserNameByMappedName(const nsACString& mappedName,
|
|
|
|
nsDependentCString* const out_userName) const
|
2015-01-13 06:51:20 +03:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
if (mVertShader->FindAttribUserNameByMappedName(mappedName, out_userName))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebGLProgram::FindUniformByMappedName(const nsACString& mappedName,
|
|
|
|
nsCString* const out_userName,
|
|
|
|
bool* const out_isArray) const
|
|
|
|
{
|
|
|
|
if (mVertShader->FindUniformByMappedName(mappedName, out_userName, out_isArray))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (mFragShader->FindUniformByMappedName(mappedName, out_userName, out_isArray))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2014-05-14 04:15:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
WebGLProgram::WrapObject(JSContext* js)
|
2014-05-14 04:15:19 +04:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
return dom::WebGLProgramBinding::Wrap(js, this);
|
2014-05-14 04:15:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(WebGLProgram, mVertShader, mFragShader)
|
2012-10-05 00:35:54 +04:00
|
|
|
|
2013-08-29 19:39:17 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLProgram, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLProgram, Release)
|
2014-11-14 07:03:50 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|