mirror of
https://github.com/par274/sharpemu.git
synced 2026-08-01 15:39:47 +08:00
[video] added sdl window and host display plumbing
This commit is contained in:
@@ -92,7 +92,7 @@ public static class SpirvFixedShaders
|
||||
return module.Build();
|
||||
}
|
||||
|
||||
public static byte[] CreateCopyFragment()
|
||||
public static byte[] CreateCopyFragment(float colorScale = 1f)
|
||||
{
|
||||
var module = new SpirvModuleBuilder();
|
||||
module.AddCapability(SpirvCapability.Shader);
|
||||
@@ -152,6 +152,16 @@ public static class SpirvFixedShaders
|
||||
coordinates,
|
||||
2,
|
||||
lod);
|
||||
if (colorScale != 1f)
|
||||
{
|
||||
var scale = module.ConstantComposite(
|
||||
vec4Type,
|
||||
module.ConstantFloat(floatType, colorScale),
|
||||
module.ConstantFloat(floatType, colorScale),
|
||||
module.ConstantFloat(floatType, colorScale),
|
||||
module.ConstantFloat(floatType, 1f));
|
||||
color = module.AddInstruction(SpirvOp.FMul, vec4Type, color, scale);
|
||||
}
|
||||
module.AddStatement(SpirvOp.Store, output, color);
|
||||
module.AddStatement(SpirvOp.Return);
|
||||
module.EndFunction();
|
||||
@@ -165,6 +175,155 @@ public static class SpirvFixedShaders
|
||||
return module.Build();
|
||||
}
|
||||
|
||||
public static byte[] CreatePqToScRgbFragment()
|
||||
{
|
||||
const float inverseM1 = 16384f / 2610f;
|
||||
const float inverseM2 = 32f / 2523f;
|
||||
const float c1 = 3424f / 4096f;
|
||||
const float c2 = 2413f / 128f;
|
||||
const float c3 = 2392f / 128f;
|
||||
|
||||
var module = new SpirvModuleBuilder();
|
||||
module.AddCapability(SpirvCapability.Shader);
|
||||
var glsl = module.ImportExtInst("GLSL.std.450");
|
||||
var voidType = module.TypeVoid();
|
||||
var floatType = module.TypeFloat(32);
|
||||
var vec2Type = module.TypeVector(floatType, 2);
|
||||
var vec3Type = module.TypeVector(floatType, 3);
|
||||
var vec4Type = module.TypeVector(floatType, 4);
|
||||
var inputVec4Pointer = module.TypePointer(SpirvStorageClass.Input, vec4Type);
|
||||
var outputVec4Pointer = module.TypePointer(SpirvStorageClass.Output, vec4Type);
|
||||
var imageType = module.TypeImage(
|
||||
floatType,
|
||||
SpirvImageDim.Dim2D,
|
||||
depth: false,
|
||||
arrayed: false,
|
||||
multisampled: false,
|
||||
sampled: 1,
|
||||
SpirvImageFormat.Unknown);
|
||||
var sampledImageType = module.TypeSampledImage(imageType);
|
||||
var sampledImagePointer =
|
||||
module.TypePointer(SpirvStorageClass.UniformConstant, sampledImageType);
|
||||
var attribute = module.AddGlobalVariable(inputVec4Pointer, SpirvStorageClass.Input);
|
||||
module.AddDecoration(attribute, SpirvDecoration.Location, 0);
|
||||
var texture = module.AddGlobalVariable(
|
||||
sampledImagePointer,
|
||||
SpirvStorageClass.UniformConstant);
|
||||
module.AddDecoration(texture, SpirvDecoration.DescriptorSet, 0);
|
||||
module.AddDecoration(texture, SpirvDecoration.Binding, 1);
|
||||
var output = module.AddGlobalVariable(outputVec4Pointer, SpirvStorageClass.Output);
|
||||
module.AddDecoration(output, SpirvDecoration.Location, 0);
|
||||
|
||||
uint Float(float value) => module.ConstantFloat(floatType, value);
|
||||
uint Vec3(float value) => module.ConstantComposite(
|
||||
vec3Type,
|
||||
Float(value),
|
||||
Float(value),
|
||||
Float(value));
|
||||
uint Ext(uint operation, uint resultType, params uint[] operands)
|
||||
{
|
||||
var values = new uint[2 + operands.Length];
|
||||
values[0] = glsl;
|
||||
values[1] = operation;
|
||||
operands.CopyTo(values, 2);
|
||||
return module.AddInstruction(SpirvOp.ExtInst, resultType, values);
|
||||
}
|
||||
uint Component(uint vector, uint index) =>
|
||||
module.AddInstruction(SpirvOp.CompositeExtract, floatType, vector, index);
|
||||
uint Multiply(uint left, float right) =>
|
||||
module.AddInstruction(SpirvOp.FMul, floatType, left, Float(right));
|
||||
uint Add3(uint first, uint second, uint third) =>
|
||||
module.AddInstruction(
|
||||
SpirvOp.FAdd,
|
||||
floatType,
|
||||
module.AddInstruction(SpirvOp.FAdd, floatType, first, second),
|
||||
third);
|
||||
|
||||
var functionType = module.TypeFunction(voidType);
|
||||
var main = module.BeginFunction(voidType, functionType);
|
||||
module.AddLabel();
|
||||
var attributeValue = module.AddInstruction(SpirvOp.Load, vec4Type, attribute);
|
||||
var coordinates = module.AddInstruction(
|
||||
SpirvOp.VectorShuffle,
|
||||
vec2Type,
|
||||
attributeValue,
|
||||
attributeValue,
|
||||
0,
|
||||
1);
|
||||
var sampledImage = module.AddInstruction(SpirvOp.Load, sampledImageType, texture);
|
||||
var color = module.AddInstruction(
|
||||
SpirvOp.ImageSampleExplicitLod,
|
||||
vec4Type,
|
||||
sampledImage,
|
||||
coordinates,
|
||||
2,
|
||||
Float(0));
|
||||
var pq = module.AddInstruction(
|
||||
SpirvOp.VectorShuffle,
|
||||
vec3Type,
|
||||
color,
|
||||
color,
|
||||
0,
|
||||
1,
|
||||
2);
|
||||
|
||||
// SMPTE ST 2084 converts normalized PQ code values to absolute luminance.
|
||||
var powered = Ext(26, vec3Type, pq, Vec3(inverseM2));
|
||||
var numerator = Ext(
|
||||
40,
|
||||
vec3Type,
|
||||
module.AddInstruction(SpirvOp.FSub, vec3Type, powered, Vec3(c1)),
|
||||
Vec3(0));
|
||||
var denominator = module.AddInstruction(
|
||||
SpirvOp.FSub,
|
||||
vec3Type,
|
||||
Vec3(c2),
|
||||
module.AddInstruction(SpirvOp.FMul, vec3Type, Vec3(c3), powered));
|
||||
var normalizedLuminance = Ext(
|
||||
26,
|
||||
vec3Type,
|
||||
module.AddInstruction(SpirvOp.FDiv, vec3Type, numerator, denominator),
|
||||
Vec3(inverseM1));
|
||||
var scRgb2020 = module.AddInstruction(
|
||||
SpirvOp.FMul,
|
||||
vec3Type,
|
||||
normalizedLuminance,
|
||||
Vec3(10000f / 80f));
|
||||
|
||||
var red2020 = Component(scRgb2020, 0);
|
||||
var green2020 = Component(scRgb2020, 1);
|
||||
var blue2020 = Component(scRgb2020, 2);
|
||||
var red = Add3(
|
||||
Multiply(red2020, 1.660491f),
|
||||
Multiply(green2020, -0.587641f),
|
||||
Multiply(blue2020, -0.072850f));
|
||||
var green = Add3(
|
||||
Multiply(red2020, -0.124550f),
|
||||
Multiply(green2020, 1.132900f),
|
||||
Multiply(blue2020, -0.008349f));
|
||||
var blue = Add3(
|
||||
Multiply(red2020, -0.018151f),
|
||||
Multiply(green2020, -0.100579f),
|
||||
Multiply(blue2020, 1.118730f));
|
||||
var converted = module.AddInstruction(
|
||||
SpirvOp.CompositeConstruct,
|
||||
vec4Type,
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
Component(color, 3));
|
||||
module.AddStatement(SpirvOp.Store, output, converted);
|
||||
module.AddStatement(SpirvOp.Return);
|
||||
module.EndFunction();
|
||||
module.AddEntryPoint(
|
||||
SpirvExecutionModel.Fragment,
|
||||
main,
|
||||
"main",
|
||||
[attribute, texture, output]);
|
||||
module.AddExecutionMode(main, SpirvExecutionMode.OriginUpperLeft);
|
||||
return module.Build();
|
||||
}
|
||||
|
||||
public static byte[] CreateSolidFragment(float red, float green, float blue, float alpha)
|
||||
{
|
||||
var module = new SpirvModuleBuilder();
|
||||
|
||||
Reference in New Issue
Block a user