• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 
28 #if ENABLE(WEBGL)
29 
30 #include "ANGLEWebKitBridge.h"
31 #include <wtf/OwnArrayPtr.h>
32 
33 namespace WebCore {
34 
35 
ANGLEWebKitBridge()36 ANGLEWebKitBridge::ANGLEWebKitBridge() :
37     builtCompilers(false),
38     m_fragmentCompiler(0),
39     m_vertexCompiler(0)
40 {
41     ShInitialize();
42 }
43 
~ANGLEWebKitBridge()44 ANGLEWebKitBridge::~ANGLEWebKitBridge()
45 {
46     cleanupCompilers();
47 }
48 
cleanupCompilers()49 void ANGLEWebKitBridge::cleanupCompilers()
50 {
51     if (m_fragmentCompiler)
52         ShDestruct(m_fragmentCompiler);
53     m_fragmentCompiler = 0;
54     if (m_vertexCompiler)
55         ShDestruct(m_vertexCompiler);
56     m_vertexCompiler = 0;
57 
58     builtCompilers = false;
59 }
60 
setResources(ShBuiltInResources resources)61 void ANGLEWebKitBridge::setResources(ShBuiltInResources resources)
62 {
63     // Resources are (possibly) changing - cleanup compilers if we had them already
64     cleanupCompilers();
65 
66     m_resources = resources;
67 }
68 
validateShaderSource(const char * shaderSource,ANGLEShaderType shaderType,String & translatedShaderSource,String & shaderValidationLog)69 bool ANGLEWebKitBridge::validateShaderSource(const char* shaderSource, ANGLEShaderType shaderType, String& translatedShaderSource, String& shaderValidationLog)
70 {
71     if (!builtCompilers) {
72         m_fragmentCompiler = ShConstructCompiler(SH_FRAGMENT_SHADER, SH_WEBGL_SPEC, &m_resources);
73         m_vertexCompiler = ShConstructCompiler(SH_VERTEX_SHADER, SH_WEBGL_SPEC, &m_resources);
74         if (!m_fragmentCompiler || !m_vertexCompiler) {
75             cleanupCompilers();
76             return false;
77         }
78 
79         builtCompilers = true;
80     }
81 
82     ShHandle compiler;
83 
84     if (shaderType == SHADER_TYPE_VERTEX)
85         compiler = m_vertexCompiler;
86     else
87         compiler = m_fragmentCompiler;
88 
89     const char* const shaderSourceStrings[] = { shaderSource };
90 
91     bool validateSuccess = ShCompile(compiler, shaderSourceStrings, 1, SH_OBJECT_CODE);
92     if (!validateSuccess) {
93         int logSize = 0;
94         ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &logSize);
95         if (logSize > 1) {
96             OwnArrayPtr<char> logBuffer = adoptArrayPtr(new char[logSize]);
97             if (logBuffer) {
98                 ShGetInfoLog(compiler, logBuffer.get());
99                 shaderValidationLog = logBuffer.get();
100             }
101         }
102         return false;
103     }
104 
105     int translationLength = 0;
106     ShGetInfo(compiler, SH_OBJECT_CODE_LENGTH, &translationLength);
107     if (translationLength > 1) {
108         OwnArrayPtr<char> translationBuffer = adoptArrayPtr(new char[translationLength]);
109         if (!translationBuffer)
110             return false;
111         ShGetObjectCode(compiler, translationBuffer.get());
112         translatedShaderSource = translationBuffer.get();
113     }
114 
115     return true;
116 }
117 
118 }
119 
120 #endif // ENABLE(WEBGL)
121