fix ps1 shader

This commit is contained in:
2025-01-09 23:20:18 -06:00
parent 4a7543fa68
commit a76b8725cd
23 changed files with 313 additions and 18 deletions

View File

@@ -651,6 +651,8 @@ function gpupresent()
filter:"nearest", filter:"nearest",
load: "clear" load: "clear"
}); });
imgui.prepend(cmds);
// now imgui // now imgui
var pass = cmds.render_pass({ var pass = cmds.render_pass({

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,28 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct type_Globals
{
float4 ditherPattern2x2[4];
};
struct main0_out
{
float4 out_var_SV_TARGET [[color(0)]];
};
struct main0_in
{
float2 in_var_TEXCOORD0 [[user(locn0)]];
};
fragment main0_out main0(main0_in in [[stage_in]], constant type_Globals& _Globals [[buffer(0)]], texture2d<float> diffuseTexture [[texture(0)]], sampler smp [[sampler(0)]], float4 gl_FragCoord [[position]])
{
main0_out out = {};
float4 _61 = fast::clamp(diffuseTexture.sample(smp, in.in_var_TEXCOORD0) + float4(0.03125 * _Globals.ditherPattern2x2[((int(gl_FragCoord.y) & 1) * 2) + (int(gl_FragCoord.x) & 1)].x), float4(0.0), float4(1.0));
out.out_var_SV_TARGET = float4(rint(_61.xyz * 31.0) * float3(0.0322580635547637939453125), rint(_61.w * 31.0) * 0.0322580635547637939453125);
return out;
}

View File

@@ -0,0 +1,50 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct type_TransformBuffer
{
float4x4 world_to_projection;
float4x4 projection_to_world;
float4x4 world_to_view;
float4x4 view_to_projection;
packed_float3 camera_pos_world;
float viewport_min_z;
packed_float3 camera_dir_world;
float viewport_max_z;
float2 viewport_size;
float2 viewport_offset;
float2 render_size;
float time;
};
struct type_model
{
float4x4 model;
float4 color;
};
struct main0_out
{
float2 out_var_TEXCOORD0 [[user(locn0)]];
float4 out_var_COLOR0 [[user(locn1)]];
float4 gl_Position [[position]];
};
struct main0_in
{
float2 in_var_pos [[attribute(0)]];
float2 in_var_uv [[attribute(1)]];
float4 in_var_color [[attribute(2)]];
};
vertex main0_out main0(main0_in in [[stage_in]], constant type_TransformBuffer& TransformBuffer [[buffer(0)]], constant type_model& model [[buffer(1)]])
{
main0_out out = {};
out.gl_Position = TransformBuffer.world_to_projection * (model.model * float4(in.in_var_pos, 0.0, 1.0));
out.out_var_TEXCOORD0 = in.in_var_uv;
out.out_var_COLOR0 = in.in_var_color * model.color;
return out;
}

View File

@@ -1,11 +1,5 @@
SamplerState pointSampler Texture2D diffuseTexture : register(t0,space2);
{ SamplerState smp : register(s0,space2);
Filter = MIN_MAG_MIP_POINT; // Nearest-neighbor
AddressU = Wrap;
AddressV = Wrap;
};
Texture2D diffuseTexture : register(t0);
struct PSInput struct PSInput
{ {
@@ -18,10 +12,10 @@ float ditherPattern2x2[4] = {
0.75, 0.25 0.75, 0.25
}; };
float4 mainPS(PSInput input) : SV_TARGET float4 main(PSInput input) : SV_TARGET
{ {
// Sample texture with nearest-neighbor // Sample texture with nearest-neighbor
float4 color = diffuseTexture.Sample(pointSampler, input.texcoord); float4 color = diffuseTexture.Sample(smp, input.texcoord);
// Optional: Affine distortion effect // Optional: Affine distortion effect
// If you want to simulate affine warping, you could do some screen-space // If you want to simulate affine warping, you could do some screen-space
@@ -40,6 +34,7 @@ float4 mainPS(PSInput input) : SV_TARGET
// To simulate PS1 color quantization (e.g. to 5 bits for R,G,B): // To simulate PS1 color quantization (e.g. to 5 bits for R,G,B):
// We'll quantize each channel. // We'll quantize each channel.
// Suppose colorBitDepth.x = 5 means 5 bits for R/G/B, thats 32 steps. // Suppose colorBitDepth.x = 5 means 5 bits for R/G/B, thats 32 steps.
float3 colorBitDepth = float3(5,5,5);
float stepsRGB = pow(2.0, colorBitDepth.x); float stepsRGB = pow(2.0, colorBitDepth.x);
float stepsA = pow(2.0, colorBitDepth.y); float stepsA = pow(2.0, colorBitDepth.y);

View File

@@ -1,3 +1,5 @@
#include "common/vertex.hlsl"
struct VSInput struct VSInput
{ {
float3 position : POSITION; float3 position : POSITION;
@@ -10,6 +12,11 @@ struct VSOutput
float2 texcoord : TEXCOORD0; float2 texcoord : TEXCOORD0;
}; };
output vertex(output i)
{
return i;
}
/*
VSOutput mainVS(VSInput input) VSOutput mainVS(VSInput input)
{ {
VSOutput output; VSOutput output;
@@ -39,3 +46,4 @@ VSOutput mainVS(VSInput input)
return output; return output;
} }
*/

View File

@@ -0,0 +1,66 @@
{
"entryPoints" : [
{
"name" : "main",
"mode" : "frag"
}
],
"types" : {
"_10" : {
"name" : "type.$Globals",
"members" : [
{
"name" : "ditherPattern2x2",
"type" : "float",
"array" : [
4
],
"array_size_is_literal" : [
true
],
"offset" : 0,
"array_stride" : 16
}
]
}
},
"inputs" : [
{
"type" : "vec2",
"name" : "in.var.TEXCOORD0",
"location" : 0
}
],
"outputs" : [
{
"type" : "vec4",
"name" : "out.var.SV_TARGET",
"location" : 0
}
],
"separate_images" : [
{
"type" : "texture2D",
"name" : "diffuseTexture",
"set" : 2,
"binding" : 0
}
],
"separate_samplers" : [
{
"type" : "sampler",
"name" : "smp",
"set" : 2,
"binding" : 0
}
],
"ubos" : [
{
"type" : "_10",
"name" : "type.$Globals",
"block_size" : 64,
"set" : 0,
"binding" : 0
}
]
}

View File

@@ -0,0 +1,145 @@
{
"entryPoints" : [
{
"name" : "main",
"mode" : "vert"
}
],
"types" : {
"_8" : {
"name" : "type.TransformBuffer",
"members" : [
{
"name" : "world_to_projection",
"type" : "mat4",
"offset" : 0,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "projection_to_world",
"type" : "mat4",
"offset" : 64,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "world_to_view",
"type" : "mat4",
"offset" : 128,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "view_to_projection",
"type" : "mat4",
"offset" : 192,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "camera_pos_world",
"type" : "vec3",
"offset" : 256
},
{
"name" : "viewport_min_z",
"type" : "float",
"offset" : 268
},
{
"name" : "camera_dir_world",
"type" : "vec3",
"offset" : 272
},
{
"name" : "viewport_max_z",
"type" : "float",
"offset" : 284
},
{
"name" : "viewport_size",
"type" : "vec2",
"offset" : 288
},
{
"name" : "viewport_offset",
"type" : "vec2",
"offset" : 296
},
{
"name" : "render_size",
"type" : "vec2",
"offset" : 304
},
{
"name" : "time",
"type" : "float",
"offset" : 312
}
]
},
"_10" : {
"name" : "type.model",
"members" : [
{
"name" : "model",
"type" : "mat4",
"offset" : 0,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "color",
"type" : "vec4",
"offset" : 64
}
]
}
},
"inputs" : [
{
"type" : "vec2",
"name" : "in.var.pos",
"location" : 0
},
{
"type" : "vec2",
"name" : "in.var.uv",
"location" : 1
},
{
"type" : "vec4",
"name" : "in.var.color",
"location" : 2
}
],
"outputs" : [
{
"type" : "vec2",
"name" : "out.var.TEXCOORD0",
"location" : 0
},
{
"type" : "vec4",
"name" : "out.var.COLOR0",
"location" : 1
}
],
"ubos" : [
{
"type" : "_8",
"name" : "type.TransformBuffer",
"block_size" : 316,
"set" : 1,
"binding" : 0
},
{
"type" : "_10",
"name" : "type.model",
"block_size" : 80,
"set" : 1,
"binding" : 1
}
]
}

View File

@@ -5,4 +5,3 @@ output vertex(output i)
{ {
return i; return i;
} }

Binary file not shown.

Binary file not shown.

View File

@@ -770,15 +770,16 @@ JSC_CCALL(imgui_newframe,
ImGui::NewFrame(); ImGui::NewFrame();
) )
JSC_CCALL(imgui_endframe, JSC_CCALL(imgui_prepend,
ImGui::Render(); ImGui::Render();
ImDrawData *draw_data = ImGui::GetDrawData(); SDL_GPUCommandBuffer *cmd = js2SDL_GPUCommandBuffer(js,argv[0]);
Imgui_ImplSDLGPU3_PrepareDrawData(ImGui::GetDrawData(),cmd);
SDL_GPUCommandBuffer *cmd = js2SDL_GPUCommandBuffer(js,argv[0]); )
Imgui_ImplSDLGPU3_PrepareDrawData(draw_data,cmd);
SDL_GPURenderPass *pass = js2SDL_GPURenderPass(js,argv[1]);
ImGui_ImplSDLGPU3_RenderDrawData(draw_data, cmd, pass); JSC_CCALL(imgui_endframe,
SDL_GPUCommandBuffer *cmd = js2SDL_GPUCommandBuffer(js,argv[0]);
SDL_GPURenderPass *pass = js2SDL_GPURenderPass(js,argv[1]);
ImGui_ImplSDLGPU3_RenderDrawData(ImGui::GetDrawData(), cmd, pass);
ImGui::UpdatePlatformWindows(); ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault(); ImGui::RenderPlatformWindowsDefault();
) )
@@ -862,6 +863,7 @@ static const JSCFunctionListEntry js_imgui_funcs[] = {
MIST_FUNC_DEF(imgui, plotmousepos, 0), MIST_FUNC_DEF(imgui, plotmousepos, 0),
MIST_FUNC_DEF(imgui, plothovered, 0), MIST_FUNC_DEF(imgui, plothovered, 0),
MIST_FUNC_DEF(imgui, axeslimits, 4), MIST_FUNC_DEF(imgui, axeslimits, 4),
MIST_FUNC_DEF(imgui, prepend, 1),
MIST_FUNC_DEF(imgui, fitaxis, 1), MIST_FUNC_DEF(imgui, fitaxis, 1),
MIST_FUNC_DEF(imgui, columns, 1), MIST_FUNC_DEF(imgui, columns, 1),
MIST_FUNC_DEF(imgui, nextcolumn, 0), MIST_FUNC_DEF(imgui, nextcolumn, 0),