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 0
31 #define FAR 1
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 #include "util/macros.h"
51 /* u_sampler_view_default_template */
52 #include "util/u_sampler.h"
53 /* debug_dump_surface_bmp */
54 #include "util/u_debug_image.h"
55 /* util_draw_vertex_buffer helper */
56 #include "util/u_draw_quad.h"
57 /* FREE & CALLOC_STRUCT */
58 #include "util/u_memory.h"
59 /* util_make_[fragment|vertex]_passthrough_shader */
60 #include "util/u_simple_shaders.h"
61 /* to get a hardware pipe driver */
62 #include "pipe-loader/pipe_loader.h"
63 
64 struct program
65 {
66 	struct pipe_loader_device *dev;
67 	struct pipe_screen *screen;
68 	struct pipe_context *pipe;
69 	struct cso_context *cso;
70 
71 	struct pipe_blend_state blend;
72 	struct pipe_depth_stencil_alpha_state depthstencil;
73 	struct pipe_rasterizer_state rasterizer;
74 	struct pipe_sampler_state sampler;
75 	struct pipe_viewport_state viewport;
76 	struct pipe_framebuffer_state framebuffer;
77 	struct cso_velems_state velem;
78 
79 	void *vs;
80 	void *fs;
81 
82 	union pipe_color_union clear_color;
83 
84 	struct pipe_resource *vbuf;
85 	struct pipe_resource *target;
86 	struct pipe_resource *tex;
87 	struct pipe_sampler_view *view;
88 };
89 
init_prog(struct program * p)90 static void init_prog(struct program *p)
91 {
92 	struct pipe_surface surf_tmpl;
93 	ASSERTED int ret;
94 
95 	/* find a hardware device */
96 	ret = pipe_loader_probe(&p->dev, 1);
97 	assert(ret);
98 
99 	/* init a pipe screen */
100 	p->screen = pipe_loader_create_screen(p->dev);
101 	assert(p->screen);
102 
103 	/* create the pipe driver context and cso context */
104 	p->pipe = p->screen->context_create(p->screen, NULL, 0);
105 	p->cso = cso_create_context(p->pipe, 0);
106 
107 	/* set clear color */
108 	p->clear_color.f[0] = 0.3;
109 	p->clear_color.f[1] = 0.1;
110 	p->clear_color.f[2] = 0.3;
111 	p->clear_color.f[3] = 1.0;
112 
113 	/* vertex buffer */
114 	{
115 		float vertices[4][2][4] = {
116 			{
117 				{ 0.9f, 0.9f, 0.0f, 1.0f },
118 				{ 1.0f, 1.0f, 0.0f, 1.0f }
119 			},
120 			{
121 				{ -0.9f, 0.9f, 0.0f, 1.0f },
122 				{  0.0f, 1.0f, 0.0f, 1.0f }
123 			},
124 			{
125 				{ -0.9f, -0.9f, 0.0f, 1.0f },
126 				{  0.0f,  0.0f, 1.0f, 1.0f }
127 			},
128 			{
129 				{ 0.9f, -0.9f, 0.0f, 1.0f },
130 				{ 1.0f,  0.0f, 1.0f, 1.0f }
131 			}
132 		};
133 
134 		p->vbuf = pipe_buffer_create(p->screen, PIPE_BIND_VERTEX_BUFFER,
135 					     PIPE_USAGE_DEFAULT, 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.array_size = 1;
149 		tmplt.last_level = 0;
150 		tmplt.bind = PIPE_BIND_RENDER_TARGET;
151 
152 		p->target = p->screen->resource_create(p->screen, &tmplt);
153 	}
154 
155 	/* sampler texture */
156 	{
157 		uint32_t *ptr;
158 		struct pipe_transfer *t;
159 		struct pipe_resource t_tmplt;
160 		struct pipe_sampler_view v_tmplt;
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.array_size = 1;
170 		t_tmplt.last_level = 0;
171 		t_tmplt.bind = PIPE_BIND_RENDER_TARGET;
172 
173 		p->tex = p->screen->resource_create(p->screen, &t_tmplt);
174 
175 		memset(&box, 0, sizeof(box));
176 		box.width = 2;
177 		box.height = 2;
178 		box.depth = 1;
179 
180 		ptr = p->pipe->texture_map(p->pipe, p->tex, 0, PIPE_MAP_WRITE, &box, &t);
181 		ptr[0] = 0xffff0000;
182 		ptr[1] = 0xff0000ff;
183 		ptr[2] = 0xff00ff00;
184 		ptr[3] = 0xffffff00;
185 		p->pipe->texture_unmap(p->pipe, t);
186 
187 		u_sampler_view_default_template(&v_tmplt, p->tex, p->tex->format);
188 
189 		p->view = p->pipe->create_sampler_view(p->pipe, p->tex, &v_tmplt);
190 	}
191 
192 	/* disabled blending/masking */
193 	memset(&p->blend, 0, sizeof(p->blend));
194 	p->blend.rt[0].colormask = PIPE_MASK_RGBA;
195 
196 	/* no-op depth/stencil/alpha */
197 	memset(&p->depthstencil, 0, sizeof(p->depthstencil));
198 
199 	/* rasterizer */
200 	memset(&p->rasterizer, 0, sizeof(p->rasterizer));
201 	p->rasterizer.cull_face = PIPE_FACE_NONE;
202 	p->rasterizer.half_pixel_center = 1;
203 	p->rasterizer.bottom_edge_rule = 1;
204 	p->rasterizer.depth_clip_near = 1;
205 	p->rasterizer.depth_clip_far = 1;
206 
207 	/* sampler */
208 	memset(&p->sampler, 0, sizeof(p->sampler));
209 	p->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
210 	p->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
211 	p->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
212 	p->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
213 	p->sampler.min_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
214 	p->sampler.mag_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
215 	p->sampler.normalized_coords = 1;
216 
217 	surf_tmpl.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */
218 	surf_tmpl.u.tex.level = 0;
219 	surf_tmpl.u.tex.first_layer = 0;
220 	surf_tmpl.u.tex.last_layer = 0;
221 	/* drawing destination */
222 	memset(&p->framebuffer, 0, sizeof(p->framebuffer));
223 	p->framebuffer.width = WIDTH;
224 	p->framebuffer.height = HEIGHT;
225 	p->framebuffer.nr_cbufs = 1;
226 	p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, p->target, &surf_tmpl);
227 
228 	/* viewport, depth isn't really needed */
229 	{
230 		float x = 0;
231 		float y = 0;
232 		float z = NEAR;
233 		float half_width = (float)WIDTH / 2.0f;
234 		float half_height = (float)HEIGHT / 2.0f;
235 		float half_depth = ((float)FAR - (float)NEAR) / 2.0f;
236 		float scale, bias;
237 
238 		if (FLIP) {
239 			scale = -1.0f;
240 			bias = (float)HEIGHT;
241 		} else {
242 			scale = 1.0f;
243 			bias = 0.0f;
244 		}
245 
246 		p->viewport.scale[0] = half_width;
247 		p->viewport.scale[1] = half_height * scale;
248 		p->viewport.scale[2] = half_depth;
249 
250 		p->viewport.translate[0] = half_width + x;
251 		p->viewport.translate[1] = (half_height + y) * scale + bias;
252 		p->viewport.translate[2] = half_depth + z;
253 
254 		p->viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
255 		p->viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
256 		p->viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
257 		p->viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
258 	}
259 
260 	/* vertex elements state */
261 	memset(&p->velem, 0, sizeof(p->velem));
262         p->velem.count = 2;
263 
264 	p->velem.velems[0].src_offset = 0 * 4 * sizeof(float); /* offset 0, first element */
265 	p->velem.velems[0].instance_divisor = 0;
266 	p->velem.velems[0].vertex_buffer_index = 0;
267 	p->velem.velems[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
268 
269 	p->velem.velems[1].src_offset = 1 * 4 * sizeof(float); /* offset 16, second element */
270 	p->velem.velems[1].instance_divisor = 0;
271 	p->velem.velems[1].vertex_buffer_index = 0;
272 	p->velem.velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
273 
274 	/* vertex shader */
275 	{
276 		const enum tgsi_semantic semantic_names[] =
277                    { TGSI_SEMANTIC_POSITION, TGSI_SEMANTIC_GENERIC };
278 		const uint semantic_indexes[] = { 0, 0 };
279 		p->vs = util_make_vertex_passthrough_shader(p->pipe, 2, semantic_names, semantic_indexes, FALSE);
280 	}
281 
282 	/* fragment shader */
283 	p->fs = util_make_fragment_tex_shader(p->pipe, TGSI_TEXTURE_2D,
284 	                                      TGSI_INTERPOLATE_LINEAR,
285 	                                      TGSI_RETURN_TYPE_FLOAT,
286 	                                      TGSI_RETURN_TYPE_FLOAT, false,
287                                               false);
288 }
289 
close_prog(struct program * p)290 static void close_prog(struct program *p)
291 {
292 	cso_destroy_context(p->cso);
293 
294 	p->pipe->delete_vs_state(p->pipe, p->vs);
295 	p->pipe->delete_fs_state(p->pipe, p->fs);
296 
297 	pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);
298 	pipe_sampler_view_reference(&p->view, NULL);
299 	pipe_resource_reference(&p->target, NULL);
300 	pipe_resource_reference(&p->tex, NULL);
301 	pipe_resource_reference(&p->vbuf, NULL);
302 
303 	p->pipe->destroy(p->pipe);
304 	p->screen->destroy(p->screen);
305 	pipe_loader_release(&p->dev, 1);
306 
307 	FREE(p);
308 }
309 
draw(struct program * p)310 static void draw(struct program *p)
311 {
312 	const struct pipe_sampler_state *samplers[] = {&p->sampler};
313 
314 	/* set the render target */
315 	cso_set_framebuffer(p->cso, &p->framebuffer);
316 
317 	/* clear the render target */
318 	p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, NULL, &p->clear_color, 0, 0);
319 
320 	/* set misc state we care about */
321 	cso_set_blend(p->cso, &p->blend);
322 	cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);
323 	cso_set_rasterizer(p->cso, &p->rasterizer);
324 	cso_set_viewport(p->cso, &p->viewport);
325 
326 	/* sampler */
327 	cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 1, samplers);
328 
329 	/* texture sampler view */
330 	p->pipe->set_sampler_views(p->pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &p->view);
331 
332 	/* shaders */
333 	cso_set_fragment_shader_handle(p->cso, p->fs);
334 	cso_set_vertex_shader_handle(p->cso, p->vs);
335 
336 	/* vertex element data */
337 	cso_set_vertex_elements(p->cso, &p->velem);
338 
339 	util_draw_vertex_buffer(p->pipe, p->cso,
340 	                        p->vbuf, 0, 0,
341 	                        PIPE_PRIM_QUADS,
342 	                        4,  /* verts */
343 	                        2); /* attribs/vert */
344 
345         p->pipe->flush(p->pipe, NULL, 0);
346 
347 	debug_dump_surface_bmp(p->pipe, "result.bmp", p->framebuffer.cbufs[0]);
348 }
349 
main(int argc,char ** argv)350 int main(int argc, char** argv)
351 {
352 	struct program *p = CALLOC_STRUCT(program);
353 
354 	init_prog(p);
355 	draw(p);
356 	close_prog(p);
357 
358 	return 0;
359 }
360