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