1 module swapchain;
2 
3 import example;
4 
5 import gfx.graal;
6 import gfx.window;
7 
8 import std.typecons;
9 
10 
11 class SwapchainExample : Example
12 {
13     this(string[] args) {
14         super("Swapchain", args);
15     }
16 
17     class SwapchainFrameData : FrameData
18     {
19         PrimaryCommandBuffer cmdBuf;
20 
21         this(ImageBase swcColor, CommandBuffer tempBuf)
22         {
23             super(swcColor);
24             cmdBuf = cmdPool.allocatePrimary(1)[0];
25 
26             this.outer.recordImageLayoutBarrier(
27                 tempBuf, swcColor, trans(ImageLayout.undefined, ImageLayout.presentSrc)
28             );
29         }
30 
31         override void dispose()
32         {
33             cmdPool.free([ cast(CommandBuffer)cmdBuf ]);
34             super.dispose();
35         }
36     }
37 
38     override FrameData makeFrameData(ImageBase swcColor, CommandBuffer tempBuf)
39     {
40         return new SwapchainFrameData(swcColor, tempBuf);
41     }
42 
43     override Submission[] recordCmds(FrameData frameData)
44     {
45         auto sfd = cast(SwapchainFrameData)frameData;
46         const clearValues = ClearColorValues(0.6f, 0.6f, 0.6f, hasAlpha ? 0.5f : 1f);
47         auto subrange = ImageSubresourceRange(ImageAspect.color, 0, 1, 0, 1);
48 
49         auto buf = sfd.cmdBuf;
50         buf.begin(CommandBufferUsage.none);
51 
52             buf.pipelineBarrier(
53                 trans(PipelineStage.transfer, PipelineStage.transfer), [],
54                 [ ImageMemoryBarrier(
55                     trans(Access.memoryRead, Access.transferWrite),
56                     trans(ImageLayout.presentSrc, ImageLayout.transferDstOptimal),
57                     trans(presentQueue.index, graphicsQueue.index),
58                     sfd.swcColor, subrange
59                 ) ]
60             );
61 
62             buf.clearColorImage(
63                 sfd.swcColor, ImageLayout.transferDstOptimal, clearValues, [ subrange ]
64             );
65 
66             buf.pipelineBarrier(
67                 trans(PipelineStage.transfer, PipelineStage.transfer), [],
68                 [ ImageMemoryBarrier(
69                     trans(Access.transferWrite, Access.memoryRead),
70                     trans(ImageLayout.transferDstOptimal, ImageLayout.presentSrc),
71                     trans(graphicsQueue.index, presentQueue.index),
72                     sfd.swcColor, subrange
73                 ) ]
74             );
75 
76         buf.end();
77 
78         return simpleSubmission([ buf ]);
79     }
80 }
81 
82 int main(string[] args)
83 {
84     try {
85         auto example = new SwapchainExample(args);
86         example.prepare();
87         scope(exit) example.dispose();
88 
89         example.window.onKeyOn = (KeyEvent ev)
90         {
91             if (ev.sym == KeySym.escape) {
92                 example.window.closeFlag = true;
93             }
94         };
95 
96         while (!example.window.closeFlag) {
97             example.render();
98             example.frameTick();
99             example.display.pollAndDispatch();
100         }
101 
102         return 0;
103     }
104     catch(Exception ex) {
105         gfxExLog.errorf("error occured: %s", ex.msg);
106         return 1;
107     }
108 }