test_s3d_sampler.c (8232B)
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 #include "test_s3d_cbox.h" 20 #include "test_s3d_utils.h" 21 22 #include <rsys/float3.h> 23 24 #define NSAMPS 4096 25 26 int 27 main(int argc, char** argv) 28 { 29 struct mem_allocator allocator; 30 struct s3d_device* dev; 31 struct s3d_scene* scn; 32 struct s3d_scene* scn2; 33 struct s3d_scene_view* scnview; 34 struct s3d_shape* cbox; 35 struct s3d_shape* walls; 36 struct s3d_shape* short_block; 37 struct s3d_shape* tall_block; 38 struct s3d_vertex_data attribs; 39 struct s3d_primitive prim; 40 struct s3d_primitive prim1; 41 struct s3d_attrib attr0, attr1; 42 struct cbox_desc desc; 43 float uv[2]; 44 size_t i; 45 unsigned ntris, nverts; 46 unsigned cbox_id; 47 unsigned walls_id; 48 unsigned short_block_id; 49 unsigned tall_block_id; 50 (void)argc, (void)argv; 51 52 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 53 54 CHK(s3d_device_create(NULL, &allocator, 1, &dev) == RES_OK); 55 CHK(s3d_scene_create(dev, &scn) == RES_OK); 56 CHK(s3d_scene_create(dev, &scn2) == RES_OK); 57 CHK(s3d_scene_instantiate(scn, &cbox) == RES_OK); 58 CHK(s3d_scene_attach_shape(scn2, cbox) == RES_OK); 59 CHK(s3d_shape_create_mesh(dev, &walls) == RES_OK); 60 CHK(s3d_shape_create_mesh(dev, &short_block) == RES_OK); 61 CHK(s3d_shape_create_mesh(dev, &tall_block) == RES_OK); 62 63 CHK(s3d_shape_get_id(cbox, &cbox_id) == RES_OK); 64 CHK(s3d_shape_get_id(walls, &walls_id) == RES_OK); 65 CHK(s3d_shape_get_id(short_block, &short_block_id) == RES_OK); 66 CHK(s3d_shape_get_id(tall_block, &tall_block_id) == RES_OK); 67 68 CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK); 69 CHK(s3d_scene_view_sample(NULL, 0, 0, 0, NULL, NULL) == RES_BAD_ARG); 70 CHK(s3d_scene_view_sample(scnview, 0, 0, 0, NULL, NULL) == RES_BAD_ARG); 71 CHK(s3d_scene_view_sample(NULL, 0, 0, 0, &prim, NULL) == RES_BAD_ARG); 72 CHK(s3d_scene_view_sample(scnview, 0, 0, 0, &prim, NULL) == RES_BAD_ARG); 73 CHK(s3d_scene_view_sample(NULL, 0, 0, 0, NULL, uv) == RES_BAD_ARG); 74 CHK(s3d_scene_view_sample(scnview, 0, 0, 0, NULL, uv) == RES_BAD_ARG); 75 CHK(s3d_scene_view_sample(NULL, 0, 0, 0, &prim, uv) == RES_BAD_ARG); 76 CHK(s3d_scene_view_sample(scnview, 0, 0, 0, &prim, uv) == RES_OK); 77 CHK(s3d_scene_view_sample(scnview, -1, 0, 0, &prim, uv) == RES_BAD_ARG); 78 CHK(s3d_scene_view_sample(scnview, 0, -1, 0, &prim, uv) == RES_BAD_ARG); 79 CHK(s3d_scene_view_sample(scnview, 0, 0, -1, &prim, uv) == RES_BAD_ARG); 80 CHK(s3d_scene_view_sample(scnview, 1, 0, 0, &prim, uv) == RES_BAD_ARG); 81 CHK(s3d_scene_view_sample(scnview, 0, 1, 0, &prim, uv) == RES_BAD_ARG); 82 CHK(s3d_scene_view_sample(scnview, 0, 0, 1, &prim, uv) == RES_BAD_ARG); 83 CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK); 84 CHK(S3D_PRIMITIVE_EQ(&prim, &S3D_PRIMITIVE_NULL) == 1); 85 CHK(s3d_scene_view_ref_put(scnview) == RES_OK); 86 87 attribs.usage = S3D_POSITION; 88 attribs.type = S3D_FLOAT3; 89 attribs.get = cbox_get_position; 90 91 ntris = cbox_walls_ntris; 92 nverts = cbox_walls_nverts; 93 desc.vertices = cbox_walls; 94 desc.indices = cbox_walls_ids; 95 CHK(s3d_mesh_setup_indexed_vertices 96 (walls, ntris, cbox_get_ids, nverts, &attribs, 1, &desc) == RES_OK); 97 98 CHK(s3d_scene_attach_shape(scn, walls) == RES_OK); 99 100 CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK); 101 102 CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK); 103 CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr0) == RES_OK); 104 CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK); 105 CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1) == RES_OK); 106 107 prim1 = prim; 108 CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 1); 109 prim1.inst_id = prim.inst_id + 1; 110 CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 0); 111 prim1.inst_id = prim.inst_id; 112 CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 1); 113 prim1.prim_id = S3D_INVALID_ID; 114 CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 0); 115 prim1.prim_id = prim.prim_id; 116 prim1.geom_id = S3D_INVALID_ID; 117 CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 0); 118 119 CHK(attr0.type == S3D_FLOAT3); 120 CHK(attr1.type == S3D_FLOAT3); 121 CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f) == 1); 122 123 CHK(s3d_scene_view_sample(scnview, 0.3f, 0.1f, 0.2f, &prim, uv) == RES_OK); 124 CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1) == RES_OK); 125 CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f) != 1); 126 127 CHK(s3d_scene_view_ref_put(scnview) == RES_OK); 128 129 CHK(s3d_shape_enable(walls, 0) == RES_OK); 130 CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK); 131 CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK); 132 CHK(S3D_PRIMITIVE_EQ(&prim, &S3D_PRIMITIVE_NULL) == 1); 133 CHK(s3d_scene_view_ref_put(scnview) == RES_OK); 134 CHK(s3d_shape_enable(walls, 1) == RES_OK); 135 136 ntris = cbox_block_ntris; 137 nverts = cbox_block_nverts; 138 desc.vertices = cbox_short_block; 139 desc.indices = cbox_block_ids; 140 CHK(s3d_mesh_setup_indexed_vertices 141 (short_block, ntris, cbox_get_ids, nverts, &attribs, 1, &desc) == RES_OK); 142 143 CHK(s3d_scene_attach_shape(scn, short_block) == RES_OK); 144 CHK(s3d_scene_attach_shape(scn, tall_block) == RES_OK); 145 146 CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK); 147 CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK); 148 CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr0) == RES_OK); 149 desc.vertices = cbox_tall_block; 150 CHK(s3d_mesh_setup_indexed_vertices 151 (tall_block, ntris, cbox_get_ids, nverts, &attribs, 1, &desc) == RES_OK); 152 CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK); 153 CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1) == RES_OK); 154 CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f) == 1); 155 CHK(s3d_scene_view_ref_put(scnview) == RES_OK); 156 157 CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK); 158 CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK); 159 CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1) == RES_OK); 160 CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f) != 1); 161 CHK(s3d_scene_view_ref_put(scnview) == RES_OK); 162 163 CHK(s3d_shape_enable(cbox, 0) == RES_OK); 164 CHK(s3d_scene_view_create(scn2, S3D_SAMPLE|S3D_TRACE, &scnview) == RES_OK); 165 CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK); 166 CHK(S3D_PRIMITIVE_EQ(&prim, &S3D_PRIMITIVE_NULL) == 1); 167 CHK(s3d_scene_view_ref_put(scnview) == RES_OK); 168 CHK(s3d_shape_enable(cbox, 1) == RES_OK); 169 CHK(s3d_scene_view_create(scn2, S3D_SAMPLE|S3D_TRACE, &scnview) == RES_OK); 170 FOR_EACH(i, 0, NSAMPS) { 171 const float u = rand_canonic(); 172 const float v = rand_canonic(); 173 const float w = rand_canonic(); 174 CHK(s3d_scene_view_sample(scnview, u, v, w, &prim, uv) == RES_OK); 175 CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr0) == RES_OK); 176 177 CHK(prim.inst_id == cbox_id); 178 CHK(prim.geom_id == walls_id 179 || prim.geom_id == tall_block_id 180 || prim.geom_id == short_block_id); 181 CHK(prim.prim_id < 10); 182 CHK(prim.scene_prim_id >= prim.prim_id); 183 CHK(prim.scene_prim_id < 30); 184 printf("%f %f %f\n", SPLIT3(attr0.value)); 185 } 186 CHK(s3d_scene_view_ref_put(scnview) == RES_OK); 187 188 CHK(s3d_device_ref_put(dev) == RES_OK); 189 CHK(s3d_scene_ref_put(scn) == RES_OK); 190 CHK(s3d_scene_ref_put(scn2) == RES_OK); 191 CHK(s3d_shape_ref_put(cbox) == RES_OK); 192 CHK(s3d_shape_ref_put(walls) == RES_OK); 193 CHK(s3d_shape_ref_put(short_block) == RES_OK); 194 CHK(s3d_shape_ref_put(tall_block) == RES_OK); 195 196 check_memory_allocator(&allocator); 197 mem_shutdown_proxy_allocator(&allocator); 198 CHK(mem_allocated_size() == 0); 199 return 0; 200 } 201