Skip to content

Commit

Permalink
sokol: fix usage of sokol sampler (#19527)
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon authored Oct 7, 2023
1 parent e19e17f commit 3c68e78
Show file tree
Hide file tree
Showing 24 changed files with 354 additions and 239 deletions.
1 change: 0 additions & 1 deletion .github/workflows/gg_regressions_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ jobs:
sleep 1; ./v gret -t ./gg-regression-images/vgret.v_examples.toml -v ./gg-sample_images ./gg-regression-images
- name: Upload regression to imgur
continue-on-error: true
if: steps.compare.outcome != 'success'
run: |
./imgur.sh /tmp/fail.png
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/other_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ jobs:
run: ./v -autofree -o v2 cmd/v ## NB: this does not mean it runs, but at least keeps it from regressing

- name: Shader examples can be built
continue-on-error: true
run: |
wget https://github.com/floooh/sokol-tools-bin/raw/6040b18d39649d56dbae2ae1aed59fb755b26369/bin/linux/sokol-shdc
chmod +x ./sokol-shdc
Expand Down
2 changes: 1 addition & 1 deletion examples/gg/rotating_textured_quad.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn (mut window Window) draw() {
sgl.rotate(angle, 0.0, 0.0, 1.0) // rotate around the Z axis pointing towards the camera

sgl.enable_texture()
sgl.texture(window.img.simg)
sgl.texture(window.img.simg, window.img.ssmp)
sgl.begin_quads()
sgl.c4b(255, 255, 255, 255)
sgl.v3f_t2f(200, 200, 0, 1.0, 1.0)
Expand Down
24 changes: 15 additions & 9 deletions examples/sokol/01_cubes/cube.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mut:
gg &gg.Context = unsafe { nil }
pip_3d sgl.Pipeline
texture gfx.Image
sampler gfx.Sampler
init_flag bool
frame_count int
mouse_x int = -1
Expand All @@ -39,17 +40,13 @@ mut:
* Texture functions
*
******************************************************************************/
fn create_texture(w int, h int, buf &u8) gfx.Image {
fn create_texture(w int, h int, buf &u8) (gfx.Image, gfx.Sampler) {
sz := w * h * 4
mut img_desc := gfx.ImageDesc{
width: w
height: h
num_mipmaps: 0
// min_filter: .linear
// mag_filter: .linear
// usage: .dynamic
// wrap_u: .clamp_to_edge
// wrap_v: .clamp_to_edge
label: &u8(0)
d3d11_texture: 0
}
Expand All @@ -60,7 +57,16 @@ fn create_texture(w int, h int, buf &u8) gfx.Image {
}

sg_img := gfx.make_image(&img_desc)
return sg_img

mut smp_desc := gfx.SamplerDesc{
min_filter: .linear
mag_filter: .linear
wrap_u: .clamp_to_edge
wrap_v: .clamp_to_edge
}

sg_smp := gfx.make_sampler(&smp_desc)
return sg_img, sg_smp
}

fn destroy_texture(sg_img gfx.Image) {
Expand Down Expand Up @@ -205,7 +211,7 @@ fn draw_texture_cubes(app App) {
sgl.load_pipeline(app.pip_3d)

sgl.enable_texture()
sgl.texture(app.texture)
sgl.texture(app.texture, app.sampler)

sgl.matrix_mode_projection()
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0)
Expand Down Expand Up @@ -242,7 +248,7 @@ fn cube_field(app App) {
sgl.load_pipeline(app.pip_3d)

sgl.enable_texture()
sgl.texture(app.texture)
sgl.texture(app.texture, app.sampler)

sgl.matrix_mode_projection()
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 200.0)
Expand Down Expand Up @@ -373,7 +379,7 @@ fn my_init(mut app App) {
}
}
unsafe {
app.texture = create_texture(w, h, tmp_txt)
app.texture, app.sampler = create_texture(w, h, tmp_txt)
free(tmp_txt)
}
}
Expand Down
9 changes: 6 additions & 3 deletions examples/sokol/02_cubes_glsl/cube_glsl.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ void main() {
#pragma sokol @end

#pragma sokol @fs fs
uniform sampler2D tex;

uniform texture2D tex;
uniform sampler smp;

uniform fs_params {
vec2 text_res;
float iTime;
Expand Down Expand Up @@ -65,7 +68,7 @@ vec3 background(float t, vec3 rd)
vec4 mainImage(vec2 fragCoord)
{
vec2 uv = (fragCoord-vec2(0.4,0.4))*2.0;

//vec2 uv = (-1.0 + 2.0*fc.xy / text_res.xy) * vec2(text_res.x/text_res.y, 1.0);
vec3 ro = vec3(0.0, 0.0, -3.0);
vec3 rd = normalize(vec3(uv, 1.0));
Expand All @@ -83,7 +86,7 @@ vec4 mainImage(vec2 fragCoord)

void main() {
vec4 c = color;
vec4 txt = texture(tex, uv/4.0);
vec4 txt = texture(sampler2D(tex, smp), uv/4.0);
c = txt * c;
vec4 col_ray = mainImage(uv);
float txt_mix = mod(iTime,5);
Expand Down
24 changes: 15 additions & 9 deletions examples/sokol/02_cubes_glsl/cube_glsl.v
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mut:
gg &gg.Context = unsafe { nil }
pip_3d sgl.Pipeline
texture gfx.Image
sampler gfx.Sampler
init_flag bool
frame_count int
mouse_x int = -1
Expand All @@ -55,17 +56,13 @@ mut:
* Texture functions
*
******************************************************************************/
fn create_texture(w int, h int, buf &byte) gfx.Image {
fn create_texture(w int, h int, buf &byte) (gfx.Image, gfx.Sampler) {
sz := w * h * 4
mut img_desc := gfx.ImageDesc{
width: w
height: h
num_mipmaps: 0
// min_filter: .linear
// mag_filter: .linear
// usage: .dynamic
// wrap_u: .clamp_to_edge
// wrap_v: .clamp_to_edge
label: &u8(0)
d3d11_texture: 0
}
Expand All @@ -76,7 +73,16 @@ fn create_texture(w int, h int, buf &byte) gfx.Image {
}

sg_img := gfx.make_image(&img_desc)
return sg_img

mut smp_desc := gfx.SamplerDesc{
min_filter: .linear
mag_filter: .linear
wrap_u: .clamp_to_edge
wrap_v: .clamp_to_edge
}

sg_smp := gfx.make_sampler(&smp_desc)
return sg_img, sg_smp
}

fn destroy_texture(sg_img gfx.Image) {
Expand Down Expand Up @@ -374,7 +380,7 @@ fn draw_cube_glsl(app App) {
// clear
ws := gg.window_size_real_pixels()
mut color_action := gfx.ColorAttachmentAction{
load_action: unsafe { gfx.Action(C.SG_LOADACTION_DONTCARE) } // C.SG_ACTION_CLEAR)
load_action: unsafe { gfx.LoadAction(C.SG_LOADACTION_DONTCARE) } // C.SG_ACTION_CLEAR)
clear_value: gfx.Color{
r: 1.0
g: 1.0
Expand Down Expand Up @@ -433,7 +439,7 @@ fn draw_texture_cubes(app App) {
sgl.load_pipeline(app.pip_3d)

sgl.enable_texture()
sgl.texture(app.texture)
sgl.texture(app.texture, app.sampler)

sgl.matrix_mode_projection()
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0)
Expand Down Expand Up @@ -573,7 +579,7 @@ fn my_init(mut app App) {
i += 4
}
}
app.texture = create_texture(w, h, tmp_txt)
app.texture, app.sampler = create_texture(w, h, tmp_txt)
unsafe { free(tmp_txt) }

// glsl
Expand Down
Loading

0 comments on commit 3c68e78

Please sign in to comment.