Path: blob/21.2-virgl/src/gallium/tests/graw/shader-leak.c
4565 views
/**1* Create shaders in a loop to test memory usage.2*/34#include <stdio.h>5#include "frontend/graw.h"6#include "pipe/p_screen.h"7#include "pipe/p_context.h"8#include "pipe/p_state.h"9#include "pipe/p_defines.h"1011#include "util/u_memory.h" /* Offset() */12#include "util/u_draw_quad.h"13#include "util/u_inlines.h"141516static int num_iters = 100;171819enum pipe_format formats[] = {20PIPE_FORMAT_RGBA8888_UNORM,21PIPE_FORMAT_BGRA8888_UNORM,22PIPE_FORMAT_NONE23};2425static const int WIDTH = 300;26static const int HEIGHT = 300;2728static 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;3334struct vertex {35float position[4];36float color[4];37};3839static 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};4647484950static void set_viewport( float x, float y,51float width, float height,52float zNear, float zFar)53{54float z = zFar;55float half_width = (float)width / 2.0f;56float half_height = (float)height / 2.0f;57float half_depth = ((float)zFar - (float)zNear) / 2.0f;58struct pipe_viewport_state vp;5960vp.scale[0] = half_width;61vp.scale[1] = half_height;62vp.scale[2] = half_depth;6364vp.translate[0] = half_width + x;65vp.translate[1] = half_height + y;66vp.translate[2] = half_depth + z;6768vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;69vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;70vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;71vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;7273ctx->set_viewport_states( ctx, 0, 1, &vp );74}7576static void set_vertices( void )77{78struct pipe_vertex_element ve[2];79struct pipe_vertex_buffer vbuf;80void *handle;8182memset(ve, 0, sizeof ve);8384ve[0].src_offset = Offset(struct vertex, position);85ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;86ve[1].src_offset = Offset(struct vertex, color);87ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;8889handle = ctx->create_vertex_elements_state(ctx, 2, ve);90ctx->bind_vertex_elements_state(ctx, handle);9192memset(&vbuf, 0, sizeof vbuf);9394vbuf.stride = sizeof(struct vertex);95vbuf.buffer_offset = 0;96vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,97PIPE_BIND_VERTEX_BUFFER,98PIPE_USAGE_DEFAULT,99sizeof(vertices),100vertices);101102ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf);103}104105static void set_vertex_shader( void )106{107void *handle;108const char *text =109"VERT\n"110"DCL IN[0]\n"111"DCL IN[1]\n"112"DCL OUT[0], POSITION\n"113"DCL OUT[1], COLOR\n"114" 0: MOV OUT[1], IN[1]\n"115" 1: MOV OUT[0], IN[0]\n"116" 2: END\n";117118handle = graw_parse_vertex_shader(ctx, text);119ctx->bind_vs_state(ctx, handle);120}121122123124static void *125set_fragment_shader( void )126{127void *handle;128const char *text =129"FRAG\n"130"DCL IN[0], COLOR, LINEAR\n"131"DCL OUT[0], COLOR\n"132"DCL TEMP[0..1]\n"133" 0: MUL TEMP[0], IN[0], IN[0]\n"134" 1: ADD TEMP[1], IN[0], IN[0]\n"135" 2: SUB OUT[0], TEMP[0], TEMP[1]\n"136" 3: END\n";137138handle = graw_parse_fragment_shader(ctx, text);139return handle;140}141142143static void draw( void )144{145union pipe_color_union clear_color = { {0,0,0,1} };146int i;147148printf("Creating %d shaders\n", num_iters);149150for (i = 0; i < num_iters; i++) {151void *fs = set_fragment_shader();152153ctx->bind_fs_state(ctx, fs);154155ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);156util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, 1);157ctx->flush(ctx, NULL, 0);158159ctx->bind_fs_state(ctx, NULL);160ctx->delete_fs_state(ctx, fs);161}162163screen->flush_frontbuffer(screen, ctx, tex, 0, 0, window, NULL);164ctx->destroy(ctx);165166exit(0);167}168169170static void init( void )171{172struct pipe_framebuffer_state fb;173struct pipe_resource templat;174struct pipe_surface surf_tmpl;175int i;176177/* It's hard to say whether window or screen should be created178* first. Different environments would prefer one or the other.179*180* Also, no easy way of querying supported formats if the screen181* cannot be created first.182*/183for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {184screen = graw_create_window_and_screen(0, 0, 300, 300,185formats[i],186&window);187if (window && screen)188break;189}190if (!screen || !window) {191fprintf(stderr, "Unable to create window\n");192exit(1);193}194195ctx = screen->context_create(screen, NULL, 0);196if (ctx == NULL)197exit(3);198199memset(&templat, 0, sizeof(templat));200templat.target = PIPE_TEXTURE_2D;201templat.format = formats[i];202templat.width0 = WIDTH;203templat.height0 = HEIGHT;204templat.depth0 = 1;205templat.last_level = 0;206templat.bind = (PIPE_BIND_RENDER_TARGET |207PIPE_BIND_DISPLAY_TARGET);208209tex = screen->resource_create(screen, &templat);210if (tex == NULL) {211fprintf(stderr, "Unable to create screen texture!\n");212exit(4);213}214215surf_tmpl.format = templat.format;216surf_tmpl.u.tex.level = 0;217surf_tmpl.u.tex.first_layer = 0;218surf_tmpl.u.tex.last_layer = 0;219surf = ctx->create_surface(ctx, tex, &surf_tmpl);220if (surf == NULL) {221fprintf(stderr, "Unable to create tex surface!\n");222exit(5);223}224225memset(&fb, 0, sizeof fb);226fb.nr_cbufs = 1;227fb.width = WIDTH;228fb.height = HEIGHT;229fb.cbufs[0] = surf;230231ctx->set_framebuffer_state(ctx, &fb);232233{234struct pipe_blend_state blend;235void *handle;236memset(&blend, 0, sizeof blend);237blend.rt[0].colormask = PIPE_MASK_RGBA;238handle = ctx->create_blend_state(ctx, &blend);239ctx->bind_blend_state(ctx, handle);240}241242{243struct pipe_depth_stencil_alpha_state depthstencil;244void *handle;245memset(&depthstencil, 0, sizeof depthstencil);246handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);247ctx->bind_depth_stencil_alpha_state(ctx, handle);248}249250{251struct pipe_rasterizer_state rasterizer;252void *handle;253memset(&rasterizer, 0, sizeof rasterizer);254rasterizer.cull_face = PIPE_FACE_NONE;255rasterizer.half_pixel_center = 1;256rasterizer.bottom_edge_rule = 1;257rasterizer.depth_clip_near = 1;258rasterizer.depth_clip_far = 1;259handle = ctx->create_rasterizer_state(ctx, &rasterizer);260ctx->bind_rasterizer_state(ctx, handle);261}262263set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);264set_vertices();265set_vertex_shader();266if (0)267set_fragment_shader();268}269270271int main( int argc, char *argv[] )272{273if (argc > 1)274num_iters = atoi(argv[1]);275276init();277278graw_set_display_func( draw );279graw_main_loop();280return 0;281}282283284