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