aboutsummaryrefslogtreecommitdiff
path: root/src/draw/framebuffer.odin
blob: cd7848bf3acc75cc0a8d84d377a6cf6008173aee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package draw

import sg "shared:sokol/gfx"

Framebuffer :: struct {
  img: sg.Image,
  view: sg.View,
  pipe: sg.Pipeline,
  pass: sg.Pass,
}

init_framebuffer :: proc(
  fb: ^Framebuffer,
  width: i32,
  height: i32,
  clear_color := Color{0, 0, 0, 0},
) {
  fb.pipe = sg.make_pipeline({
    shader = sg.make_shader(default_shader_desc(sg.query_backend())),
    layout = {
      attrs = {
        ATTR_default_vposition = {format = .FLOAT2},
        ATTR_default_vuv = {format = .FLOAT2},
        ATTR_default_vcolor = {format = .FLOAT4},
      },
    },
    depth = {
      pixel_format = .NONE,
    },
    sample_count = 1,
    colors = {
      0 = {
        pixel_format = .RGBA8,
        blend = {
          enabled = true,
          src_factor_rgb = .SRC_ALPHA,
          dst_factor_rgb = .ONE_MINUS_SRC_ALPHA,
          src_factor_alpha = .ONE,
          dst_factor_alpha = .ZERO,
        },
      },
    },
  })

  fb.img = sg.make_image({
    usage = {
      color_attachment = true,
    },

    width = width,
    height = height,

    pixel_format = .RGBA8,
    sample_count = 1,
  })

  fb.view = sg.make_view({
    texture = {
      image = fb.img,
    },
  })

  fb.pass = {
    attachments = {
      colors = {
        0 = sg.make_view({
          color_attachment = {
            image = fb.img,
          },
        }),
      },
    },
    action = {
      colors = {
        0 = {load_action = .CLEAR, clear_value = transmute(sg.Color)clear_color},
      },
    },
  }
}

destroy_framebuffer :: proc(fb: ^Framebuffer) {
  sg.destroy_pipeline(fb.pipe)
  sg.destroy_view(fb.view)
  sg.destroy_image(fb.img)
}

framebuffer_draw_start :: proc(fb: Framebuffer) {
  sg.begin_pass(fb.pass)
  sg.apply_pipeline(fb.pipe)
}

framebuffer_draw_end :: proc(fb: Framebuffer) {
  sg.end_pass()
}