Merge branch '7.8'
[mesa.git] / src / gallium / tests / trivial / quad-tex.c
1 /**************************************************************************
2 *
3 * Copyright © 2010 Jakob Bornecrantz
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27 #define USE_TRACE 0
28 #define WIDTH 300
29 #define HEIGHT 300
30 #define NEAR 30
31 #define FAR 1000
32 #define FLIP 0
33
34 /* pipe_*_state structs */
35 #include "pipe/p_state.h"
36 /* pipe_context */
37 #include "pipe/p_context.h"
38 /* pipe_screen */
39 #include "pipe/p_screen.h"
40 /* PIPE_* */
41 #include "pipe/p_defines.h"
42 /* TGSI_SEMANTIC_{POSITION|GENERIC} */
43 #include "pipe/p_shader_tokens.h"
44 /* pipe_buffer_* helpers */
45 #include "util/u_inlines.h"
46
47 /* constant state object helper */
48 #include "cso_cache/cso_context.h"
49
50 /* u_sampler_view_default_template */
51 #include "util/u_sampler.h"
52 /* debug_dump_surface_bmp */
53 #include "util/u_debug.h"
54 /* util_draw_vertex_buffer helper */
55 #include "util/u_draw_quad.h"
56 /* FREE & CALLOC_STRUCT */
57 #include "util/u_memory.h"
58 /* util_make_[fragment|vertex]_passthrough_shader */
59 #include "util/u_simple_shaders.h"
60
61 /* softpipe software driver */
62 #include "softpipe/sp_public.h"
63
64 /* null software winsys */
65 #include "sw/null/null_sw_winsys.h"
66
67 /* traceing support see src/gallium/drivers/trace/README for more info. */
68 #if USE_TRACE
69 #include "trace/tr_screen.h"
70 #include "trace/tr_context.h"
71 #endif
72
73 struct program
74 {
75 struct pipe_screen *screen;
76 struct pipe_context *pipe;
77 struct cso_context *cso;
78
79 struct pipe_blend_state blend;
80 struct pipe_depth_stencil_alpha_state depthstencil;
81 struct pipe_rasterizer_state rasterizer;
82 struct pipe_sampler_state sampler;
83 struct pipe_viewport_state viewport;
84 struct pipe_framebuffer_state framebuffer;
85 struct pipe_vertex_element velem[2];
86
87 void *vs;
88 void *fs;
89
90 float clear_color[4];
91
92 struct pipe_resource *vbuf;
93 struct pipe_resource *target;
94 struct pipe_resource *tex;
95 struct pipe_sampler_view *view;
96 };
97
98 static void init_prog(struct program *p)
99 {
100 /* create the software rasterizer */
101 p->screen = softpipe_create_screen(null_sw_create());
102 #if USE_TRACE
103 p->screen = trace_screen_create(p->screen);
104 #endif
105 p->pipe = p->screen->context_create(p->screen, NULL);
106 p->cso = cso_create_context(p->pipe);
107
108 /* set clear color */
109 p->clear_color[0] = 0.3;
110 p->clear_color[1] = 0.1;
111 p->clear_color[2] = 0.3;
112 p->clear_color[3] = 1.0;
113
114 /* vertex buffer */
115 {
116 float vertices[4][2][4] = {
117 {
118 { 0.9f, 0.9f, 0.0f, 1.0f },
119 { 1.0f, 1.0f, 0.0f, 1.0f }
120 },
121 {
122 { -0.9f, 0.9f, 0.0f, 1.0f },
123 { 0.0f, 1.0f, 0.0f, 1.0f }
124 },
125 {
126 { -0.9f, -0.9f, 0.0f, 1.0f },
127 { 0.0f, 0.0f, 1.0f, 1.0f }
128 },
129 {
130 { 0.9f, -0.9f, 0.0f, 1.0f },
131 { 1.0f, 0.0f, 1.0f, 1.0f }
132 }
133 };
134
135 p->vbuf = pipe_buffer_create(p->screen, PIPE_BIND_VERTEX_BUFFER, sizeof(vertices));
136 pipe_buffer_write(p->pipe, p->vbuf, 0, sizeof(vertices), vertices);
137 }
138
139 /* render target texture */
140 {
141 struct pipe_resource tmplt;
142 memset(&tmplt, 0, sizeof(tmplt));
143 tmplt.target = PIPE_TEXTURE_2D;
144 tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */
145 tmplt.width0 = WIDTH;
146 tmplt.height0 = HEIGHT;
147 tmplt.depth0 = 1;
148 tmplt.last_level = 0;
149 tmplt.bind = PIPE_BIND_RENDER_TARGET;
150
151 p->target = p->screen->resource_create(p->screen, &tmplt);
152 }
153
154 /* sampler texture */
155 {
156 uint32_t *ptr;
157 struct pipe_transfer *t;
158 struct pipe_resource t_tmplt;
159 struct pipe_sampler_view v_tmplt;
160 struct pipe_subresource sub;
161 struct pipe_box box;
162
163 memset(&t_tmplt, 0, sizeof(t_tmplt));
164 t_tmplt.target = PIPE_TEXTURE_2D;
165 t_tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */
166 t_tmplt.width0 = 2;
167 t_tmplt.height0 = 2;
168 t_tmplt.depth0 = 1;
169 t_tmplt.last_level = 0;
170 t_tmplt.bind = PIPE_BIND_RENDER_TARGET;
171
172 p->tex = p->screen->resource_create(p->screen, &t_tmplt);
173
174 memset(&sub, 0, sizeof(sub));
175 memset(&box, 0, sizeof(box));
176 box.width = 2;
177 box.height = 2;
178
179 t = p->pipe->get_transfer(p->pipe, p->tex, sub, PIPE_TRANSFER_WRITE, &box);
180
181 ptr = p->pipe->transfer_map(p->pipe, t);
182 ptr[0] = 0xffff0000;
183 ptr[1] = 0xff0000ff;
184 ptr[2] = 0xff00ff00;
185 ptr[3] = 0xffffff00;
186 p->pipe->transfer_unmap(p->pipe, t);
187
188 p->pipe->transfer_destroy(p->pipe, t);
189
190 u_sampler_view_default_template(&v_tmplt, p->tex, p->tex->format);
191
192 p->view = p->pipe->create_sampler_view(p->pipe, p->tex, &v_tmplt);
193 }
194
195 /* disabled blending/masking */
196 memset(&p->blend, 0, sizeof(p->blend));
197 p->blend.rt[0].colormask = PIPE_MASK_RGBA;
198
199 /* no-op depth/stencil/alpha */
200 memset(&p->depthstencil, 0, sizeof(p->depthstencil));
201
202 /* rasterizer */
203 memset(&p->rasterizer, 0, sizeof(p->rasterizer));
204 p->rasterizer.front_winding = PIPE_WINDING_CW;
205 p->rasterizer.cull_mode = PIPE_WINDING_NONE;
206 p->rasterizer.gl_rasterization_rules = 1;
207
208 /* sampler */
209 memset(&p->sampler, 0, sizeof(p->sampler));
210 p->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
211 p->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
212 p->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
213 p->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
214 p->sampler.min_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
215 p->sampler.mag_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
216 p->sampler.normalized_coords = 1;
217
218 /* drawing destination */
219 memset(&p->framebuffer, 0, sizeof(p->framebuffer));
220 p->framebuffer.width = WIDTH;
221 p->framebuffer.height = HEIGHT;
222 p->framebuffer.nr_cbufs = 1;
223 p->framebuffer.cbufs[0] = p->screen->get_tex_surface(p->screen, p->target, 0, 0, 0, PIPE_BIND_RENDER_TARGET);
224
225 /* viewport, depth isn't really needed */
226 {
227 float x = 0;
228 float y = 0;
229 float z = FAR;
230 float half_width = (float)WIDTH / 2.0f;
231 float half_height = (float)HEIGHT / 2.0f;
232 float half_depth = ((float)FAR - (float)NEAR) / 2.0f;
233 float scale, bias;
234
235 if (FLIP) {
236 scale = -1.0f;
237 bias = (float)HEIGHT;
238 } else {
239 scale = 1.0f;
240 bias = 0.0f;
241 }
242
243 p->viewport.scale[0] = half_width;
244 p->viewport.scale[1] = half_height * scale;
245 p->viewport.scale[2] = half_depth;
246 p->viewport.scale[3] = 1.0f;
247
248 p->viewport.translate[0] = half_width + x;
249 p->viewport.translate[1] = (half_height + y) * scale + bias;
250 p->viewport.translate[2] = half_depth + z;
251 p->viewport.translate[3] = 0.0f;
252 }
253
254 /* vertex elements state */
255 memset(p->velem, 0, sizeof(p->velem));
256 p->velem[0].src_offset = 0 * 4 * sizeof(float); /* offset 0, first element */
257 p->velem[0].instance_divisor = 0;
258 p->velem[0].vertex_buffer_index = 0;
259 p->velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
260
261 p->velem[1].src_offset = 1 * 4 * sizeof(float); /* offset 16, second element */
262 p->velem[1].instance_divisor = 0;
263 p->velem[1].vertex_buffer_index = 0;
264 p->velem[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
265
266 /* vertex shader */
267 {
268 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
269 TGSI_SEMANTIC_GENERIC };
270 const uint semantic_indexes[] = { 0, 0 };
271 p->vs = util_make_vertex_passthrough_shader(p->pipe, 2, semantic_names, semantic_indexes);
272 }
273
274 /* fragment shader */
275 p->fs = util_make_fragment_tex_shader(p->pipe, TGSI_TEXTURE_2D);
276 }
277
278 static void close_prog(struct program *p)
279 {
280 /* unset bound textures as well */
281 cso_set_fragment_sampler_views(p->cso, 0, NULL);
282
283 /* unset all state */
284 cso_release_all(p->cso);
285
286 p->pipe->delete_vs_state(p->pipe, p->vs);
287 p->pipe->delete_fs_state(p->pipe, p->fs);
288
289 pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);
290 pipe_sampler_view_reference(&p->view, NULL);
291 pipe_resource_reference(&p->target, NULL);
292 pipe_resource_reference(&p->tex, NULL);
293 pipe_resource_reference(&p->vbuf, NULL);
294
295 cso_destroy_context(p->cso);
296 p->pipe->destroy(p->pipe);
297 p->screen->destroy(p->screen);
298
299 FREE(p);
300 }
301
302 static void draw(struct program *p)
303 {
304 /* set the render target */
305 cso_set_framebuffer(p->cso, &p->framebuffer);
306
307 /* clear the render target */
308 p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, p->clear_color, 0, 0);
309
310 /* set misc state we care about */
311 cso_set_blend(p->cso, &p->blend);
312 cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);
313 cso_set_rasterizer(p->cso, &p->rasterizer);
314 cso_set_viewport(p->cso, &p->viewport);
315
316 /* sampler */
317 cso_single_sampler(p->cso, 0, &p->sampler);
318 cso_single_sampler_done(p->cso);
319
320 /* texture sampler view */
321 cso_set_fragment_sampler_views(p->cso, 1, &p->view);
322
323 /* shaders */
324 cso_set_fragment_shader_handle(p->cso, p->fs);
325 cso_set_vertex_shader_handle(p->cso, p->vs);
326
327 /* vertex element data */
328 cso_set_vertex_elements(p->cso, 2, p->velem);
329
330 util_draw_vertex_buffer(p->pipe,
331 p->vbuf, 0,
332 PIPE_PRIM_QUADS,
333 4, /* verts */
334 2); /* attribs/vert */
335
336 p->pipe->flush(p->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
337
338 debug_dump_surface_bmp(p->pipe, "result.bmp", p->framebuffer.cbufs[0]);
339 }
340
341 int main(int argc, char** argv)
342 {
343 struct program *p = CALLOC_STRUCT(program);
344
345 init_prog(p);
346 draw(p);
347 close_prog(p);
348
349 return 0;
350 }