1818#define SAFE_RELEASE (p ) if (p!=nullptr ) {p->release (); p = nullptr ;}
1919#define UNUSED_VARIABLE (a ) (void )(a)
2020#define LAST_CLIP_INDEX ((uint8_t ) r->commands.cliprects_buffer.GetNumElements()-1 )
21+ #define assert_msg (expr, msg ) assert ((expr) && (msg))
2122
2223// ---------------------------------------------------------------------------------------------------------------------------
2324// Constants
@@ -381,7 +382,7 @@ void od_init_screenshot_resources(struct onedraw* r)
381382// ----------------------------------------------------------------------------------------------------------------------------
382383void od_create_atlas (struct onedraw * r, uint32_t width, uint32_t height, uint32_t slice_count)
383384{
384- assert (slice_count < UINT8_MAX);
385+ assert_msg (slice_count < UINT8_MAX, " too many slices " );
385386 MTL::TextureDescriptor* desc = MTL::TextureDescriptor::alloc ()->init ();
386387 desc->setTextureType (MTL::TextureType2DArray);
387388 desc->setPixelFormat (MTL::PixelFormat::PixelFormatRGBA8Unorm_sRGB);
@@ -733,8 +734,8 @@ void od_bin_commands(struct onedraw* r)
733734// ----------------------------------------------------------------------------------------------------------------------------
734735void od_flush (struct onedraw * r, void * drawable)
735736{
736- assert ((uint16_t )((CA::MetalDrawable*)drawable)->texture ()->width () == r->rasterizer .width );
737- assert ((uint16_t )((CA::MetalDrawable*)drawable)->texture ()->height () == r->rasterizer .height );
737+ assert_msg ((uint16_t )((CA::MetalDrawable*)drawable)->texture ()->width () == r->rasterizer .width , " drawable/renderer size mismatch " );
738+ assert_msg ((uint16_t )((CA::MetalDrawable*)drawable)->texture ()->height () == r->rasterizer .height , " drawable/renderer size mismatch " );
738739
739740 r->command_buffer = r->command_queue ->commandBuffer ();
740741
@@ -824,10 +825,10 @@ size_t od_min_memory_size()
824825// ----------------------------------------------------------------------------------------------------------------------------
825826struct onedraw * od_init (onedraw_def* def)
826827{
827- assert (def->preallocated_buffer != nullptr );
828- assert (((uintptr_t )def->preallocated_buffer )%sizeof (uintptr_t ) == 0 );
828+ assert_msg (def->preallocated_buffer != nullptr , " forgot to allocate memory? " );
829+ assert_msg (((uintptr_t )def->preallocated_buffer )%sizeof (uintptr_t ) == 0 , " preallocated_buffer must be aligned on sizeof(uintptr_t) " );
829830 assert (def->metal_device != nullptr );
830- assert (((MTL::Device*)def->metal_device )->supportsFamily (MTL::GPUFamilyApple7));
831+ assert_msg (((MTL::Device*)def->metal_device )->supportsFamily (MTL::GPUFamilyApple7), " onedraw supports only M1/A14 GPU and later " );
831832
832833 onedraw* r = new (def->preallocated_buffer ) onedraw;
833834
@@ -882,7 +883,7 @@ struct onedraw* od_init(onedraw_def* def)
882883// ----------------------------------------------------------------------------------------------------------------------------
883884void od_upload_slice (struct onedraw * r, const void * pixel_data, uint32_t slice_index)
884885{
885- assert (slice_index<r->rasterizer .atlas ->arrayLength ());
886+ assert_msg (slice_index<r->rasterizer .atlas ->arrayLength (), " slice_index is out of bound " );
886887
887888 const NS::UInteger bpp = 4 ; // MTL::PixelFormat::PixelFormatRGBA8Unorm_sRGB
888889 const NS::UInteger bytes_per_row = r->rasterizer .atlas ->width () * bpp;
@@ -902,7 +903,8 @@ void od_upload_slice(struct onedraw* r, const void* pixel_data, uint32_t slice_i
902903// ----------------------------------------------------------------------------------------------------------------------------
903904void od_capture_region (struct onedraw * r, uint32_t x, uint32_t y, uint32_t width, uint32_t height)
904905{
905- assert (x<=r->rasterizer .width && width<=r->rasterizer .width && y<=r->rasterizer .height && height<=r->rasterizer .height );
906+ assert_msg (x<=r->rasterizer .width && width<=r->rasterizer .width && y<=r->rasterizer .height && height<=r->rasterizer .height ,
907+ " capture region cannot be bigger than the rendertarget" );
906908 r->screenshot .region_x = x;
907909 r->screenshot .region_y = y;
908910 r->screenshot .region_width = width;
@@ -951,22 +953,20 @@ void od_resize(struct onedraw* r, uint32_t width, uint32_t height)
951953// ----------------------------------------------------------------------------------------------------------------------------
952954void od_begin_frame (struct onedraw * r)
953955{
954- assert (r->commands .group_aabb == nullptr );
956+ assert_msg (r->commands .group_aabb == nullptr , " previous frame was not ended properly with od_end_frame " );
955957 r->stats .frame_index ++;
956958 r->commands .buffer .Map (r->stats .frame_index );
957959 r->commands .colors .Map (r->stats .frame_index );
958960 r->commands .draw_aabb = r->commands .aabb_buffer .Map (r->stats .frame_index );
959961 r->commands .data_buffer .Map (r->stats .frame_index );
960962 r->commands .cliprects_buffer .Map (r->stats .frame_index );
961963 od_set_cliprect (r, 0 , 0 , (uint16_t ) r->rasterizer .width , (uint16_t ) r->rasterizer .height );
962- r->commands .group_aabb = nullptr ;
963964}
964965
965966// ----------------------------------------------------------------------------------------------------------------------------
966967void od_end_frame (struct onedraw * r, void * drawable)
967968{
968- assert (r->commands .group_aabb == nullptr );
969-
969+ assert_msg (r->commands .group_aabb == nullptr , " you need to call od_end_group, before od_end_frame" );
970970 if (r->screenshot .show_region )
971971 {
972972 aabb capture_region = {.min = {(float )r->screenshot .region_x , (float )r->screenshot .region_y }};
@@ -1039,8 +1039,8 @@ void od_get_stats(struct onedraw* r, od_stats* stats)
10391039// ----------------------------------------------------------------------------------------------------------------------------
10401040void od_begin_group (struct onedraw * r, bool smoothblend, float group_smoothness, float outline_width)
10411041{
1042- assert (r->commands .group_aabb == nullptr );
1043- assert (group_smoothness >= 0 .f );
1042+ assert_msg (r->commands .group_aabb == nullptr , " cannot call a second time od_begin_group without closing the previous group " );
1043+ assert_msg (group_smoothness >= 0 .f , " smoothness cannot be negative " );
10441044
10451045 draw_command* cmd = r->commands .buffer .NewElement ();
10461046 draw_color* color = r->commands .colors .NewElement ();
@@ -1078,7 +1078,7 @@ void od_begin_group(struct onedraw* r, bool smoothblend, float group_smoothness,
10781078// ----------------------------------------------------------------------------------------------------------------------------
10791079void od_end_group (struct onedraw * r, draw_color outline_color)
10801080{
1081- assert (r->commands .group_aabb != nullptr );
1081+ assert_msg (r->commands .group_aabb != nullptr , " you have to call od_begin_group() before closing it " );
10821082
10831083 draw_command* cmd = r->commands .buffer .NewElement ();
10841084 draw_color* color = r->commands .colors .NewElement ();
@@ -1562,7 +1562,7 @@ void od_draw_text(struct onedraw* r, float x, float y, const char* text, draw_co
15621562// ----------------------------------------------------------------------------------------------------------------------------
15631563void od_draw_quad (struct onedraw * r, float x0, float y0, float x1, float y1, od_quad_uv uv, uint32_t slice_index, draw_color srgb_color)
15641564{
1565- assert (slice_index < r->rasterizer .atlas ->arrayLength ());
1565+ assert_msg (slice_index < r->rasterizer .atlas ->arrayLength (), " slice index out of bound " );
15661566
15671567 if (fabsf (x0 - x1) < HALF_PIXEL || fabsf (y0 - y1) < HALF_PIXEL)
15681568 return ;
@@ -1597,7 +1597,7 @@ void od_draw_quad(struct onedraw* r, float x0, float y0, float x1, float y1, od_
15971597// ----------------------------------------------------------------------------------------------------------------------------
15981598void od_draw_oriented_quad (struct onedraw * r, float cx, float cy, float width, float height, float angle, od_quad_uv uv, uint32_t slice_index, draw_color srgb_color)
15991599{
1600- assert (slice_index < r->rasterizer .atlas ->arrayLength ());
1600+ assert_msg (slice_index < r->rasterizer .atlas ->arrayLength (), " slice index out of bound " );
16011601
16021602 if (width < HALF_PIXEL || height < HALF_PIXEL)
16031603 return ;
0 commit comments