starling-framework
Version:
A fast, productive library for 2D cross-platform development.
237 lines (219 loc) • 7.4 kB
JavaScript
// Class: starling.utils.RenderUtil
var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this
$global.Object.defineProperty(exports, "__esModule", {value: true});
var __map_reserved = {};
// Imports
var $hxClasses = require("./../../hxClasses_stub").default;
var $hxEnums = require("./../../hxEnums_stub").default;
var $import = require("./../../import_stub").default;
var $bind = require("./../../bind_stub").default;
function starling_core_Starling() {return require("./../../starling/core/Starling");}
function starling_utils_Color() {return require("./../../starling/utils/Color");}
function starling_utils_SystemUtil() {return require("./../../starling/utils/SystemUtil");}
function starling_utils_Execute() {return require("./../../starling/utils/Execute");}
function Std() {return require("./../../Std");}
function js_Boot() {return require("./../../js/Boot");}
function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");}
function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));}
function openfl_errors_Error() {return $import(require("openfl/errors/Error"));}
function haxe_Timer() {return require("./../../haxe/Timer");}
// Constructor
var RenderUtil = function() {
}
// Meta
RenderUtil.__name__ = "starling.utils.RenderUtil";
RenderUtil.__isInterface__ = false;
RenderUtil.prototype = {
};
RenderUtil.prototype.__class__ = RenderUtil.prototype.constructor = $hxClasses["starling.utils.RenderUtil"] = RenderUtil;
// Init
// Statics
RenderUtil.clear = function(rgb,alpha,depth,stencil) {
if(stencil == null) {
stencil = 0;
}
if(depth == null) {
depth = 1.0;
}
if(alpha == null) {
alpha = 0.0;
}
if(rgb == null) {
rgb = 0;
}
(starling_core_Starling().default).get_current().get_context().clear((starling_utils_Color().default).getRed(rgb) / 255.0,(starling_utils_Color().default).getGreen(rgb) / 255.0,(starling_utils_Color().default).getBlue(rgb) / 255.0,alpha,depth,stencil);
}
RenderUtil.getTextureLookupFlags = function(format,mipMapping,repeat,smoothing) {
if(smoothing == null) {
smoothing = "bilinear";
}
if(repeat == null) {
repeat = false;
}
var options = ["2d",repeat ? "repeat" : "clamp"];
if(format == "compressed") {
options.push("dxt1");
} else if(format == "compressedAlpha") {
options.push("dxt5");
}
if(smoothing == "none") {
options.push("nearest");
options.push(mipMapping ? "mipnearest" : "mipnone");
} else if(smoothing == "bilinear") {
options.push("linear");
options.push(mipMapping ? "mipnearest" : "mipnone");
} else {
options.push("linear");
options.push(mipMapping ? "miplinear" : "mipnone");
}
return "<" + options.join(",") + ">";
}
RenderUtil.getTextureVariantBits = function(texture) {
if(texture == null) {
return 0;
}
var bitField = 0;
var formatBits = 0;
switch(texture.get_format()) {
case "compressed":
formatBits = 2;
break;
case "compressedAlpha":
formatBits = 3;
break;
default:
formatBits = 1;
}
bitField = bitField | formatBits;
if(!texture.get_premultipliedAlpha()) {
bitField = bitField | 4;
}
return bitField;
}
RenderUtil.setSamplerStateAt = function(sampler,mipMapping,smoothing,repeat) {
if(repeat == null) {
repeat = false;
}
if(smoothing == null) {
smoothing = "bilinear";
}
var wrap = repeat ? "repeat" : "clamp";
var filter;
var mipFilter;
if(smoothing == "none") {
filter = "nearest";
mipFilter = mipMapping ? "mipnearest" : "mipnone";
} else if(smoothing == "bilinear") {
filter = "linear";
mipFilter = mipMapping ? "mipnearest" : "mipnone";
} else {
filter = "linear";
mipFilter = mipMapping ? "miplinear" : "mipnone";
}
(starling_core_Starling().default).get_current().get_context().setSamplerStateAt(sampler,wrap,filter,mipFilter);
}
RenderUtil.createAGALTexOperation = function(resultReg,uvReg,sampler,texture,convertToPmaIfRequired,tempReg) {
if(tempReg == null) {
tempReg = "ft0";
}
if(convertToPmaIfRequired == null) {
convertToPmaIfRequired = true;
}
var format = texture.get_format();
var formatFlag;
switch(format) {
case "compressed":
formatFlag = "dxt1";
break;
case "compressedAlpha":
formatFlag = "dxt5";
break;
default:
formatFlag = "rgba";
}
var needsConversion = convertToPmaIfRequired && !texture.get_premultipliedAlpha();
var texReg = needsConversion && resultReg == "oc" ? tempReg : resultReg;
var operation = "tex " + texReg + ", " + uvReg + ", fs" + sampler + " <2d, " + formatFlag + ">\n";
if(needsConversion) {
if(resultReg == "oc") {
operation += "mul " + texReg + ".xyz, " + texReg + ".xyz, " + texReg + ".www\n";
operation += "mov " + resultReg + ", " + texReg;
} else {
operation += "mul " + resultReg + ".xyz, " + texReg + ".xyz, " + texReg + ".www\n";
}
}
return operation;
}
RenderUtil.requestContext3D = function(stage3D,renderMode,profile) {
var profiles;
var currentProfile;
var executeFunc = (starling_utils_SystemUtil().default).get_isDesktop() ? (starling_utils_Execute().default).execute : (starling_utils_SystemUtil().default).executeWhenApplicationIsActive;
if(profile == "auto") {
profiles = ["enhanced","standardExtended","standard","standardConstrained","baselineExtended","baseline","baselineConstrained"];
} else if(typeof(profile) == "number" && ((profile | 0) === profile)) {
profiles = [profile];
} else if(typeof(profile) == "string") {
profiles = [(Std().default).string(profile)];
} else if(((profile) instanceof Array)) {
var dynProfiles = (js_Boot().default).__cast(profile , Array);
profiles = [];
var _g = 0;
while(_g < dynProfiles.length) {
var prof = dynProfiles[_g];
++_g;
if(typeof(prof) == "number" && ((prof | 0) === prof)) {
profiles.push(prof);
} else {
profiles.push((Std().default).string(prof));
}
}
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Profile must be of type 'String' or 'Array'"));
}
var requestNextProfile = null;
var onFinished = null;
var onError = null;
var onCreated = null;
requestNextProfile = function() {
currentProfile = profiles.shift();
try {
executeFunc($bind(stage3D,stage3D.requestContext3D),[renderMode,currentProfile]);
} catch( error ) {
var error1 = ((error) instanceof (js__$Boot_HaxeError().default)) ? error.val : error;
if(((error1) instanceof (openfl_errors_Error().default))) {
if(profiles.length != 0) {
(haxe_Timer().default).delay(requestNextProfile,1);
} else {
throw new (js__$Boot_HaxeError().default)(error1);
}
} else {
throw error;
}
}
};
onCreated = function(event) {
var context = stage3D.context3D;
if(renderMode == "auto" && profiles.length != 0 && context.driverInfo.indexOf("Software") != -1) {
onError(event);
} else {
onFinished();
}
};
onError = function(event1) {
if(profiles.length != 0) {
event1.stopImmediatePropagation();
(haxe_Timer().default).delay(requestNextProfile,1);
} else {
onFinished();
}
};
onFinished = function() {
stage3D.removeEventListener("context3DCreate",onCreated);
stage3D.removeEventListener("error",onError);
};
stage3D.addEventListener("context3DCreate",onCreated,false,100);
stage3D.addEventListener("error",onError,false,100);
requestNextProfile();
}
// Export
exports.default = RenderUtil;