2121 * 3. This notice may not be removed or altered from any source distribution.
2222 */
2323
24+ #include <assert.h>
2425#include <stdlib.h>
2526
2627#include <SDL3/SDL.h>
2728
2829#include <Objectively.h>
2930#include <ObjectivelyGPU.h>
3031
31- #ifndef SHADERS
32- # define SHADERS "."
32+ #ifndef EXAMPLES
33+ # define EXAMPLES "."
3334#endif
3435
3536typedef struct {
@@ -52,29 +53,9 @@ static const VertexData vertex_data[] = {
5253 { -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 },
5354};
5455
55- static void log_sdl_error (const char * what ) {
56- SDL_LogCritical (SDL_LOG_CATEGORY_APPLICATION , "%s: %s" , what , SDL_GetError ());
57- }
58-
59- static SDL_GPUTexture * create_depth_texture (const RenderDevice * renderDevice , SDL_Size size ) {
60- if (size .w <= 0 || size .h <= 0 ) {
61- return NULL ;
62- }
63-
64- return $ (renderDevice , createTexture , & (SDL_GPUTextureCreateInfo ) {
65- .type = SDL_GPU_TEXTURETYPE_2D ,
66- .format = SDL_GPU_TEXTUREFORMAT_D16_UNORM ,
67- .usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET ,
68- .width = (Uint32 ) size .w ,
69- .height = (Uint32 ) size .h ,
70- .layer_count_or_depth = 1 ,
71- .num_levels = 1 ,
72- .sample_count = SDL_GPU_SAMPLECOUNT_1 ,
73- }, NULL );
74- }
75-
7656static void upload_vertex_buffer (const RenderDevice * renderDevice , SDL_GPUBuffer * buffer ) {
77- SDL_GPUTransferBuffer * transferBuffer = $ (renderDevice , createTransferBuffer , & (SDL_GPUTransferBufferCreateInfo ) {
57+
58+ SDL_GPUTransferBuffer * transferBuffer = $ (renderDevice , createTransferBuffer , & (SDL_GPUTransferBufferCreateInfo ) {
7859 .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD ,
7960 .size = sizeof (vertex_data ),
8061 });
@@ -105,69 +86,66 @@ static void upload_vertex_buffer(const RenderDevice *renderDevice, SDL_GPUBuffer
10586 $ (renderDevice , releaseTransferBuffer , transferBuffer );
10687}
10788
108-
89+ /**
90+ * @brief
91+ */
10992int main (int argc , char * * argv ) {
11093
11194 int status = EXIT_FAILURE ;
112- SDL_Window * window = NULL ;
113- RenderDevice * renderDevice = NULL ;
114- SDL_GPUBuffer * vertexBuffer = NULL ;
115- SDL_GPUTexture * depthTexture = NULL ;
116- SDL_GPUShader * vertexShader = NULL ;
117- SDL_GPUShader * fragmentShader = NULL ;
118- SDL_GPUGraphicsPipeline * pipeline = NULL ;
119- SDL_Size depthSize = MakeSize (0 , 0 );
120-
121- if (!SDL_Init (SDL_INIT_VIDEO )) {
122- log_sdl_error ("SDL_Init" );
123- return status ;
124- }
12595
126- $$ ( Resource , addResourcePath , SHADERS );
96+ SDL_Init ( SDL_INIT_VIDEO );
12797
128- window = SDL_CreateWindow ("ObjectivelyGPU Hello" , 800 , 600 , SDL_WINDOW_HIGH_PIXEL_DENSITY );
129- if (!window ) {
130- log_sdl_error ("SDL_CreateWindow" );
131- goto cleanup ;
132- }
98+ SDL_Window * window = SDL_CreateWindow ("ObjectivelyGPU Hello" , 800 , 600 , SDL_WINDOW_HIGH_PIXEL_DENSITY );
99+ GPU_Assert (window , "Failed to create window" );
100+
101+ $$ (Resource , addResourcePath , EXAMPLES );
133102
134- renderDevice = $ (alloc (RenderDevice ), initWithWindow , window );
103+ RenderDevice * renderDevice = $ (alloc (RenderDevice ), initWithWindow , window );
104+ assert (renderDevice );
135105
136- vertexBuffer = $ (renderDevice , createBuffer , & (SDL_GPUBufferCreateInfo ) {
106+ SDL_GPUBuffer * vertexBuffer = $ (renderDevice , createBuffer , & (SDL_GPUBufferCreateInfo ) {
137107 .usage = SDL_GPU_BUFFERUSAGE_VERTEX ,
138108 .size = sizeof (vertex_data ),
139109 });
140110 upload_vertex_buffer (renderDevice , vertexBuffer );
141111
142- int drawableWidth = 0 ;
143- int drawableHeight = 0 ;
144- if (!SDL_GetWindowSizeInPixels (window , & drawableWidth , & drawableHeight )) {
145- log_sdl_error ("SDL_GetWindowSizeInPixels" );
146- goto cleanup ;
147- }
148-
149- depthSize = MakeSize (drawableWidth , drawableHeight );
150- depthTexture = create_depth_texture (renderDevice , depthSize );
151-
152- vertexShader = $ (renderDevice , loadShader , "Hello.vert" , & (SDL_GPUShaderCreateInfo ) {
112+ int w = 0 ;
113+ int h = 0 ;
114+ SDL_GetWindowSizeInPixels (window , & w , & h );
115+
116+ SDL_GPUTexture * depthTexture = $ (renderDevice , createTexture , & (SDL_GPUTextureCreateInfo ) {
117+ .type = SDL_GPU_TEXTURETYPE_2D ,
118+ .format = SDL_GPU_TEXTUREFORMAT_D16_UNORM ,
119+ .usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET ,
120+ .width = (Uint32 ) w ,
121+ .height = (Uint32 ) h ,
122+ .layer_count_or_depth = 1 ,
123+ .num_levels = 1 ,
124+ .sample_count = SDL_GPU_SAMPLECOUNT_1 ,
125+ }, NULL );
126+
127+ SDL_GPUShader * vertexShader = $ (renderDevice , loadShader , "Hello.vert" , & (SDL_GPUShaderCreateInfo ) {
153128 .entrypoint = "vs_main" ,
154129 .stage = SDL_GPU_SHADERSTAGE_VERTEX ,
155130 .num_uniform_buffers = 1 ,
156131 });
157- fragmentShader = $ (renderDevice , loadShader , "Hello.frag" , & (SDL_GPUShaderCreateInfo ) {
132+
133+ SDL_GPUShader * fragmentShader = $ (renderDevice , loadShader , "Hello.frag" , & (SDL_GPUShaderCreateInfo ) {
158134 .entrypoint = "fs_main" ,
159135 .stage = SDL_GPU_SHADERSTAGE_FRAGMENT ,
160136 });
161137
162138 SDL_GPUColorTargetDescription colorTargetDescription = {
163139 .format = $ (renderDevice , getSwapchainTextureFormat , window ),
164140 };
165- SDL_GPUVertexBufferDescription vertexBufferDescription = {
141+
142+ SDL_GPUVertexBufferDescription vertexBufferDescription = {
166143 .slot = 0 ,
167144 .pitch = sizeof (VertexData ),
168145 .input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX ,
169146 .instance_step_rate = 0 ,
170147 };
148+
171149 SDL_GPUVertexAttribute vertexAttributes [] = {
172150 {
173151 .location = 0 ,
@@ -183,7 +161,7 @@ int main(int argc, char **argv) {
183161 },
184162 };
185163
186- pipeline = $ (renderDevice , createGraphicsPipeline , & (SDL_GPUGraphicsPipelineCreateInfo ) {
164+ SDL_GPUGraphicsPipeline * pipeline = $ (renderDevice , createGraphicsPipeline , & (SDL_GPUGraphicsPipelineCreateInfo ) {
187165 .vertex_shader = vertexShader ,
188166 .fragment_shader = fragmentShader ,
189167 .vertex_input_state = {
@@ -266,12 +244,6 @@ int main(int argc, char **argv) {
266244 continue ;
267245 }
268246
269- if (depthTexture == NULL || swapchain .size .w != depthSize .w || swapchain .size .h != depthSize .h ) {
270- depthSize = swapchain .size ;
271- $ (renderDevice , releaseTexture , depthTexture );
272- depthTexture = create_depth_texture (renderDevice , depthSize );
273- }
274-
275247 float4x4 mv = float4x4_rotation (angleX , float3_new (1.f , 0.f , 0.f ));
276248 mv = float4x4_mul (float4x4_rotation (angleY , float3_new (0.f , 1.f , 0.f )), mv );
277249 mv = float4x4_mul (float4x4_translation (float3_new (0.f , 0.f , -2.5f )), mv );
0 commit comments