1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WebGLShader.h"
8 #include "GLSLANG/ShaderLang.h"
10 #include "mozilla/dom/WebGLRenderingContextBinding.h"
11 #include "mozilla/MemoryReporting.h"
12 #include "nsPrintfCString.h"
15 #include "WebGLContext.h"
16 #include "WebGLObjectModel.h"
17 #include "WebGLShaderValidator.h"
18 #include "WebGLValidateStrings.h"
22 static void PrintLongString(const char* const begin
, const size_t len
) {
23 // Wow - Roll Your Own Foreach-Lines because printf_stderr has a hard-coded
24 // internal size, so long strings are truncated.
26 const size_t chunkSize
= 1000;
27 const UniqueBuffer
buf(moz_xmalloc(chunkSize
+ 1)); // +1 for null-term
28 const auto bufBegin
= (char*)buf
.get();
29 bufBegin
[chunkSize
] = '\0';
31 auto chunkBegin
= begin
;
32 const auto end
= begin
+ len
;
33 while (chunkBegin
+ chunkSize
< end
) {
34 memcpy(bufBegin
, chunkBegin
, chunkSize
);
35 printf_stderr("%s", bufBegin
);
36 chunkBegin
+= chunkSize
;
38 printf_stderr("%s", chunkBegin
);
42 static bool SubstringStartsWith(const std::string
& testStr
, size_t offset
,
43 const char (&refStr
)[N
]) {
44 for (size_t i
= 0; i
< N
- 1; i
++) {
45 if (testStr
[offset
+ i
] != refStr
[i
]) return false;
50 static void GetCompilationStatusAndLog(gl::GLContext
* gl
, GLuint shader
,
51 bool* const out_success
,
52 std::string
* const out_log
) {
53 GLint compileStatus
= LOCAL_GL_FALSE
;
54 gl
->fGetShaderiv(shader
, LOCAL_GL_COMPILE_STATUS
, &compileStatus
);
56 // It's simpler if we always get the log.
57 GLint lenWithNull
= 0;
58 gl
->fGetShaderiv(shader
, LOCAL_GL_INFO_LOG_LENGTH
, &lenWithNull
);
59 if (lenWithNull
< 1) {
62 std::vector
<char> buffer(lenWithNull
);
63 gl
->fGetShaderInfoLog(shader
, buffer
.size(), nullptr, buffer
.data());
64 *out_log
= buffer
.data();
66 *out_success
= (compileStatus
== LOCAL_GL_TRUE
);
69 ////////////////////////////////////////////////////////////////////////////////
71 WebGLShader::WebGLShader(WebGLContext
* webgl
, GLenum type
)
72 : WebGLContextBoundObject(webgl
),
73 mGLName(webgl
->gl
->fCreateShader(type
)),
75 mCompileResults
= std::make_unique
<webgl::ShaderValidatorResults
>();
78 WebGLShader::~WebGLShader() {
79 if (!mContext
) return;
80 mContext
->gl
->fDeleteShader(mGLName
);
83 void WebGLShader::ShaderSource(const std::string
& u8
) {
84 mSource
= CrushGlslToAscii(u8
);
87 void WebGLShader::CompileShader() {
88 mCompilationSuccessful
= false;
90 gl::GLContext
* gl
= mContext
->gl
;
92 static const bool kDumpShaders
= PR_GetEnv("MOZ_WEBGL_DUMP_SHADERS");
93 if (MOZ_UNLIKELY(kDumpShaders
)) {
94 printf_stderr("==== begin MOZ_WEBGL_DUMP_SHADERS ====\n");
95 PrintLongString(mSource
.c_str(), mSource
.size());
99 const auto validator
= mContext
->CreateShaderValidator(mType
);
100 MOZ_ASSERT(validator
);
102 mCompileResults
= validator
->ValidateAndTranslate(mSource
.c_str());
105 mCompilationLog
= mCompileResults
->mInfoLog
.c_str();
106 const auto& success
= mCompileResults
->mValid
;
108 if (MOZ_UNLIKELY(kDumpShaders
)) {
109 printf_stderr("\n==== \\/ \\/ \\/ ====\n");
111 const auto& translated
= mCompileResults
->mObjectCode
;
112 PrintLongString(translated
.data(), translated
.size());
114 printf_stderr("Validation failed:\n%s",
115 mCompileResults
->mInfoLog
.c_str());
117 printf_stderr("\n==== end ====\n");
120 if (!success
) return;
122 const std::array
<const char*, 1> parts
= {
123 mCompileResults
->mObjectCode
.c_str()};
124 gl
->fShaderSource(mGLName
, parts
.size(), parts
.data(), nullptr);
126 gl
->fCompileShader(mGLName
);
128 GetCompilationStatusAndLog(gl
, mGLName
, &mCompilationSuccessful
,
132 ////////////////////////////////////////////////////////////////////////////////
134 size_t WebGLShader::CalcNumSamplerUniforms() const {
136 for (const auto& cur
: mCompileResults
->mUniforms
) {
137 const auto& type
= cur
.type
;
138 if (type
== LOCAL_GL_SAMPLER_2D
|| type
== LOCAL_GL_SAMPLER_CUBE
) {
139 accum
+= cur
.getArraySizeProduct();
145 size_t WebGLShader::NumAttributes() const {
146 return mCompileResults
->mAttributes
.size();
149 void WebGLShader::BindAttribLocation(GLuint prog
, const std::string
& userName
,
150 GLuint index
) const {
151 for (const auto& attrib
: mCompileResults
->mAttributes
) {
152 if (attrib
.name
== userName
) {
153 mContext
->gl
->fBindAttribLocation(prog
, index
, attrib
.mappedName
.c_str());
159 void WebGLShader::MapTransformFeedbackVaryings(
160 const std::vector
<std::string
>& varyings
,
161 std::vector
<std::string
>* out_mappedVaryings
) const {
162 MOZ_ASSERT(mType
== LOCAL_GL_VERTEX_SHADER
);
163 MOZ_ASSERT(out_mappedVaryings
);
165 out_mappedVaryings
->clear();
166 out_mappedVaryings
->reserve(varyings
.size());
168 const auto& shaderVaryings
= mCompileResults
->mVaryings
;
170 for (const auto& userName
: varyings
) {
171 const auto* mappedName
= &userName
;
172 for (const auto& shaderVarying
: shaderVaryings
) {
173 if (shaderVarying
.name
== userName
) {
174 mappedName
= &shaderVarying
.mappedName
;
178 out_mappedVaryings
->push_back(*mappedName
);
182 ////////////////////////////////////////////////////////////////////////////////
185 size_t WebGLShader::SizeOfIncludingThis(MallocSizeOf mallocSizeOf
) const {
186 return mallocSizeOf(this) + mSource
.size() + 1 +
187 mCompileResults
->SizeOfIncludingThis(mallocSizeOf
) +
188 mCompilationLog
.size() + 1;
191 } // namespace mozilla