13464ebd5Sriastradh/************************************************************************** 23464ebd5Sriastradh * 33464ebd5Sriastradh * Copyright © 2010 Jakob Bornecrantz 43464ebd5Sriastradh * 53464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a 63464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"), 73464ebd5Sriastradh * to deal in the Software without restriction, including without limitation 83464ebd5Sriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense, 93464ebd5Sriastradh * and/or sell copies of the Software, and to permit persons to whom the 103464ebd5Sriastradh * Software is furnished to do so, subject to the following conditions: 113464ebd5Sriastradh * 123464ebd5Sriastradh * The above copyright notice and this permission notice (including the next 133464ebd5Sriastradh * paragraph) shall be included in all copies or substantial portions of the 143464ebd5Sriastradh * Software. 153464ebd5Sriastradh * 163464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 173464ebd5Sriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 183464ebd5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 193464ebd5Sriastradh * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 203464ebd5Sriastradh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 213464ebd5Sriastradh * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 223464ebd5Sriastradh * DEALINGS IN THE SOFTWARE. 233464ebd5Sriastradh * 243464ebd5Sriastradh **************************************************************************/ 253464ebd5Sriastradh 263464ebd5Sriastradh 273464ebd5Sriastradh#define USE_TRACE 0 283464ebd5Sriastradh#define WIDTH 300 293464ebd5Sriastradh#define HEIGHT 300 3001e04c3fSmrg#define NEAR 0 3101e04c3fSmrg#define FAR 1 323464ebd5Sriastradh#define FLIP 0 333464ebd5Sriastradh 343464ebd5Sriastradh/* pipe_*_state structs */ 353464ebd5Sriastradh#include "pipe/p_state.h" 363464ebd5Sriastradh/* pipe_context */ 373464ebd5Sriastradh#include "pipe/p_context.h" 383464ebd5Sriastradh/* pipe_screen */ 393464ebd5Sriastradh#include "pipe/p_screen.h" 403464ebd5Sriastradh/* PIPE_* */ 413464ebd5Sriastradh#include "pipe/p_defines.h" 423464ebd5Sriastradh/* TGSI_SEMANTIC_{POSITION|GENERIC} */ 433464ebd5Sriastradh#include "pipe/p_shader_tokens.h" 443464ebd5Sriastradh/* pipe_buffer_* helpers */ 453464ebd5Sriastradh#include "util/u_inlines.h" 463464ebd5Sriastradh 473464ebd5Sriastradh/* constant state object helper */ 483464ebd5Sriastradh#include "cso_cache/cso_context.h" 493464ebd5Sriastradh 507ec681f3Smrg#include "util/macros.h" 513464ebd5Sriastradh/* u_sampler_view_default_template */ 523464ebd5Sriastradh#include "util/u_sampler.h" 533464ebd5Sriastradh/* debug_dump_surface_bmp */ 5401e04c3fSmrg#include "util/u_debug_image.h" 553464ebd5Sriastradh/* util_draw_vertex_buffer helper */ 563464ebd5Sriastradh#include "util/u_draw_quad.h" 573464ebd5Sriastradh/* FREE & CALLOC_STRUCT */ 583464ebd5Sriastradh#include "util/u_memory.h" 593464ebd5Sriastradh/* util_make_[fragment|vertex]_passthrough_shader */ 603464ebd5Sriastradh#include "util/u_simple_shaders.h" 61af69d88dSmrg/* to get a hardware pipe driver */ 62af69d88dSmrg#include "pipe-loader/pipe_loader.h" 633464ebd5Sriastradh 643464ebd5Sriastradhstruct program 653464ebd5Sriastradh{ 66af69d88dSmrg struct pipe_loader_device *dev; 673464ebd5Sriastradh struct pipe_screen *screen; 683464ebd5Sriastradh struct pipe_context *pipe; 693464ebd5Sriastradh struct cso_context *cso; 703464ebd5Sriastradh 713464ebd5Sriastradh struct pipe_blend_state blend; 723464ebd5Sriastradh struct pipe_depth_stencil_alpha_state depthstencil; 733464ebd5Sriastradh struct pipe_rasterizer_state rasterizer; 743464ebd5Sriastradh struct pipe_sampler_state sampler; 753464ebd5Sriastradh struct pipe_viewport_state viewport; 763464ebd5Sriastradh struct pipe_framebuffer_state framebuffer; 777ec681f3Smrg struct cso_velems_state velem; 783464ebd5Sriastradh 793464ebd5Sriastradh void *vs; 803464ebd5Sriastradh void *fs; 813464ebd5Sriastradh 82af69d88dSmrg union pipe_color_union clear_color; 833464ebd5Sriastradh 843464ebd5Sriastradh struct pipe_resource *vbuf; 853464ebd5Sriastradh struct pipe_resource *target; 863464ebd5Sriastradh struct pipe_resource *tex; 873464ebd5Sriastradh struct pipe_sampler_view *view; 883464ebd5Sriastradh}; 893464ebd5Sriastradh 903464ebd5Sriastradhstatic void init_prog(struct program *p) 913464ebd5Sriastradh{ 923464ebd5Sriastradh struct pipe_surface surf_tmpl; 937ec681f3Smrg ASSERTED int ret; 94af69d88dSmrg 95af69d88dSmrg /* find a hardware device */ 96af69d88dSmrg ret = pipe_loader_probe(&p->dev, 1); 97af69d88dSmrg assert(ret); 98af69d88dSmrg 99af69d88dSmrg /* init a pipe screen */ 10001e04c3fSmrg p->screen = pipe_loader_create_screen(p->dev); 101af69d88dSmrg assert(p->screen); 1023464ebd5Sriastradh 1033464ebd5Sriastradh /* create the pipe driver context and cso context */ 10401e04c3fSmrg p->pipe = p->screen->context_create(p->screen, NULL, 0); 10501e04c3fSmrg p->cso = cso_create_context(p->pipe, 0); 1063464ebd5Sriastradh 1073464ebd5Sriastradh /* set clear color */ 108af69d88dSmrg p->clear_color.f[0] = 0.3; 109af69d88dSmrg p->clear_color.f[1] = 0.1; 110af69d88dSmrg p->clear_color.f[2] = 0.3; 111af69d88dSmrg p->clear_color.f[3] = 1.0; 1123464ebd5Sriastradh 1133464ebd5Sriastradh /* vertex buffer */ 1143464ebd5Sriastradh { 1153464ebd5Sriastradh float vertices[4][2][4] = { 1163464ebd5Sriastradh { 1173464ebd5Sriastradh { 0.9f, 0.9f, 0.0f, 1.0f }, 1183464ebd5Sriastradh { 1.0f, 1.0f, 0.0f, 1.0f } 1193464ebd5Sriastradh }, 1203464ebd5Sriastradh { 1213464ebd5Sriastradh { -0.9f, 0.9f, 0.0f, 1.0f }, 1223464ebd5Sriastradh { 0.0f, 1.0f, 0.0f, 1.0f } 1233464ebd5Sriastradh }, 1243464ebd5Sriastradh { 1253464ebd5Sriastradh { -0.9f, -0.9f, 0.0f, 1.0f }, 1263464ebd5Sriastradh { 0.0f, 0.0f, 1.0f, 1.0f } 1273464ebd5Sriastradh }, 1283464ebd5Sriastradh { 1293464ebd5Sriastradh { 0.9f, -0.9f, 0.0f, 1.0f }, 1303464ebd5Sriastradh { 1.0f, 0.0f, 1.0f, 1.0f } 1313464ebd5Sriastradh } 1323464ebd5Sriastradh }; 1333464ebd5Sriastradh 1343464ebd5Sriastradh p->vbuf = pipe_buffer_create(p->screen, PIPE_BIND_VERTEX_BUFFER, 135af69d88dSmrg PIPE_USAGE_DEFAULT, sizeof(vertices)); 1363464ebd5Sriastradh pipe_buffer_write(p->pipe, p->vbuf, 0, sizeof(vertices), vertices); 1373464ebd5Sriastradh } 1383464ebd5Sriastradh 1393464ebd5Sriastradh /* render target texture */ 1403464ebd5Sriastradh { 1413464ebd5Sriastradh struct pipe_resource tmplt; 1423464ebd5Sriastradh memset(&tmplt, 0, sizeof(tmplt)); 1433464ebd5Sriastradh tmplt.target = PIPE_TEXTURE_2D; 1443464ebd5Sriastradh tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */ 1453464ebd5Sriastradh tmplt.width0 = WIDTH; 1463464ebd5Sriastradh tmplt.height0 = HEIGHT; 1473464ebd5Sriastradh tmplt.depth0 = 1; 1483464ebd5Sriastradh tmplt.array_size = 1; 1493464ebd5Sriastradh tmplt.last_level = 0; 1503464ebd5Sriastradh tmplt.bind = PIPE_BIND_RENDER_TARGET; 1513464ebd5Sriastradh 1523464ebd5Sriastradh p->target = p->screen->resource_create(p->screen, &tmplt); 1533464ebd5Sriastradh } 1543464ebd5Sriastradh 1553464ebd5Sriastradh /* sampler texture */ 1563464ebd5Sriastradh { 1573464ebd5Sriastradh uint32_t *ptr; 1583464ebd5Sriastradh struct pipe_transfer *t; 1593464ebd5Sriastradh struct pipe_resource t_tmplt; 1603464ebd5Sriastradh struct pipe_sampler_view v_tmplt; 1613464ebd5Sriastradh struct pipe_box box; 1623464ebd5Sriastradh 1633464ebd5Sriastradh memset(&t_tmplt, 0, sizeof(t_tmplt)); 1643464ebd5Sriastradh t_tmplt.target = PIPE_TEXTURE_2D; 1653464ebd5Sriastradh t_tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */ 1663464ebd5Sriastradh t_tmplt.width0 = 2; 1673464ebd5Sriastradh t_tmplt.height0 = 2; 1683464ebd5Sriastradh t_tmplt.depth0 = 1; 1693464ebd5Sriastradh t_tmplt.array_size = 1; 1703464ebd5Sriastradh t_tmplt.last_level = 0; 1713464ebd5Sriastradh t_tmplt.bind = PIPE_BIND_RENDER_TARGET; 1723464ebd5Sriastradh 1733464ebd5Sriastradh p->tex = p->screen->resource_create(p->screen, &t_tmplt); 1743464ebd5Sriastradh 1753464ebd5Sriastradh memset(&box, 0, sizeof(box)); 1763464ebd5Sriastradh box.width = 2; 1773464ebd5Sriastradh box.height = 2; 17801e04c3fSmrg box.depth = 1; 1793464ebd5Sriastradh 1807ec681f3Smrg ptr = p->pipe->texture_map(p->pipe, p->tex, 0, PIPE_MAP_WRITE, &box, &t); 1813464ebd5Sriastradh ptr[0] = 0xffff0000; 1823464ebd5Sriastradh ptr[1] = 0xff0000ff; 1833464ebd5Sriastradh ptr[2] = 0xff00ff00; 1843464ebd5Sriastradh ptr[3] = 0xffffff00; 1857ec681f3Smrg p->pipe->texture_unmap(p->pipe, t); 1863464ebd5Sriastradh 1873464ebd5Sriastradh u_sampler_view_default_template(&v_tmplt, p->tex, p->tex->format); 1883464ebd5Sriastradh 1893464ebd5Sriastradh p->view = p->pipe->create_sampler_view(p->pipe, p->tex, &v_tmplt); 1903464ebd5Sriastradh } 1913464ebd5Sriastradh 1923464ebd5Sriastradh /* disabled blending/masking */ 1933464ebd5Sriastradh memset(&p->blend, 0, sizeof(p->blend)); 1943464ebd5Sriastradh p->blend.rt[0].colormask = PIPE_MASK_RGBA; 1953464ebd5Sriastradh 1963464ebd5Sriastradh /* no-op depth/stencil/alpha */ 1973464ebd5Sriastradh memset(&p->depthstencil, 0, sizeof(p->depthstencil)); 1983464ebd5Sriastradh 1993464ebd5Sriastradh /* rasterizer */ 2003464ebd5Sriastradh memset(&p->rasterizer, 0, sizeof(p->rasterizer)); 2013464ebd5Sriastradh p->rasterizer.cull_face = PIPE_FACE_NONE; 202af69d88dSmrg p->rasterizer.half_pixel_center = 1; 203af69d88dSmrg p->rasterizer.bottom_edge_rule = 1; 20401e04c3fSmrg p->rasterizer.depth_clip_near = 1; 20501e04c3fSmrg p->rasterizer.depth_clip_far = 1; 2063464ebd5Sriastradh 2073464ebd5Sriastradh /* sampler */ 2083464ebd5Sriastradh memset(&p->sampler, 0, sizeof(p->sampler)); 2093464ebd5Sriastradh p->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 2103464ebd5Sriastradh p->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 2113464ebd5Sriastradh p->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 2123464ebd5Sriastradh p->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 2133464ebd5Sriastradh p->sampler.min_img_filter = PIPE_TEX_MIPFILTER_LINEAR; 2143464ebd5Sriastradh p->sampler.mag_img_filter = PIPE_TEX_MIPFILTER_LINEAR; 2153464ebd5Sriastradh p->sampler.normalized_coords = 1; 2163464ebd5Sriastradh 2173464ebd5Sriastradh surf_tmpl.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */ 2183464ebd5Sriastradh surf_tmpl.u.tex.level = 0; 2193464ebd5Sriastradh surf_tmpl.u.tex.first_layer = 0; 2203464ebd5Sriastradh surf_tmpl.u.tex.last_layer = 0; 2213464ebd5Sriastradh /* drawing destination */ 2223464ebd5Sriastradh memset(&p->framebuffer, 0, sizeof(p->framebuffer)); 2233464ebd5Sriastradh p->framebuffer.width = WIDTH; 2243464ebd5Sriastradh p->framebuffer.height = HEIGHT; 2253464ebd5Sriastradh p->framebuffer.nr_cbufs = 1; 2263464ebd5Sriastradh p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, p->target, &surf_tmpl); 2273464ebd5Sriastradh 2283464ebd5Sriastradh /* viewport, depth isn't really needed */ 2293464ebd5Sriastradh { 2303464ebd5Sriastradh float x = 0; 2313464ebd5Sriastradh float y = 0; 23201e04c3fSmrg float z = NEAR; 2333464ebd5Sriastradh float half_width = (float)WIDTH / 2.0f; 2343464ebd5Sriastradh float half_height = (float)HEIGHT / 2.0f; 2353464ebd5Sriastradh float half_depth = ((float)FAR - (float)NEAR) / 2.0f; 2363464ebd5Sriastradh float scale, bias; 2373464ebd5Sriastradh 2383464ebd5Sriastradh if (FLIP) { 2393464ebd5Sriastradh scale = -1.0f; 2403464ebd5Sriastradh bias = (float)HEIGHT; 2413464ebd5Sriastradh } else { 2423464ebd5Sriastradh scale = 1.0f; 2433464ebd5Sriastradh bias = 0.0f; 2443464ebd5Sriastradh } 2453464ebd5Sriastradh 2463464ebd5Sriastradh p->viewport.scale[0] = half_width; 2473464ebd5Sriastradh p->viewport.scale[1] = half_height * scale; 2483464ebd5Sriastradh p->viewport.scale[2] = half_depth; 2493464ebd5Sriastradh 2503464ebd5Sriastradh p->viewport.translate[0] = half_width + x; 2513464ebd5Sriastradh p->viewport.translate[1] = (half_height + y) * scale + bias; 2523464ebd5Sriastradh p->viewport.translate[2] = half_depth + z; 2537ec681f3Smrg 2547ec681f3Smrg p->viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X; 2557ec681f3Smrg p->viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y; 2567ec681f3Smrg p->viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z; 2577ec681f3Smrg p->viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W; 2583464ebd5Sriastradh } 2593464ebd5Sriastradh 2603464ebd5Sriastradh /* vertex elements state */ 2617ec681f3Smrg memset(&p->velem, 0, sizeof(p->velem)); 2627ec681f3Smrg p->velem.count = 2; 2637ec681f3Smrg 2647ec681f3Smrg p->velem.velems[0].src_offset = 0 * 4 * sizeof(float); /* offset 0, first element */ 2657ec681f3Smrg p->velem.velems[0].instance_divisor = 0; 2667ec681f3Smrg p->velem.velems[0].vertex_buffer_index = 0; 2677ec681f3Smrg p->velem.velems[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 2683464ebd5Sriastradh 2697ec681f3Smrg p->velem.velems[1].src_offset = 1 * 4 * sizeof(float); /* offset 16, second element */ 2707ec681f3Smrg p->velem.velems[1].instance_divisor = 0; 2717ec681f3Smrg p->velem.velems[1].vertex_buffer_index = 0; 2727ec681f3Smrg p->velem.velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 2733464ebd5Sriastradh 2743464ebd5Sriastradh /* vertex shader */ 2753464ebd5Sriastradh { 27601e04c3fSmrg const enum tgsi_semantic semantic_names[] = 27701e04c3fSmrg { TGSI_SEMANTIC_POSITION, TGSI_SEMANTIC_GENERIC }; 2783464ebd5Sriastradh const uint semantic_indexes[] = { 0, 0 }; 27901e04c3fSmrg p->vs = util_make_vertex_passthrough_shader(p->pipe, 2, semantic_names, semantic_indexes, FALSE); 2803464ebd5Sriastradh } 2813464ebd5Sriastradh 2823464ebd5Sriastradh /* fragment shader */ 28301e04c3fSmrg p->fs = util_make_fragment_tex_shader(p->pipe, TGSI_TEXTURE_2D, 28401e04c3fSmrg TGSI_INTERPOLATE_LINEAR, 28501e04c3fSmrg TGSI_RETURN_TYPE_FLOAT, 28601e04c3fSmrg TGSI_RETURN_TYPE_FLOAT, false, 28701e04c3fSmrg false); 2883464ebd5Sriastradh} 2893464ebd5Sriastradh 2903464ebd5Sriastradhstatic void close_prog(struct program *p) 2913464ebd5Sriastradh{ 29201e04c3fSmrg cso_destroy_context(p->cso); 2933464ebd5Sriastradh 2943464ebd5Sriastradh p->pipe->delete_vs_state(p->pipe, p->vs); 2953464ebd5Sriastradh p->pipe->delete_fs_state(p->pipe, p->fs); 2963464ebd5Sriastradh 2973464ebd5Sriastradh pipe_surface_reference(&p->framebuffer.cbufs[0], NULL); 2983464ebd5Sriastradh pipe_sampler_view_reference(&p->view, NULL); 2993464ebd5Sriastradh pipe_resource_reference(&p->target, NULL); 3003464ebd5Sriastradh pipe_resource_reference(&p->tex, NULL); 3013464ebd5Sriastradh pipe_resource_reference(&p->vbuf, NULL); 3023464ebd5Sriastradh 3033464ebd5Sriastradh p->pipe->destroy(p->pipe); 3043464ebd5Sriastradh p->screen->destroy(p->screen); 305af69d88dSmrg pipe_loader_release(&p->dev, 1); 3063464ebd5Sriastradh 3073464ebd5Sriastradh FREE(p); 3083464ebd5Sriastradh} 3093464ebd5Sriastradh 3103464ebd5Sriastradhstatic void draw(struct program *p) 3113464ebd5Sriastradh{ 31201e04c3fSmrg const struct pipe_sampler_state *samplers[] = {&p->sampler}; 31301e04c3fSmrg 3143464ebd5Sriastradh /* set the render target */ 3153464ebd5Sriastradh cso_set_framebuffer(p->cso, &p->framebuffer); 3163464ebd5Sriastradh 3173464ebd5Sriastradh /* clear the render target */ 3187ec681f3Smrg p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, NULL, &p->clear_color, 0, 0); 3193464ebd5Sriastradh 3203464ebd5Sriastradh /* set misc state we care about */ 3213464ebd5Sriastradh cso_set_blend(p->cso, &p->blend); 3223464ebd5Sriastradh cso_set_depth_stencil_alpha(p->cso, &p->depthstencil); 3233464ebd5Sriastradh cso_set_rasterizer(p->cso, &p->rasterizer); 3243464ebd5Sriastradh cso_set_viewport(p->cso, &p->viewport); 3253464ebd5Sriastradh 3263464ebd5Sriastradh /* sampler */ 32701e04c3fSmrg cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 1, samplers); 3283464ebd5Sriastradh 3293464ebd5Sriastradh /* texture sampler view */ 3307ec681f3Smrg p->pipe->set_sampler_views(p->pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &p->view); 3313464ebd5Sriastradh 3323464ebd5Sriastradh /* shaders */ 3333464ebd5Sriastradh cso_set_fragment_shader_handle(p->cso, p->fs); 3343464ebd5Sriastradh cso_set_vertex_shader_handle(p->cso, p->vs); 3353464ebd5Sriastradh 3363464ebd5Sriastradh /* vertex element data */ 3377ec681f3Smrg cso_set_vertex_elements(p->cso, &p->velem); 3383464ebd5Sriastradh 3393464ebd5Sriastradh util_draw_vertex_buffer(p->pipe, p->cso, 340af69d88dSmrg p->vbuf, 0, 0, 3413464ebd5Sriastradh PIPE_PRIM_QUADS, 3423464ebd5Sriastradh 4, /* verts */ 3433464ebd5Sriastradh 2); /* attribs/vert */ 3443464ebd5Sriastradh 345af69d88dSmrg p->pipe->flush(p->pipe, NULL, 0); 3463464ebd5Sriastradh 3473464ebd5Sriastradh debug_dump_surface_bmp(p->pipe, "result.bmp", p->framebuffer.cbufs[0]); 3483464ebd5Sriastradh} 3493464ebd5Sriastradh 3503464ebd5Sriastradhint main(int argc, char** argv) 3513464ebd5Sriastradh{ 3523464ebd5Sriastradh struct program *p = CALLOC_STRUCT(program); 3533464ebd5Sriastradh 3543464ebd5Sriastradh init_prog(p); 3553464ebd5Sriastradh draw(p); 3563464ebd5Sriastradh close_prog(p); 3573464ebd5Sriastradh 3583464ebd5Sriastradh return 0; 3593464ebd5Sriastradh} 360