test_s3d_scene_view_delayed_cache_update.c (4048B)
1 /* Copyright (C) 2015-2023, 2026 |Méso|Star> (contact@meso-star.com) 2 * 3 * This file is part of Star-3D. 4 * 5 * Star-3D is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * Star-3D is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with Star-3D. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #include "s3d.h" 19 20 #include <rsys/float3.h> 21 #include <rsys/mem_allocator.h> 22 23 /******************************************************************************* 24 * Helper functions 25 ******************************************************************************/ 26 static void 27 get_ids(const unsigned itri, unsigned ids[3], void* data) 28 { 29 (void)itri, (void)ids, (void)data; 30 ids[0] = 0; 31 ids[1] = 1; 32 ids[2] = 2; 33 } 34 35 static void 36 get_pos(const unsigned ivtx, float pos[3], void* data) 37 { 38 (void)data; 39 40 switch(ivtx) { 41 case 0: f3(pos, 0.f, 0.f, 0.f); break; 42 case 1: f3(pos, 1.f, 0.f, 0.f); break; 43 case 2: f3(pos, 1.f, 1.f, 0.f); break; 44 default: FATAL("Unreachable code\n"); break; 45 } 46 } 47 48 static struct s3d_shape* 49 create_triangle(struct s3d_device* dev) 50 { 51 struct s3d_vertex_data vdata; 52 struct s3d_shape* tri = NULL; 53 54 vdata.type = S3D_FLOAT3; 55 vdata.usage = S3D_POSITION; 56 vdata.get = get_pos; 57 58 CHK(s3d_shape_create_mesh(dev, &tri) == RES_OK); 59 CHK(s3d_mesh_setup_indexed_vertices(tri, 1, get_ids, 3, &vdata, 1, NULL) 60 == RES_OK); 61 62 return tri; 63 } 64 65 /* Detach, attach and re-attach the same shape while the view is active */ 66 static void 67 test_scenario1(struct s3d_device* dev) 68 { 69 struct s3d_shape* triangle; 70 struct s3d_scene* scn = NULL; 71 struct s3d_scene_view* view = NULL; 72 const int mask = S3D_SAMPLE | S3D_TRACE; 73 74 CHK(s3d_scene_create(dev, &scn) == RES_OK); 75 76 triangle = create_triangle(dev); 77 CHK(s3d_scene_attach_shape(scn, triangle) == RES_OK); 78 CHK(s3d_scene_view_create(scn, mask, &view) == RES_OK); 79 80 CHK(s3d_scene_detach_shape(scn, triangle) == RES_OK); 81 CHK(s3d_scene_attach_shape(scn, triangle) == RES_OK); 82 CHK(s3d_scene_detach_shape(scn, triangle) == RES_OK); 83 84 CHK(s3d_shape_ref_put(triangle) == RES_OK); 85 CHK(s3d_scene_ref_put(scn) == RES_OK); 86 87 CHK(s3d_scene_view_ref_put(view) == RES_OK); 88 } 89 90 /* Detach and delete a shape. Then create a new one that in doing so could reuse 91 * the Id of the previous shape, before attaching it and detach it from the 92 * scene. All this while a view of the scene continues to rely on the geometry 93 * of the first shape */ 94 static void 95 test_scenario2(struct s3d_device* dev) 96 { 97 struct s3d_scene* scn = NULL; 98 struct s3d_scene_view* view; 99 struct s3d_shape* triangle; 100 const int mask = S3D_SAMPLE | S3D_TRACE; 101 102 CHK(s3d_scene_create(dev, &scn) == RES_OK); 103 104 triangle = create_triangle(dev); 105 CHK(s3d_scene_attach_shape(scn, triangle) == RES_OK); 106 CHK(s3d_scene_view_create(scn, mask, &view) == RES_OK); 107 108 CHK(s3d_scene_detach_shape(scn, triangle) == RES_OK); 109 CHK(s3d_shape_ref_put(triangle) == RES_OK); 110 111 triangle = create_triangle(dev); 112 CHK(s3d_scene_attach_shape(scn, triangle) == RES_OK); 113 CHK(s3d_scene_detach_shape(scn, triangle) == RES_OK); 114 CHK(s3d_shape_ref_put(triangle) == RES_OK); 115 116 CHK(s3d_scene_ref_put(scn) == RES_OK); 117 118 CHK(s3d_scene_view_ref_put(view) == RES_OK); 119 } 120 121 122 /******************************************************************************* 123 * The test 124 ******************************************************************************/ 125 int 126 main(void) 127 { 128 struct s3d_device* dev = NULL; 129 130 CHK(s3d_device_create(NULL, NULL, 0, &dev) == RES_OK); 131 132 test_scenario1(dev); 133 test_scenario2(dev); 134 135 CHK(s3d_device_ref_put(dev) == RES_OK); 136 137 CHK(mem_allocated_size() == 0); 138 return 0; 139 }