Skip to content

Commit 64360d2

Browse files
authored
sokol: fix v run examples/2048/ on macos (regressed after 5d6de17) (#26705)
1 parent 602a493 commit 64360d2

2 files changed

Lines changed: 116 additions & 22 deletions

File tree

vlib/sokol/sfons/sfons.v

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,52 @@ void main() {
7373
}
7474
'
7575

76+
// Metal vertex shader source.
77+
const vs_source_metal = '#include <metal_stdlib>
78+
using namespace metal;
79+
80+
struct VSIn {
81+
float4 position [[attribute(0)]];
82+
float2 texcoord0 [[attribute(1)]];
83+
float4 color0 [[attribute(2)]];
84+
float psize [[attribute(3)]];
85+
};
86+
87+
struct VSOut {
88+
float4 position [[position]];
89+
float2 uv;
90+
float4 color;
91+
float psize [[point_size]];
92+
};
93+
94+
vertex VSOut main0(VSIn in [[stage_in]], constant float4* vs_params [[buffer(0)]]) {
95+
VSOut out;
96+
float4x4 mvp = float4x4(vs_params[0], vs_params[1], vs_params[2], vs_params[3]);
97+
float4x4 tm = float4x4(vs_params[4], vs_params[5], vs_params[6], vs_params[7]);
98+
out.position = mvp * in.position;
99+
out.psize = in.psize;
100+
out.uv = (tm * float4(in.texcoord0, 0.0, 1.0)).xy;
101+
out.color = in.color0;
102+
return out;
103+
}
104+
'
105+
106+
// Metal fragment shader source.
107+
const fs_source_metal = '#include <metal_stdlib>
108+
using namespace metal;
109+
110+
struct FSIn {
111+
float2 uv;
112+
float4 color;
113+
float psize [[point_size]];
114+
};
115+
116+
fragment float4 main0(FSIn in [[stage_in]], texture2d<float> tex_smp [[texture(0)]],
117+
sampler smp [[sampler(0)]]) {
118+
return float4(1.0, 1.0, 1.0, tex_smp.sample(smp, in.uv).r) * in.color;
119+
}
120+
'
121+
76122
// Internal state for sokol-fontstash
77123
struct SFons {
78124
mut:
@@ -120,27 +166,28 @@ fn make_shader_desc() gfx.ShaderDesc {
120166
backend := gfx.query_backend()
121167
match backend {
122168
.glcore33 {
123-
desc.vs.source = vs_source_glsl410.str
124-
desc.fs.source = fs_source_glsl410.str
169+
desc.vs.source = &char(vs_source_glsl410.str)
170+
desc.fs.source = &char(fs_source_glsl410.str)
125171
}
126172
.gles3 {
127-
desc.vs.source = vs_source_glsl300es.str
128-
desc.fs.source = fs_source_glsl300es.str
173+
desc.vs.source = &char(vs_source_glsl300es.str)
174+
desc.fs.source = &char(fs_source_glsl300es.str)
129175
}
130176
.metal_macos, .metal_ios, .metal_simulator {
131-
// TODO: embed Metal shader bytecode
132-
desc.vs.source = vs_source_glsl410.str
133-
desc.fs.source = fs_source_glsl410.str
177+
desc.vs.entry = c'main0'
178+
desc.fs.entry = c'main0'
179+
desc.vs.source = &char(vs_source_metal.str)
180+
desc.fs.source = &char(fs_source_metal.str)
134181
}
135182
.d3d11 {
136183
// TODO: embed HLSL shader bytecode
137-
desc.vs.source = vs_source_glsl410.str
138-
desc.fs.source = fs_source_glsl410.str
184+
desc.vs.source = &char(vs_source_glsl410.str)
185+
desc.fs.source = &char(fs_source_glsl410.str)
139186
}
140187
.wgpu {
141188
// TODO: embed WGSL shader source
142-
desc.vs.source = vs_source_glsl410.str
143-
desc.fs.source = fs_source_glsl410.str
189+
desc.vs.source = &char(vs_source_glsl410.str)
190+
desc.fs.source = &char(fs_source_glsl410.str)
144191
}
145192
.dummy {
146193
desc.vs.source = c''

vlib/sokol/sgl/shaders.v

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,52 @@ void main() {
6868
}
6969
'
7070

71+
// Metal vertex shader source.
72+
const vs_source_metal = '#include <metal_stdlib>
73+
using namespace metal;
74+
75+
struct VSIn {
76+
float4 position [[attribute(0)]];
77+
float2 texcoord0 [[attribute(1)]];
78+
float4 color0 [[attribute(2)]];
79+
float psize [[attribute(3)]];
80+
};
81+
82+
struct VSOut {
83+
float4 position [[position]];
84+
float2 uv;
85+
float4 color;
86+
float psize [[point_size]];
87+
};
88+
89+
vertex VSOut main0(VSIn in [[stage_in]], constant float4* vs_params [[buffer(0)]]) {
90+
VSOut out;
91+
float4x4 mvp = float4x4(vs_params[0], vs_params[1], vs_params[2], vs_params[3]);
92+
float4x4 tm = float4x4(vs_params[4], vs_params[5], vs_params[6], vs_params[7]);
93+
out.position = mvp * in.position;
94+
out.psize = in.psize;
95+
out.uv = (tm * float4(in.texcoord0, 0.0, 1.0)).xy;
96+
out.color = in.color0;
97+
return out;
98+
}
99+
'
100+
101+
// Metal fragment shader source.
102+
const fs_source_metal = '#include <metal_stdlib>
103+
using namespace metal;
104+
105+
struct FSIn {
106+
float2 uv;
107+
float4 color;
108+
float psize [[point_size]];
109+
};
110+
111+
fragment float4 main0(FSIn in [[stage_in]], texture2d<float> tex_smp [[texture(0)]],
112+
sampler smp [[sampler(0)]]) {
113+
return tex_smp.sample(smp, in.uv) * in.color;
114+
}
115+
'
116+
71117
// Builds and returns the shader descriptor for the sgl shader.
72118
// Selects the correct shader source/bytecode based on the active graphics backend.
73119
fn make_shader_desc() gfx.ShaderDesc {
@@ -102,27 +148,28 @@ fn make_shader_desc() gfx.ShaderDesc {
102148
backend := gfx.query_backend()
103149
match backend {
104150
.glcore33 {
105-
desc.vs.source = vs_source_glsl410.str
106-
desc.fs.source = fs_source_glsl410.str
151+
desc.vs.source = &char(vs_source_glsl410.str)
152+
desc.fs.source = &char(fs_source_glsl410.str)
107153
}
108154
.gles3 {
109-
desc.vs.source = vs_source_glsl300es.str
110-
desc.fs.source = fs_source_glsl300es.str
155+
desc.vs.source = &char(vs_source_glsl300es.str)
156+
desc.fs.source = &char(fs_source_glsl300es.str)
111157
}
112158
.metal_macos, .metal_ios, .metal_simulator {
113-
// Metal backends - TODO: embed Metal shader bytecode
114-
desc.vs.source = vs_source_glsl410.str
115-
desc.fs.source = fs_source_glsl410.str
159+
desc.vs.entry = c'main0'
160+
desc.fs.entry = c'main0'
161+
desc.vs.source = &char(vs_source_metal.str)
162+
desc.fs.source = &char(fs_source_metal.str)
116163
}
117164
.d3d11 {
118165
// D3D11 backend - TODO: embed HLSL shader bytecode
119-
desc.vs.source = vs_source_glsl410.str
120-
desc.fs.source = fs_source_glsl410.str
166+
desc.vs.source = &char(vs_source_glsl410.str)
167+
desc.fs.source = &char(fs_source_glsl410.str)
121168
}
122169
.wgpu {
123170
// WebGPU - TODO: embed WGSL shader source
124-
desc.vs.source = vs_source_glsl410.str
125-
desc.fs.source = fs_source_glsl410.str
171+
desc.vs.source = &char(vs_source_glsl410.str)
172+
desc.fs.source = &char(fs_source_glsl410.str)
126173
}
127174
.dummy {
128175
desc.vs.source = c''

0 commit comments

Comments
 (0)