3131#endif
3232
3333typedef struct {
34- float x , y , z ;
35- float r , g , b ;
36- } VertexData ;
34+ RenderDevice * renderDevice ;
35+ Framebuffer * framebuffer ;
36+ SDL_GPUGraphicsPipeline * pipeline ;
37+ SDL_GPUBuffer * vertexBuffer ;
38+ } Scene ;
3739
38- static const VertexData vertex_data [] = {
40+ typedef struct {
41+ float3 position ;
42+ float3 color ;
43+ } Vertex ;
44+
45+ static const Vertex vertexes [] = {
3946 { -0.5f , 0.5f , -0.5f , 1 , 0 , 0 }, { 0.5f , -0.5f , -0.5f , 0 , 0 , 1 }, { -0.5f , -0.5f , -0.5f , 0 , 1 , 0 },
4047 { -0.5f , 0.5f , -0.5f , 1 , 0 , 0 }, { 0.5f , 0.5f , -0.5f , 1 , 1 , 0 }, { 0.5f , -0.5f , -0.5f , 0 , 0 , 1 },
4148 { -0.5f , 0.5f , 0.5f , 1 , 1 , 1 }, { -0.5f , -0.5f , -0.5f , 0 , 1 , 0 }, { -0.5f , -0.5f , 0.5f , 0 , 1 , 1 },
@@ -50,26 +57,77 @@ static const VertexData vertex_data[] = {
5057 { -0.5f , -0.5f , -0.5f , 0 , 1 , 0 }, { 0.5f , -0.5f , -0.5f , 0 , 0 , 1 }, { 0.5f , -0.5f , 0.5f , 1 , 0 , 1 },
5158};
5259
60+ /**
61+ * @brief
62+ */
63+ static void drawScene (Scene * scene ) {
64+ static float2 angles ;
65+ static Uint64 lastTicks ;
66+
67+ Uint64 ticks = SDL_GetTicks ();
68+ float dt = (float ) (ticks - lastTicks ) / 1000.0f ;
69+ lastTicks = ticks ;
70+
71+ angles .x += dt * 30.0f ;
72+ angles .y += dt * 60.0f ;
73+
74+ while (angles .x >= 360.0f ) {
75+ angles .x -= 360.0f ;
76+ }
77+ while (angles .y >= 360.0f ) {
78+ angles .y -= 360.0f ;
79+ }
80+
81+ CommandBuffer * cmd = $ (scene -> renderDevice , acquireCommandBuffer );
82+
83+ SwapchainTexture swapchain = { 0 };
84+ $ (cmd , waitAndAcquireSwapchainTexture , & swapchain );
85+
86+ float4x4 modelView = float4x4_rotation (angles .x , float3_new (1.f , 0.f , 0.f ));
87+ modelView = float4x4_mul (float4x4_rotation (angles .y , float3_new (0.f , 1.f , 0.f )), modelView );
88+ modelView = float4x4_mul (float4x4_translation (float3_new (0.f , 0.f , -2.5f )), modelView );
89+ const float4x4 projection = float4x4_perspective (45.f , (float ) swapchain .size .w / (float ) swapchain .size .h , 0.01f , 100.f );
90+ const float4x4 modelViewProjection = float4x4_mul (projection , modelView );
91+
92+ SDL_GPUColorTargetInfo colorTarget = {
93+ .texture = swapchain .texture ,
94+ .clear_color = { 0.1f , 0.1f , 0.2f , 1.0f },
95+ .load_op = SDL_GPU_LOADOP_CLEAR ,
96+ .store_op = SDL_GPU_STOREOP_STORE ,
97+ };
98+
99+ SDL_GPUDepthStencilTargetInfo depthTarget = $ (scene -> framebuffer , depthTargetInfo , SDL_GPU_LOADOP_CLEAR , SDL_GPU_STOREOP_DONT_CARE , 1.f );
100+
101+ RenderPass * renderPass = $ (cmd , beginRenderPass , & colorTarget , 1 , & depthTarget );
102+ $ (renderPass , bindPipeline , scene -> pipeline );
103+ $ (renderPass , bindVertexBuffers , 0 , & (SDL_GPUBufferBinding ) {
104+ .buffer = scene -> vertexBuffer ,
105+ .offset = 0 ,
106+ }, 1 );
107+ $ (cmd , pushVertexUniformData , 0 , modelViewProjection .f , sizeof (modelViewProjection ));
108+ $ (renderPass , drawPrimitives , (Uint32 ) SDL_arraysize (vertexes ), 1 , 0 , 0 );
109+ release (renderPass );
110+
111+ $ (cmd , submit );
112+ release (cmd );
113+ }
114+
53115/**
54116 * @brief
55117 */
56118int main (int argc , char * * argv ) {
57119
120+ $$ (Resource , addResourcePath , EXAMPLES );
121+
58122 GPU_Assert (SDL_Init (SDL_INIT_VIDEO ), "SDL_Init" );
59123
60124 SDL_Window * window = SDL_CreateWindow ("ObjectivelyGPU Hello" , 800 , 600 , SDL_WINDOW_HIGH_PIXEL_DENSITY );
61125 GPU_Assert (window , "SDL_CreateWindow" );
62126
63- $$ (Resource , addResourcePath , EXAMPLES );
64-
65127 RenderDevice * renderDevice = $ (alloc (RenderDevice ), initWithWindow , window );
66128 GPU_Assert (renderDevice , "RenderDevice init" );
67129
68- SDL_GPUBuffer * vertexBuffer = $ (renderDevice , createBufferWithConstMem ,
69- SDL_GPU_BUFFERUSAGE_VERTEX , vertex_data , sizeof (vertex_data ));
70-
71- int w = 0 ;
72- int h = 0 ;
130+ int w , h ;
73131 SDL_GetWindowSizeInPixels (window , & w , & h );
74132
75133 Framebuffer * framebuffer = $ (alloc (Framebuffer ), initWithDevice , renderDevice ,
@@ -94,7 +152,7 @@ int main(int argc, char **argv) {
94152
95153 SDL_GPUVertexBufferDescription vertexBufferDescription = {
96154 .slot = 0 ,
97- .pitch = sizeof (VertexData ),
155+ .pitch = sizeof (Vertex ),
98156 .input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX ,
99157 .instance_step_rate = 0 ,
100158 };
@@ -104,13 +162,13 @@ int main(int argc, char **argv) {
104162 .location = 0 ,
105163 .buffer_slot = 0 ,
106164 .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3 ,
107- .offset = 0 ,
165+ .offset = offsetof( Vertex , position ) ,
108166 },
109167 {
110168 .location = 1 ,
111169 .buffer_slot = 0 ,
112170 .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3 ,
113- .offset = 12 ,
171+ .offset = offsetof( Vertex , color ) ,
114172 },
115173 };
116174
@@ -149,11 +207,9 @@ int main(int argc, char **argv) {
149207 $ (renderDevice , releaseShader , vertexShader );
150208 $ (renderDevice , releaseShader , fragmentShader );
151209
152- bool running = true;
153- float angleX = 0.0f ;
154- float angleY = 0.0f ;
155- Uint64 lastTicks = SDL_GetTicks ();
210+ SDL_GPUBuffer * vertexBuffer = $ (renderDevice , createBufferWithConstMem , SDL_GPU_BUFFERUSAGE_VERTEX , vertexes , sizeof (vertexes ));
156211
212+ bool running = true;
157213 while (running ) {
158214 SDL_Event event ;
159215 while (SDL_PollEvent (& event )) {
@@ -167,52 +223,12 @@ int main(int argc, char **argv) {
167223 }
168224 }
169225
170- Uint64 ticks = SDL_GetTicks ();
171- float dt = (float ) (ticks - lastTicks ) / 1000.0f ;
172- lastTicks = ticks ;
173-
174- angleX += dt * 30.0f ;
175- angleY += dt * 60.0f ;
176-
177- while (angleX >= 360.0f ) {
178- angleX -= 360.0f ;
179- }
180- while (angleY >= 360.0f ) {
181- angleY -= 360.0f ;
182- }
183-
184- CommandBuffer * cmd = $ (renderDevice , acquireCommandBuffer );
185-
186- SwapchainTexture swapchain = { 0 };
187- $ (cmd , waitAndAcquireSwapchainTexture , window , & swapchain );
188-
189- float4x4 modelView = float4x4_rotation (angleX , float3_new (1.f , 0.f , 0.f ));
190- modelView = float4x4_mul (float4x4_rotation (angleY , float3_new (0.f , 1.f , 0.f )), modelView );
191- modelView = float4x4_mul (float4x4_translation (float3_new (0.f , 0.f , -2.5f )), modelView );
192- const float4x4 projection = float4x4_perspective (45.f , (float ) swapchain .size .w / (float ) swapchain .size .h , 0.01f , 100.f );
193- const float4x4 modelViewProjection = float4x4_mul (projection , modelView );
194-
195- SDL_GPUColorTargetInfo colorTarget = {
196- .texture = swapchain .texture ,
197- .clear_color = { 0.1f , 0.1f , 0.2f , 1.0f },
198- .load_op = SDL_GPU_LOADOP_CLEAR ,
199- .store_op = SDL_GPU_STOREOP_STORE ,
200- };
201-
202- SDL_GPUDepthStencilTargetInfo depthTarget = $ (framebuffer , depthTargetInfo , SDL_GPU_LOADOP_CLEAR , SDL_GPU_STOREOP_DONT_CARE , 1.f );
203-
204- RenderPass * renderPass = $ (cmd , beginRenderPass , & colorTarget , 1 , & depthTarget );
205- $ (renderPass , bindPipeline , pipeline );
206- $ (renderPass , bindVertexBuffers , 0 , & (SDL_GPUBufferBinding ) {
207- .buffer = vertexBuffer ,
208- .offset = 0 ,
209- }, 1 );
210- $ (cmd , pushVertexUniformData , 0 , modelViewProjection .f , sizeof (modelViewProjection ));
211- $ (renderPass , drawPrimitives , (Uint32 ) SDL_arraysize (vertex_data ), 1 , 0 , 0 );
212- release (renderPass );
213-
214- $ (cmd , submit );
215- release (cmd );
226+ drawScene (& (Scene ) {
227+ .renderDevice = renderDevice ,
228+ .framebuffer = framebuffer ,
229+ .pipeline = pipeline ,
230+ .vertexBuffer = vertexBuffer
231+ });
216232 }
217233
218234 $ (renderDevice , waitForIdle );
0 commit comments