1bf215546Sopenharmony_ci/** 2bf215546Sopenharmony_ci * Create shaders in a loop to test memory usage. 3bf215546Sopenharmony_ci */ 4bf215546Sopenharmony_ci 5bf215546Sopenharmony_ci#include <stdio.h> 6bf215546Sopenharmony_ci#include "frontend/graw.h" 7bf215546Sopenharmony_ci#include "pipe/p_screen.h" 8bf215546Sopenharmony_ci#include "pipe/p_context.h" 9bf215546Sopenharmony_ci#include "pipe/p_state.h" 10bf215546Sopenharmony_ci#include "pipe/p_defines.h" 11bf215546Sopenharmony_ci 12bf215546Sopenharmony_ci#include "util/u_memory.h" /* Offset() */ 13bf215546Sopenharmony_ci#include "util/u_draw_quad.h" 14bf215546Sopenharmony_ci#include "util/u_inlines.h" 15bf215546Sopenharmony_ci 16bf215546Sopenharmony_ci 17bf215546Sopenharmony_cistatic int num_iters = 100; 18bf215546Sopenharmony_ci 19bf215546Sopenharmony_ci 20bf215546Sopenharmony_cienum pipe_format formats[] = { 21bf215546Sopenharmony_ci PIPE_FORMAT_RGBA8888_UNORM, 22bf215546Sopenharmony_ci PIPE_FORMAT_BGRA8888_UNORM, 23bf215546Sopenharmony_ci PIPE_FORMAT_NONE 24bf215546Sopenharmony_ci}; 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_cistatic const int WIDTH = 300; 27bf215546Sopenharmony_cistatic const int HEIGHT = 300; 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_cistatic struct pipe_screen *screen = NULL; 30bf215546Sopenharmony_cistatic struct pipe_context *ctx = NULL; 31bf215546Sopenharmony_cistatic struct pipe_surface *surf = NULL; 32bf215546Sopenharmony_cistatic struct pipe_resource *tex = NULL; 33bf215546Sopenharmony_cistatic void *window = NULL; 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cistruct vertex { 36bf215546Sopenharmony_ci float position[4]; 37bf215546Sopenharmony_ci float color[4]; 38bf215546Sopenharmony_ci}; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_cistatic struct vertex vertices[1] = 41bf215546Sopenharmony_ci{ 42bf215546Sopenharmony_ci { 43bf215546Sopenharmony_ci { 0.0f, -0.9f, 0.0f, 1.0f }, 44bf215546Sopenharmony_ci { 1.0f, 0.0f, 0.0f, 1.0f } 45bf215546Sopenharmony_ci } 46bf215546Sopenharmony_ci}; 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_cistatic void set_viewport( float x, float y, 52bf215546Sopenharmony_ci float width, float height, 53bf215546Sopenharmony_ci float zNear, float zFar) 54bf215546Sopenharmony_ci{ 55bf215546Sopenharmony_ci float z = zFar; 56bf215546Sopenharmony_ci float half_width = (float)width / 2.0f; 57bf215546Sopenharmony_ci float half_height = (float)height / 2.0f; 58bf215546Sopenharmony_ci float half_depth = ((float)zFar - (float)zNear) / 2.0f; 59bf215546Sopenharmony_ci struct pipe_viewport_state vp; 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci vp.scale[0] = half_width; 62bf215546Sopenharmony_ci vp.scale[1] = half_height; 63bf215546Sopenharmony_ci vp.scale[2] = half_depth; 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci vp.translate[0] = half_width + x; 66bf215546Sopenharmony_ci vp.translate[1] = half_height + y; 67bf215546Sopenharmony_ci vp.translate[2] = half_depth + z; 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X; 70bf215546Sopenharmony_ci vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y; 71bf215546Sopenharmony_ci vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z; 72bf215546Sopenharmony_ci vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W; 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci ctx->set_viewport_states( ctx, 0, 1, &vp ); 75bf215546Sopenharmony_ci} 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_cistatic void set_vertices( void ) 78bf215546Sopenharmony_ci{ 79bf215546Sopenharmony_ci struct pipe_vertex_element ve[2]; 80bf215546Sopenharmony_ci struct pipe_vertex_buffer vbuf; 81bf215546Sopenharmony_ci void *handle; 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci memset(ve, 0, sizeof ve); 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci ve[0].src_offset = Offset(struct vertex, position); 86bf215546Sopenharmony_ci ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 87bf215546Sopenharmony_ci ve[1].src_offset = Offset(struct vertex, color); 88bf215546Sopenharmony_ci ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci handle = ctx->create_vertex_elements_state(ctx, 2, ve); 91bf215546Sopenharmony_ci ctx->bind_vertex_elements_state(ctx, handle); 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci memset(&vbuf, 0, sizeof vbuf); 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci vbuf.stride = sizeof(struct vertex); 96bf215546Sopenharmony_ci vbuf.buffer_offset = 0; 97bf215546Sopenharmony_ci vbuf.buffer.resource = pipe_buffer_create_with_data(ctx, 98bf215546Sopenharmony_ci PIPE_BIND_VERTEX_BUFFER, 99bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 100bf215546Sopenharmony_ci sizeof(vertices), 101bf215546Sopenharmony_ci vertices); 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf); 104bf215546Sopenharmony_ci} 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_cistatic void set_vertex_shader( void ) 107bf215546Sopenharmony_ci{ 108bf215546Sopenharmony_ci void *handle; 109bf215546Sopenharmony_ci const char *text = 110bf215546Sopenharmony_ci "VERT\n" 111bf215546Sopenharmony_ci "DCL IN[0]\n" 112bf215546Sopenharmony_ci "DCL IN[1]\n" 113bf215546Sopenharmony_ci "DCL OUT[0], POSITION\n" 114bf215546Sopenharmony_ci "DCL OUT[1], COLOR\n" 115bf215546Sopenharmony_ci " 0: MOV OUT[1], IN[1]\n" 116bf215546Sopenharmony_ci " 1: MOV OUT[0], IN[0]\n" 117bf215546Sopenharmony_ci " 2: END\n"; 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci handle = graw_parse_vertex_shader(ctx, text); 120bf215546Sopenharmony_ci ctx->bind_vs_state(ctx, handle); 121bf215546Sopenharmony_ci} 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_cistatic void * 126bf215546Sopenharmony_ciset_fragment_shader( void ) 127bf215546Sopenharmony_ci{ 128bf215546Sopenharmony_ci void *handle; 129bf215546Sopenharmony_ci const char *text = 130bf215546Sopenharmony_ci "FRAG\n" 131bf215546Sopenharmony_ci "DCL IN[0], COLOR, LINEAR\n" 132bf215546Sopenharmony_ci "DCL OUT[0], COLOR\n" 133bf215546Sopenharmony_ci "DCL TEMP[0..1]\n" 134bf215546Sopenharmony_ci " 0: MUL TEMP[0], IN[0], IN[0]\n" 135bf215546Sopenharmony_ci " 1: ADD TEMP[1], IN[0], IN[0]\n" 136bf215546Sopenharmony_ci " 2: SUB OUT[0], TEMP[0], TEMP[1]\n" 137bf215546Sopenharmony_ci " 3: END\n"; 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci handle = graw_parse_fragment_shader(ctx, text); 140bf215546Sopenharmony_ci return handle; 141bf215546Sopenharmony_ci} 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_cistatic void draw( void ) 145bf215546Sopenharmony_ci{ 146bf215546Sopenharmony_ci union pipe_color_union clear_color = { {0,0,0,1} }; 147bf215546Sopenharmony_ci int i; 148bf215546Sopenharmony_ci 149bf215546Sopenharmony_ci printf("Creating %d shaders\n", num_iters); 150bf215546Sopenharmony_ci 151bf215546Sopenharmony_ci for (i = 0; i < num_iters; i++) { 152bf215546Sopenharmony_ci void *fs = set_fragment_shader(); 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci ctx->bind_fs_state(ctx, fs); 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0); 157bf215546Sopenharmony_ci util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, 1); 158bf215546Sopenharmony_ci ctx->flush(ctx, NULL, 0); 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci ctx->bind_fs_state(ctx, NULL); 161bf215546Sopenharmony_ci ctx->delete_fs_state(ctx, fs); 162bf215546Sopenharmony_ci } 163bf215546Sopenharmony_ci 164bf215546Sopenharmony_ci screen->flush_frontbuffer(screen, ctx, tex, 0, 0, window, NULL); 165bf215546Sopenharmony_ci ctx->destroy(ctx); 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci exit(0); 168bf215546Sopenharmony_ci} 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_cistatic void init( void ) 172bf215546Sopenharmony_ci{ 173bf215546Sopenharmony_ci struct pipe_framebuffer_state fb; 174bf215546Sopenharmony_ci struct pipe_resource templat; 175bf215546Sopenharmony_ci struct pipe_surface surf_tmpl; 176bf215546Sopenharmony_ci int i; 177bf215546Sopenharmony_ci 178bf215546Sopenharmony_ci /* It's hard to say whether window or screen should be created 179bf215546Sopenharmony_ci * first. Different environments would prefer one or the other. 180bf215546Sopenharmony_ci * 181bf215546Sopenharmony_ci * Also, no easy way of querying supported formats if the screen 182bf215546Sopenharmony_ci * cannot be created first. 183bf215546Sopenharmony_ci */ 184bf215546Sopenharmony_ci for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) { 185bf215546Sopenharmony_ci screen = graw_create_window_and_screen(0, 0, 300, 300, 186bf215546Sopenharmony_ci formats[i], 187bf215546Sopenharmony_ci &window); 188bf215546Sopenharmony_ci if (window && screen) 189bf215546Sopenharmony_ci break; 190bf215546Sopenharmony_ci } 191bf215546Sopenharmony_ci if (!screen || !window) { 192bf215546Sopenharmony_ci fprintf(stderr, "Unable to create window\n"); 193bf215546Sopenharmony_ci exit(1); 194bf215546Sopenharmony_ci } 195bf215546Sopenharmony_ci 196bf215546Sopenharmony_ci ctx = screen->context_create(screen, NULL, 0); 197bf215546Sopenharmony_ci if (ctx == NULL) 198bf215546Sopenharmony_ci exit(3); 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci memset(&templat, 0, sizeof(templat)); 201bf215546Sopenharmony_ci templat.target = PIPE_TEXTURE_2D; 202bf215546Sopenharmony_ci templat.format = formats[i]; 203bf215546Sopenharmony_ci templat.width0 = WIDTH; 204bf215546Sopenharmony_ci templat.height0 = HEIGHT; 205bf215546Sopenharmony_ci templat.depth0 = 1; 206bf215546Sopenharmony_ci templat.last_level = 0; 207bf215546Sopenharmony_ci templat.bind = (PIPE_BIND_RENDER_TARGET | 208bf215546Sopenharmony_ci PIPE_BIND_DISPLAY_TARGET); 209bf215546Sopenharmony_ci 210bf215546Sopenharmony_ci tex = screen->resource_create(screen, &templat); 211bf215546Sopenharmony_ci if (tex == NULL) { 212bf215546Sopenharmony_ci fprintf(stderr, "Unable to create screen texture!\n"); 213bf215546Sopenharmony_ci exit(4); 214bf215546Sopenharmony_ci } 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ci surf_tmpl.format = templat.format; 217bf215546Sopenharmony_ci surf_tmpl.u.tex.level = 0; 218bf215546Sopenharmony_ci surf_tmpl.u.tex.first_layer = 0; 219bf215546Sopenharmony_ci surf_tmpl.u.tex.last_layer = 0; 220bf215546Sopenharmony_ci surf = ctx->create_surface(ctx, tex, &surf_tmpl); 221bf215546Sopenharmony_ci if (surf == NULL) { 222bf215546Sopenharmony_ci fprintf(stderr, "Unable to create tex surface!\n"); 223bf215546Sopenharmony_ci exit(5); 224bf215546Sopenharmony_ci } 225bf215546Sopenharmony_ci 226bf215546Sopenharmony_ci memset(&fb, 0, sizeof fb); 227bf215546Sopenharmony_ci fb.nr_cbufs = 1; 228bf215546Sopenharmony_ci fb.width = WIDTH; 229bf215546Sopenharmony_ci fb.height = HEIGHT; 230bf215546Sopenharmony_ci fb.cbufs[0] = surf; 231bf215546Sopenharmony_ci 232bf215546Sopenharmony_ci ctx->set_framebuffer_state(ctx, &fb); 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_ci { 235bf215546Sopenharmony_ci struct pipe_blend_state blend; 236bf215546Sopenharmony_ci void *handle; 237bf215546Sopenharmony_ci memset(&blend, 0, sizeof blend); 238bf215546Sopenharmony_ci blend.rt[0].colormask = PIPE_MASK_RGBA; 239bf215546Sopenharmony_ci handle = ctx->create_blend_state(ctx, &blend); 240bf215546Sopenharmony_ci ctx->bind_blend_state(ctx, handle); 241bf215546Sopenharmony_ci } 242bf215546Sopenharmony_ci 243bf215546Sopenharmony_ci { 244bf215546Sopenharmony_ci struct pipe_depth_stencil_alpha_state depthstencil; 245bf215546Sopenharmony_ci void *handle; 246bf215546Sopenharmony_ci memset(&depthstencil, 0, sizeof depthstencil); 247bf215546Sopenharmony_ci handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil); 248bf215546Sopenharmony_ci ctx->bind_depth_stencil_alpha_state(ctx, handle); 249bf215546Sopenharmony_ci } 250bf215546Sopenharmony_ci 251bf215546Sopenharmony_ci { 252bf215546Sopenharmony_ci struct pipe_rasterizer_state rasterizer; 253bf215546Sopenharmony_ci void *handle; 254bf215546Sopenharmony_ci memset(&rasterizer, 0, sizeof rasterizer); 255bf215546Sopenharmony_ci rasterizer.cull_face = PIPE_FACE_NONE; 256bf215546Sopenharmony_ci rasterizer.half_pixel_center = 1; 257bf215546Sopenharmony_ci rasterizer.bottom_edge_rule = 1; 258bf215546Sopenharmony_ci rasterizer.depth_clip_near = 1; 259bf215546Sopenharmony_ci rasterizer.depth_clip_far = 1; 260bf215546Sopenharmony_ci handle = ctx->create_rasterizer_state(ctx, &rasterizer); 261bf215546Sopenharmony_ci ctx->bind_rasterizer_state(ctx, handle); 262bf215546Sopenharmony_ci } 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_ci set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000); 265bf215546Sopenharmony_ci set_vertices(); 266bf215546Sopenharmony_ci set_vertex_shader(); 267bf215546Sopenharmony_ci if (0) 268bf215546Sopenharmony_ci set_fragment_shader(); 269bf215546Sopenharmony_ci} 270bf215546Sopenharmony_ci 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_ciint main( int argc, char *argv[] ) 273bf215546Sopenharmony_ci{ 274bf215546Sopenharmony_ci if (argc > 1) 275bf215546Sopenharmony_ci num_iters = atoi(argv[1]); 276bf215546Sopenharmony_ci 277bf215546Sopenharmony_ci init(); 278bf215546Sopenharmony_ci 279bf215546Sopenharmony_ci graw_set_display_func( draw ); 280bf215546Sopenharmony_ci graw_main_loop(); 281bf215546Sopenharmony_ci return 0; 282bf215546Sopenharmony_ci} 283