2025-04-10 07:36:58 +00:00
|
|
|
package kha.graphics4;
|
|
|
|
|
|
|
|
import kha.Blob;
|
|
|
|
|
|
|
|
class FragmentShader {
|
|
|
|
public var _shader: Pointer;
|
|
|
|
|
|
|
|
public function new(sources: Array<Blob>, files: Array<String>) {
|
|
|
|
//initShader(sources[0]);
|
|
|
|
var shaderBlob: Blob = null;
|
|
|
|
var shaderFile: String = null;
|
|
|
|
|
|
|
|
#if kha_opengl
|
|
|
|
final expectedExtension = ".glsl";
|
|
|
|
#elseif kha_direct3d11
|
|
|
|
final expectedExtension = ".d3d11";
|
|
|
|
#elseif kha_direct3d12
|
|
|
|
final expectedExtension = ".d3d12";
|
|
|
|
#elseif kha_metal
|
|
|
|
final expectedExtension = ".metal";
|
|
|
|
#elseif kha_vulkan
|
|
|
|
final expectedExtension = ".spirv";
|
|
|
|
#else
|
|
|
|
final expectedExtension = ".glsl";
|
|
|
|
#end
|
|
|
|
|
|
|
|
if (sources != null && files != null) {
|
|
|
|
for (i in 0...files.length) {
|
|
|
|
if (files[i].endsWith(expectedExtension)) {
|
|
|
|
shaderBlob = sources[i];
|
|
|
|
shaderFile = files[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shaderBlob == null && sources != null && sources.length > 0) {
|
|
|
|
trace('Warning: Could not find shader with extension ${expectedExtension}. Falling back to sources[0]: ${files != null && files.length > 0 ? files[0] : "Unknown"}');
|
|
|
|
shaderBlob = sources[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shaderBlob != null) {
|
|
|
|
initShader(shaderBlob);
|
|
|
|
} else {
|
|
|
|
trace('Error: No suitable fragment shader source found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function initShader(source: Blob): Void {
|
|
|
|
//_shader = kinc_create_fragmentshader(source.bytes.getData(), source.bytes.getData().length);
|
|
|
|
_shader = kinc_create_fragmentshader(source.bytes.getData(), source.length); // Use source.length here
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromSource(source: String): FragmentShader {
|
|
|
|
var sh = new FragmentShader(null, null);
|
|
|
|
sh._shader = kinc_fragmentshader_from_source(StringHelper.convert(source));
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(): Void {}
|
|
|
|
|
|
|
|
@:hlNative("std", "kinc_create_fragmentshader") static function kinc_create_fragmentshader(data: hl.Bytes, length: Int): Pointer {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@:hlNative("std", "kinc_fragmentshader_from_source") static function kinc_fragmentshader_from_source(source: hl.Bytes): Pointer {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|