1 /****************************************************************************
  2  Copyright (c) 2008-2010 Ricardo Quesada
  3  Copyright (c) 2011-2012 cocos2d-x.org
  4  Copyright (c) 2013-2014 Chukong Technologies Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * cc.configuration contains some openGL variables
 29  * @namespace
 30  * @name cc.configuration
 31  */
 32 cc.configuration = /** @lends cc.configuration# */{
 33 	// Type constants
 34 	/*
 35 	 * ERROR type
 36 	 * @public
 37 	 * @const
 38 	 * @type {Number}
 39 	 */
 40 	ERROR:0,
 41 
 42 	/*
 43 	 * STRING type
 44 	 * @public
 45 	 * @const
 46 	 * @type {Number}
 47 	 */
 48 	STRING:1,
 49 
 50 	/*
 51 	 * INT type
 52 	 * @public
 53 	 * @const
 54 	 * @type {Number}
 55 	 */
 56 	INT:2,
 57 
 58 	/*
 59 	 * DOUBLE type
 60 	 * @public
 61 	 * @const
 62 	 * @type {Number}
 63 	 */
 64 	DOUBLE:3,
 65 
 66 	/*
 67 	 * BOOLEAN type
 68 	 * @public
 69 	 * @const
 70 	 * @type {Number}
 71 	 */
 72 	BOOLEAN:4,
 73 
 74     _maxTextureSize:0,
 75     _maxModelviewStackDepth:0,
 76     _supportsPVRTC:false,
 77     _supportsNPOT:false,
 78     _supportsBGRA8888:false,
 79     _supportsDiscardFramebuffer:false,
 80     _supportsShareableVAO:false,
 81     _maxSamplesAllowed:0,
 82     _maxTextureUnits:0,
 83     _GlExtensions:"",
 84     _valueDict:{},
 85 
 86 	_inited: false,
 87 
 88 	_init:function () {
 89 		var locValueDict = this._valueDict;
 90 		locValueDict["cocos2d.x.version"] = cc.ENGINE_VERSION;
 91 		locValueDict["cocos2d.x.compiled_with_profiler"] = false;
 92 		locValueDict["cocos2d.x.compiled_with_gl_state_cache"] = cc.ENABLE_GL_STATE_CACHE;
 93 		this._inited = true;
 94 	},
 95 
 96     /**
 97      * OpenGL Max texture size.
 98      * @return {Number}
 99      */
100     getMaxTextureSize:function () {
101         return this._maxTextureSize;
102     },
103 
104     /**
105      * OpenGL Max Modelview Stack Depth.
106      * @return {Number}
107      */
108     getMaxModelviewStackDepth:function () {
109         return this._maxModelviewStackDepth;
110     },
111 
112     /**
113      * returns the maximum texture units
114      * @return {Number}
115      */
116     getMaxTextureUnits:function () {
117         return this._maxTextureUnits;
118     },
119 
120     /**
121      * Whether or not the GPU supports NPOT (Non Power Of Two) textures.
122      * OpenGL ES 2.0 already supports NPOT (iOS).
123      * @return {Boolean}
124      */
125     supportsNPOT:function () {
126         return this._supportsNPOT;
127     },
128 
129     /**
130      * Whether or not PVR Texture Compressed is supported
131      * @return {Boolean}
132      */
133     supportsPVRTC: function () {
134         return this._supportsPVRTC;
135     },
136 
137 	/**
138 	 * Whether or not ETC Texture Compressed is supported
139 	 * @return {Boolean}
140 	 */
141 	supportsETC: function() {
142 		return false;
143 	},
144 
145 	/**
146 	 * Whether or not S3TC Texture Compressed is supported
147 	 * @return {Boolean}
148 	 */
149 	supportsS3TC: function() {
150 		return false;
151 	},
152 
153 	/**
154 	 * Whether or not ATITC Texture Compressed is supported
155 	 * @return {Boolean}
156 	 */
157 	supportsATITC: function() {
158 		return false;
159 	},
160 
161     /**
162      * Whether or not BGRA8888 textures are supported.
163      * @return {Boolean}
164      */
165     supportsBGRA8888:function () {
166         return this._supportsBGRA8888;
167     },
168 
169     /**
170      * Whether or not glDiscardFramebufferEXT is supported
171      * @return {Boolean}
172      */
173     supportsDiscardFramebuffer:function () {
174         return this._supportsDiscardFramebuffer;
175     },
176 
177     /**
178      * Whether or not shareable VAOs are supported.
179      * @return {Boolean}
180      */
181     supportsShareableVAO:function () {
182         return this._supportsShareableVAO;
183     },
184 
185     /**
186      * returns whether or not an OpenGL is supported
187      * @param {String} searchName
188      */
189     checkForGLExtension:function (searchName) {
190         return this._GlExtensions.indexOf(searchName) > -1;
191     },
192 
193     /**
194      * Returns the value of a given key.  If the key is not found, it will return the default value
195      * @param {String} key
196      * @param {String|Bool|Number|Object} [default_value=null]
197      * @returns {String|Bool|Number|Object}
198      */
199     getValue: function(key, default_value){
200 	    if(!this._inited)
201 		    this._init();
202         var locValueDict = this._valueDict;
203         if(locValueDict[key])
204             return locValueDict[key];
205         return default_value;
206     },
207 
208     /**
209      * Sets a new key/value pair  in the configuration dictionary
210      * @param {string} key
211      * @param {String|Bool|Number|Object} value
212      */
213     setValue: function(key, value){
214         this._valueDict[key] = value;
215     },
216 
217     /**
218      * Dumps the current configuration on the console
219      */
220     dumpInfo: function(){
221          if(cc.ENABLE_GL_STATE_CACHE === 0){
222              cc.log("");
223              cc.log(cc._LogInfos.configuration_dumpInfo);
224              cc.log("")
225          }
226     },
227 
228     /**
229      * gathers OpenGL / GPU information
230      */
231     gatherGPUInfo: function(){
232         if(cc._renderType === cc._RENDER_TYPE_CANVAS)
233             return;
234 
235 	    if(!this._inited)
236 		    this._init();
237         var gl = cc._renderContext;
238         var locValueDict = this._valueDict;
239         locValueDict["gl.vendor"] = gl.getParameter(gl.VENDOR);
240         locValueDict["gl.renderer"] = gl.getParameter(gl.RENDERER);
241         locValueDict["gl.version"] = gl.getParameter(gl.VERSION);
242 
243         this._GlExtensions = "";
244         var extArr = gl.getSupportedExtensions();
245         for (var i = 0; i < extArr.length; i++)
246             this._GlExtensions += extArr[i] + " ";
247 
248         this._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
249         locValueDict["gl.max_texture_size"] = this._maxTextureSize;
250         this._maxTextureUnits = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
251         locValueDict["gl.max_texture_units"] = this._maxTextureUnits;
252 
253         this._supportsPVRTC = this.checkForGLExtension("GL_IMG_texture_compression_pvrtc");
254         locValueDict["gl.supports_PVRTC"] = this._supportsPVRTC;
255 
256         this._supportsNPOT = false; //true;
257         locValueDict["gl.supports_NPOT"] = this._supportsNPOT;
258 
259         this._supportsBGRA8888 = this.checkForGLExtension("GL_IMG_texture_format_BGRA888");
260         locValueDict["gl.supports_BGRA8888"] = this._supportsBGRA8888;
261 
262         this._supportsDiscardFramebuffer = this.checkForGLExtension("GL_EXT_discard_framebuffer");
263         locValueDict["gl.supports_discard_framebuffer"] = this._supportsDiscardFramebuffer;
264 
265         this._supportsShareableVAO = this.checkForGLExtension("vertex_array_object");
266         locValueDict["gl.supports_vertex_array_object"] = this._supportsShareableVAO;
267 
268         cc.checkGLErrorDebug();
269     },
270 
271     /**
272      * Loads a config file. If the keys are already present, then they are going to be replaced. Otherwise the new keys are added.
273      * @param {string} url
274      */
275     loadConfigFile: function( url){
276 	    if(!this._inited)
277 		    this._init();
278         var dict = cc.loader.getRes(url);
279         if(!dict) throw "Please load the resource first : " + url;
280         cc.assert(dict, cc._LogInfos.configuration_loadConfigFile_2, url);
281 
282         var getDatas = dict["data"];
283         if(!getDatas){
284             cc.log(cc._LogInfos.configuration_loadConfigFile, url);
285             return;
286         }
287 
288         // Add all keys in the existing dictionary
289         for(var selKey in getDatas)
290             this._valueDict[selKey] = getDatas[selKey];
291     }
292 };