Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[mesa.git] / src / gallium / tests / graw / shader-leak.c
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_debug.h" /* debug_dump_surface_bmp() */
13 #include "util/u_memory.h" /* Offset() */
14 #include "util/u_draw_quad.h"
15
16
17 static int num_iters = 100;
18
19
20 enum pipe_format formats[] = {
21 PIPE_FORMAT_R8G8B8A8_UNORM,
22 PIPE_FORMAT_B8G8R8A8_UNORM,
23 PIPE_FORMAT_NONE
24 };
25
26 static const int WIDTH = 300;
27 static const int HEIGHT = 300;
28
29 static struct pipe_screen *screen = NULL;
30 static struct pipe_context *ctx = NULL;
31 static struct pipe_surface *surf = NULL;
32 static void *window = NULL;
33
34 struct vertex {
35 float position[4];
36 float color[4];
37 };
38
39 static 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
50 static 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
73 static 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.max_index = sizeof(vertices) / vbuf.stride;
92 vbuf.buffer_offset = 0;
93 vbuf.buffer = screen->user_buffer_create(screen,
94 vertices,
95 sizeof(vertices),
96 PIPE_BIND_VERTEX_BUFFER);
97
98 ctx->set_vertex_buffers(ctx, 1, &vbuf);
99 }
100
101 static void set_vertex_shader( void )
102 {
103 void *handle;
104 const char *text =
105 "VERT\n"
106 "DCL IN[0]\n"
107 "DCL IN[1]\n"
108 "DCL OUT[0], POSITION\n"
109 "DCL OUT[1], COLOR\n"
110 " 0: MOV OUT[1], IN[1]\n"
111 " 1: MOV OUT[0], IN[0]\n"
112 " 2: END\n";
113
114 handle = graw_parse_vertex_shader(ctx, text);
115 ctx->bind_vs_state(ctx, handle);
116 }
117
118
119
120 static void *
121 set_fragment_shader( void )
122 {
123 void *handle;
124 const char *text =
125 "FRAG\n"
126 "DCL IN[0], COLOR, LINEAR\n"
127 "DCL OUT[0], COLOR\n"
128 "DCL TEMP[0..1]\n"
129 " 0: MUL TEMP[0], IN[0], IN[0]\n"
130 " 1: ADD TEMP[1], IN[0], IN[0]\n"
131 " 2: SUB OUT[0], TEMP[0], TEMP[1]\n"
132 " 3: END\n";
133
134 handle = graw_parse_fragment_shader(ctx, text);
135 return handle;
136 }
137
138
139 static void draw( void )
140 {
141 float clear_color[4] = {0, 0, 0, 1};
142 int i;
143
144 printf("Creating %d shaders\n", num_iters);
145
146 for (i = 0; i < num_iters; i++) {
147 void *fs = set_fragment_shader();
148
149 ctx->bind_fs_state(ctx, fs);
150
151 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
152 util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, 1);
153 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
154
155 ctx->bind_fs_state(ctx, NULL);
156 ctx->delete_fs_state(ctx, fs);
157 }
158
159 screen->flush_frontbuffer(screen, surf, window);
160 ctx->destroy(ctx);
161
162 exit(0);
163 }
164
165
166 static void init( void )
167 {
168 struct pipe_framebuffer_state fb;
169 struct pipe_resource *tex, templat;
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;
179 window == NULL && formats[i] != PIPE_FORMAT_NONE;
180 i++) {
181
182 screen = graw_create_window_and_screen(0,0,300,300,
183 formats[i],
184 &window);
185 }
186
187 ctx = screen->context_create(screen, NULL);
188 if (ctx == NULL)
189 exit(3);
190
191 templat.target = PIPE_TEXTURE_2D;
192 templat.format = formats[i];
193 templat.width0 = WIDTH;
194 templat.height0 = HEIGHT;
195 templat.depth0 = 1;
196 templat.last_level = 0;
197 templat.nr_samples = 1;
198 templat.bind = (PIPE_BIND_RENDER_TARGET |
199 PIPE_BIND_DISPLAY_TARGET);
200
201 tex = screen->resource_create(screen, &templat);
202 if (tex == NULL) {
203 fprintf(stderr, "Unable to create screen texture!\n");
204 exit(4);
205 }
206
207 surf = screen->get_tex_surface(screen, tex, 0, 0, 0,
208 PIPE_BIND_RENDER_TARGET |
209 PIPE_BIND_DISPLAY_TARGET);
210 if (surf == NULL)
211 exit(5);
212
213 memset(&fb, 0, sizeof fb);
214 fb.nr_cbufs = 1;
215 fb.width = WIDTH;
216 fb.height = HEIGHT;
217 fb.cbufs[0] = surf;
218
219 ctx->set_framebuffer_state(ctx, &fb);
220
221 {
222 struct pipe_blend_state blend;
223 void *handle;
224 memset(&blend, 0, sizeof blend);
225 blend.rt[0].colormask = PIPE_MASK_RGBA;
226 handle = ctx->create_blend_state(ctx, &blend);
227 ctx->bind_blend_state(ctx, handle);
228 }
229
230 {
231 struct pipe_depth_stencil_alpha_state depthstencil;
232 void *handle;
233 memset(&depthstencil, 0, sizeof depthstencil);
234 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
235 ctx->bind_depth_stencil_alpha_state(ctx, handle);
236 }
237
238 {
239 struct pipe_rasterizer_state rasterizer;
240 void *handle;
241 memset(&rasterizer, 0, sizeof rasterizer);
242 rasterizer.cull_face = PIPE_FACE_NONE;
243 rasterizer.gl_rasterization_rules = 1;
244 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
245 ctx->bind_rasterizer_state(ctx, handle);
246 }
247
248 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
249 set_vertices();
250 set_vertex_shader();
251 if (0)
252 set_fragment_shader();
253 }
254
255
256 int main( int argc, char *argv[] )
257 {
258 if (argc > 1)
259 num_iters = atoi(argv[1]);
260
261 init();
262
263 graw_set_display_func( draw );
264 graw_main_loop();
265 return 0;
266 }