1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Test draw instancing. 3bf215546Sopenharmony_ci */ 4bf215546Sopenharmony_ci 5bf215546Sopenharmony_ci#include <stdio.h> 6bf215546Sopenharmony_ci#include <string.h> 7bf215546Sopenharmony_ci 8bf215546Sopenharmony_ci#include "frontend/graw.h" 9bf215546Sopenharmony_ci#include "pipe/p_screen.h" 10bf215546Sopenharmony_ci#include "pipe/p_context.h" 11bf215546Sopenharmony_ci#include "pipe/p_state.h" 12bf215546Sopenharmony_ci#include "pipe/p_defines.h" 13bf215546Sopenharmony_ci 14bf215546Sopenharmony_ci#include "util/u_memory.h" /* Offset() */ 15bf215546Sopenharmony_ci#include "util/u_draw_quad.h" 16bf215546Sopenharmony_ci#include "util/u_inlines.h" 17bf215546Sopenharmony_ci 18bf215546Sopenharmony_ci 19bf215546Sopenharmony_cienum pipe_format formats[] = { 20bf215546Sopenharmony_ci PIPE_FORMAT_RGBA8888_UNORM, 21bf215546Sopenharmony_ci PIPE_FORMAT_BGRA8888_UNORM, 22bf215546Sopenharmony_ci PIPE_FORMAT_NONE 23bf215546Sopenharmony_ci}; 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_cistatic const int WIDTH = 300; 26bf215546Sopenharmony_cistatic const int HEIGHT = 300; 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_cistatic struct pipe_screen *screen = NULL; 29bf215546Sopenharmony_cistatic struct pipe_context *ctx = NULL; 30bf215546Sopenharmony_cistatic struct pipe_surface *surf = NULL; 31bf215546Sopenharmony_cistatic struct pipe_resource *tex = NULL; 32bf215546Sopenharmony_cistatic void *window = NULL; 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_cistruct vertex { 35bf215546Sopenharmony_ci float position[4]; 36bf215546Sopenharmony_ci float color[4]; 37bf215546Sopenharmony_ci}; 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_cistatic int draw_elements = 0; 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci/** 44bf215546Sopenharmony_ci * Vertex data. 45bf215546Sopenharmony_ci * Each vertex has three attributes: position, color and translation. 46bf215546Sopenharmony_ci * The translation attribute is a per-instance attribute. See 47bf215546Sopenharmony_ci * "instance_divisor" below. 48bf215546Sopenharmony_ci */ 49bf215546Sopenharmony_cistatic struct vertex vertices[4] = 50bf215546Sopenharmony_ci{ 51bf215546Sopenharmony_ci { 52bf215546Sopenharmony_ci { 0.0f, -0.3f, 0.0f, 1.0f }, /* pos */ 53bf215546Sopenharmony_ci { 1.0f, 0.0f, 0.0f, 1.0f } /* color */ 54bf215546Sopenharmony_ci }, 55bf215546Sopenharmony_ci { 56bf215546Sopenharmony_ci { -0.2f, 0.3f, 0.0f, 1.0f }, 57bf215546Sopenharmony_ci { 0.0f, 1.0f, 0.0f, 1.0f } 58bf215546Sopenharmony_ci }, 59bf215546Sopenharmony_ci { 60bf215546Sopenharmony_ci { 0.2f, 0.3f, 0.0f, 1.0f }, 61bf215546Sopenharmony_ci { 0.0f, 0.0f, 1.0f, 1.0f } 62bf215546Sopenharmony_ci } 63bf215546Sopenharmony_ci}; 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci#define NUM_INST 5 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_cistatic float inst_data[NUM_INST][4] = 69bf215546Sopenharmony_ci{ 70bf215546Sopenharmony_ci { -0.50f, 0.4f, 0.0f, 0.0f }, 71bf215546Sopenharmony_ci { -0.25f, 0.1f, 0.0f, 0.0f }, 72bf215546Sopenharmony_ci { 0.00f, 0.2f, 0.0f, 0.0f }, 73bf215546Sopenharmony_ci { 0.25f, 0.1f, 0.0f, 0.0f }, 74bf215546Sopenharmony_ci { 0.50f, 0.3f, 0.0f, 0.0f } 75bf215546Sopenharmony_ci}; 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_cistatic ushort indices[3] = { 0, 2, 1 }; 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci 81bf215546Sopenharmony_cistatic void set_viewport( float x, float y, 82bf215546Sopenharmony_ci float width, float height, 83bf215546Sopenharmony_ci float zNear, float zFar) 84bf215546Sopenharmony_ci{ 85bf215546Sopenharmony_ci float z = zFar; 86bf215546Sopenharmony_ci float half_width = (float)width / 2.0f; 87bf215546Sopenharmony_ci float half_height = (float)height / 2.0f; 88bf215546Sopenharmony_ci float half_depth = ((float)zFar - (float)zNear) / 2.0f; 89bf215546Sopenharmony_ci struct pipe_viewport_state vp; 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci vp.scale[0] = half_width; 92bf215546Sopenharmony_ci vp.scale[1] = half_height; 93bf215546Sopenharmony_ci vp.scale[2] = half_depth; 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci vp.translate[0] = half_width + x; 96bf215546Sopenharmony_ci vp.translate[1] = half_height + y; 97bf215546Sopenharmony_ci vp.translate[2] = half_depth + z; 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X; 100bf215546Sopenharmony_ci vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y; 101bf215546Sopenharmony_ci vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z; 102bf215546Sopenharmony_ci vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W; 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci ctx->set_viewport_states( ctx, 0, 1, &vp ); 105bf215546Sopenharmony_ci} 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_cistatic void set_vertices( void ) 109bf215546Sopenharmony_ci{ 110bf215546Sopenharmony_ci struct pipe_vertex_element ve[3]; 111bf215546Sopenharmony_ci struct pipe_vertex_buffer vbuf[2]; 112bf215546Sopenharmony_ci void *handle; 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci memset(ve, 0, sizeof ve); 115bf215546Sopenharmony_ci 116bf215546Sopenharmony_ci /* pos */ 117bf215546Sopenharmony_ci ve[0].src_offset = Offset(struct vertex, position); 118bf215546Sopenharmony_ci ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 119bf215546Sopenharmony_ci ve[0].vertex_buffer_index = 0; 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci /* color */ 122bf215546Sopenharmony_ci ve[1].src_offset = Offset(struct vertex, color); 123bf215546Sopenharmony_ci ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 124bf215546Sopenharmony_ci ve[1].vertex_buffer_index = 0; 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci /* per-instance info */ 127bf215546Sopenharmony_ci ve[2].src_offset = 0; 128bf215546Sopenharmony_ci ve[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 129bf215546Sopenharmony_ci ve[2].vertex_buffer_index = 1; 130bf215546Sopenharmony_ci ve[2].instance_divisor = 1; 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci handle = ctx->create_vertex_elements_state(ctx, 3, ve); 133bf215546Sopenharmony_ci ctx->bind_vertex_elements_state(ctx, handle); 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci memset(&vbuf, 0, sizeof vbuf); 136bf215546Sopenharmony_ci 137bf215546Sopenharmony_ci /* vertex data */ 138bf215546Sopenharmony_ci vbuf[0].stride = sizeof( struct vertex ); 139bf215546Sopenharmony_ci vbuf[0].buffer_offset = 0; 140bf215546Sopenharmony_ci vbuf[0].buffer.resource = pipe_buffer_create_with_data(ctx, 141bf215546Sopenharmony_ci PIPE_BIND_VERTEX_BUFFER, 142bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 143bf215546Sopenharmony_ci sizeof(vertices), 144bf215546Sopenharmony_ci vertices); 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci /* instance data */ 147bf215546Sopenharmony_ci vbuf[1].stride = sizeof( inst_data[0] ); 148bf215546Sopenharmony_ci vbuf[1].buffer_offset = 0; 149bf215546Sopenharmony_ci vbuf[1].buffer.resource = pipe_buffer_create_with_data(ctx, 150bf215546Sopenharmony_ci PIPE_BIND_VERTEX_BUFFER, 151bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 152bf215546Sopenharmony_ci sizeof(inst_data), 153bf215546Sopenharmony_ci inst_data); 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci ctx->set_vertex_buffers(ctx, 0, 2, 0, false, vbuf); 156bf215546Sopenharmony_ci} 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_cistatic void set_vertex_shader( void ) 159bf215546Sopenharmony_ci{ 160bf215546Sopenharmony_ci void *handle; 161bf215546Sopenharmony_ci const char *text = 162bf215546Sopenharmony_ci "VERT\n" 163bf215546Sopenharmony_ci "DCL IN[0]\n" 164bf215546Sopenharmony_ci "DCL IN[1]\n" 165bf215546Sopenharmony_ci "DCL IN[2]\n" 166bf215546Sopenharmony_ci "DCL OUT[0], POSITION\n" 167bf215546Sopenharmony_ci "DCL OUT[1], COLOR\n" 168bf215546Sopenharmony_ci " 0: MOV OUT[1], IN[1]\n" 169bf215546Sopenharmony_ci " 1: ADD OUT[0], IN[0], IN[2]\n" /* add instance pos to vertex pos */ 170bf215546Sopenharmony_ci " 2: END\n"; 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci handle = graw_parse_vertex_shader(ctx, text); 173bf215546Sopenharmony_ci ctx->bind_vs_state(ctx, handle); 174bf215546Sopenharmony_ci} 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_cistatic void set_fragment_shader( void ) 177bf215546Sopenharmony_ci{ 178bf215546Sopenharmony_ci void *handle; 179bf215546Sopenharmony_ci const char *text = 180bf215546Sopenharmony_ci "FRAG\n" 181bf215546Sopenharmony_ci "DCL IN[0], COLOR, LINEAR\n" 182bf215546Sopenharmony_ci "DCL OUT[0], COLOR\n" 183bf215546Sopenharmony_ci " 0: MOV OUT[0], IN[0]\n" 184bf215546Sopenharmony_ci " 1: END\n"; 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci handle = graw_parse_fragment_shader(ctx, text); 187bf215546Sopenharmony_ci ctx->bind_fs_state(ctx, handle); 188bf215546Sopenharmony_ci} 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_cistatic void draw( void ) 192bf215546Sopenharmony_ci{ 193bf215546Sopenharmony_ci union pipe_color_union clear_color = { {1,0,1,1} }; 194bf215546Sopenharmony_ci struct pipe_draw_info info; 195bf215546Sopenharmony_ci struct pipe_draw_start_count_bias draw; 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0); 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci util_draw_init_info(&info); 201bf215546Sopenharmony_ci info.index_size = draw_elements ? 2 : 0; 202bf215546Sopenharmony_ci info.mode = PIPE_PRIM_TRIANGLES; 203bf215546Sopenharmony_ci draw.start = 0; 204bf215546Sopenharmony_ci draw.count = 3; 205bf215546Sopenharmony_ci draw.index_bias = 0; 206bf215546Sopenharmony_ci /* draw NUM_INST triangles */ 207bf215546Sopenharmony_ci info.instance_count = NUM_INST; 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci /* index data */ 210bf215546Sopenharmony_ci if (info.index_size) { 211bf215546Sopenharmony_ci info.index.resource = 212bf215546Sopenharmony_ci pipe_buffer_create_with_data(ctx, 213bf215546Sopenharmony_ci PIPE_BIND_INDEX_BUFFER, 214bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 215bf215546Sopenharmony_ci sizeof(indices), 216bf215546Sopenharmony_ci indices); 217bf215546Sopenharmony_ci } 218bf215546Sopenharmony_ci 219bf215546Sopenharmony_ci ctx->draw_vbo(ctx, &info, 0, NULL, &draw, 1); 220bf215546Sopenharmony_ci 221bf215546Sopenharmony_ci pipe_resource_reference(&info.index.resource, NULL); 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ci ctx->flush(ctx, NULL, 0); 224bf215546Sopenharmony_ci 225bf215546Sopenharmony_ci graw_save_surface_to_file(ctx, surf, NULL); 226bf215546Sopenharmony_ci 227bf215546Sopenharmony_ci screen->flush_frontbuffer(screen, ctx, tex, 0, 0, window, NULL); 228bf215546Sopenharmony_ci} 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci 231bf215546Sopenharmony_cistatic void init( void ) 232bf215546Sopenharmony_ci{ 233bf215546Sopenharmony_ci struct pipe_framebuffer_state fb; 234bf215546Sopenharmony_ci struct pipe_resource templat; 235bf215546Sopenharmony_ci struct pipe_surface surf_tmpl; 236bf215546Sopenharmony_ci int i; 237bf215546Sopenharmony_ci 238bf215546Sopenharmony_ci /* It's hard to say whether window or screen should be created 239bf215546Sopenharmony_ci * first. Different environments would prefer one or the other. 240bf215546Sopenharmony_ci * 241bf215546Sopenharmony_ci * Also, no easy way of querying supported formats if the screen 242bf215546Sopenharmony_ci * cannot be created first. 243bf215546Sopenharmony_ci */ 244bf215546Sopenharmony_ci for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) { 245bf215546Sopenharmony_ci screen = graw_create_window_and_screen(0, 0, 300, 300, 246bf215546Sopenharmony_ci formats[i], 247bf215546Sopenharmony_ci &window); 248bf215546Sopenharmony_ci if (window && screen) 249bf215546Sopenharmony_ci break; 250bf215546Sopenharmony_ci } 251bf215546Sopenharmony_ci if (!screen || !window) { 252bf215546Sopenharmony_ci fprintf(stderr, "Unable to create window\n"); 253bf215546Sopenharmony_ci exit(1); 254bf215546Sopenharmony_ci } 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_ci ctx = screen->context_create(screen, NULL, 0); 257bf215546Sopenharmony_ci if (ctx == NULL) 258bf215546Sopenharmony_ci exit(3); 259bf215546Sopenharmony_ci 260bf215546Sopenharmony_ci memset(&templat, 0, sizeof(templat)); 261bf215546Sopenharmony_ci templat.target = PIPE_TEXTURE_2D; 262bf215546Sopenharmony_ci templat.format = formats[i]; 263bf215546Sopenharmony_ci templat.width0 = WIDTH; 264bf215546Sopenharmony_ci templat.height0 = HEIGHT; 265bf215546Sopenharmony_ci templat.depth0 = 1; 266bf215546Sopenharmony_ci templat.array_size = 1; 267bf215546Sopenharmony_ci templat.last_level = 0; 268bf215546Sopenharmony_ci templat.bind = (PIPE_BIND_RENDER_TARGET | 269bf215546Sopenharmony_ci PIPE_BIND_DISPLAY_TARGET); 270bf215546Sopenharmony_ci 271bf215546Sopenharmony_ci tex = screen->resource_create(screen, 272bf215546Sopenharmony_ci &templat); 273bf215546Sopenharmony_ci if (tex == NULL) 274bf215546Sopenharmony_ci exit(4); 275bf215546Sopenharmony_ci 276bf215546Sopenharmony_ci surf_tmpl.format = templat.format; 277bf215546Sopenharmony_ci surf_tmpl.u.tex.level = 0; 278bf215546Sopenharmony_ci surf_tmpl.u.tex.first_layer = 0; 279bf215546Sopenharmony_ci surf_tmpl.u.tex.last_layer = 0; 280bf215546Sopenharmony_ci surf = ctx->create_surface(ctx, tex, &surf_tmpl); 281bf215546Sopenharmony_ci if (surf == NULL) 282bf215546Sopenharmony_ci exit(5); 283bf215546Sopenharmony_ci 284bf215546Sopenharmony_ci memset(&fb, 0, sizeof fb); 285bf215546Sopenharmony_ci fb.nr_cbufs = 1; 286bf215546Sopenharmony_ci fb.width = WIDTH; 287bf215546Sopenharmony_ci fb.height = HEIGHT; 288bf215546Sopenharmony_ci fb.cbufs[0] = surf; 289bf215546Sopenharmony_ci 290bf215546Sopenharmony_ci ctx->set_framebuffer_state(ctx, &fb); 291bf215546Sopenharmony_ci 292bf215546Sopenharmony_ci { 293bf215546Sopenharmony_ci struct pipe_blend_state blend; 294bf215546Sopenharmony_ci void *handle; 295bf215546Sopenharmony_ci memset(&blend, 0, sizeof blend); 296bf215546Sopenharmony_ci blend.rt[0].colormask = PIPE_MASK_RGBA; 297bf215546Sopenharmony_ci handle = ctx->create_blend_state(ctx, &blend); 298bf215546Sopenharmony_ci ctx->bind_blend_state(ctx, handle); 299bf215546Sopenharmony_ci } 300bf215546Sopenharmony_ci 301bf215546Sopenharmony_ci { 302bf215546Sopenharmony_ci struct pipe_depth_stencil_alpha_state depthstencil; 303bf215546Sopenharmony_ci void *handle; 304bf215546Sopenharmony_ci memset(&depthstencil, 0, sizeof depthstencil); 305bf215546Sopenharmony_ci handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil); 306bf215546Sopenharmony_ci ctx->bind_depth_stencil_alpha_state(ctx, handle); 307bf215546Sopenharmony_ci } 308bf215546Sopenharmony_ci 309bf215546Sopenharmony_ci { 310bf215546Sopenharmony_ci struct pipe_rasterizer_state rasterizer; 311bf215546Sopenharmony_ci void *handle; 312bf215546Sopenharmony_ci memset(&rasterizer, 0, sizeof rasterizer); 313bf215546Sopenharmony_ci rasterizer.cull_face = PIPE_FACE_NONE; 314bf215546Sopenharmony_ci rasterizer.half_pixel_center = 1; 315bf215546Sopenharmony_ci rasterizer.bottom_edge_rule = 1; 316bf215546Sopenharmony_ci rasterizer.depth_clip_near = 1; 317bf215546Sopenharmony_ci rasterizer.depth_clip_far = 1; 318bf215546Sopenharmony_ci handle = ctx->create_rasterizer_state(ctx, &rasterizer); 319bf215546Sopenharmony_ci ctx->bind_rasterizer_state(ctx, handle); 320bf215546Sopenharmony_ci } 321bf215546Sopenharmony_ci 322bf215546Sopenharmony_ci set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000); 323bf215546Sopenharmony_ci set_vertices(); 324bf215546Sopenharmony_ci set_vertex_shader(); 325bf215546Sopenharmony_ci set_fragment_shader(); 326bf215546Sopenharmony_ci} 327bf215546Sopenharmony_ci 328bf215546Sopenharmony_ci 329bf215546Sopenharmony_cistatic void options(int argc, char *argv[]) 330bf215546Sopenharmony_ci{ 331bf215546Sopenharmony_ci int i; 332bf215546Sopenharmony_ci 333bf215546Sopenharmony_ci for (i = 1; i < argc;) { 334bf215546Sopenharmony_ci if (graw_parse_args(&i, argc, argv)) { 335bf215546Sopenharmony_ci continue; 336bf215546Sopenharmony_ci } 337bf215546Sopenharmony_ci if (strcmp(argv[i], "-e") == 0) { 338bf215546Sopenharmony_ci draw_elements = 1; 339bf215546Sopenharmony_ci i++; 340bf215546Sopenharmony_ci } 341bf215546Sopenharmony_ci else { 342bf215546Sopenharmony_ci i++; 343bf215546Sopenharmony_ci } 344bf215546Sopenharmony_ci } 345bf215546Sopenharmony_ci if (draw_elements) 346bf215546Sopenharmony_ci printf("Using pipe_context::draw_elements_instanced()\n"); 347bf215546Sopenharmony_ci else 348bf215546Sopenharmony_ci printf("Using pipe_context::draw_arrays_instanced()\n"); 349bf215546Sopenharmony_ci} 350bf215546Sopenharmony_ci 351bf215546Sopenharmony_ci 352bf215546Sopenharmony_ciint main( int argc, char *argv[] ) 353bf215546Sopenharmony_ci{ 354bf215546Sopenharmony_ci options(argc, argv); 355bf215546Sopenharmony_ci 356bf215546Sopenharmony_ci init(); 357bf215546Sopenharmony_ci 358bf215546Sopenharmony_ci graw_set_display_func( draw ); 359bf215546Sopenharmony_ci graw_main_loop(); 360bf215546Sopenharmony_ci return 0; 361bf215546Sopenharmony_ci} 362