Path: blob/21.2-virgl/src/gallium/tests/trivial/tri.c
4561 views
/**************************************************************************1*2* Copyright © 2010 Jakob Bornecrantz3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23**************************************************************************/242526#define USE_TRACE 027#define WIDTH 30028#define HEIGHT 30029#define NEAR 030#define FAR 131#define FLIP 03233/* pipe_*_state structs */34#include "pipe/p_state.h"35/* pipe_context */36#include "pipe/p_context.h"37/* pipe_screen */38#include "pipe/p_screen.h"39/* PIPE_* */40#include "pipe/p_defines.h"41/* TGSI_SEMANTIC_{POSITION|GENERIC} */42#include "pipe/p_shader_tokens.h"43/* pipe_buffer_* helpers */44#include "util/u_inlines.h"4546/* constant state object helper */47#include "cso_cache/cso_context.h"4849/* debug_dump_surface_bmp */50#include "util/u_debug_image.h"51/* util_draw_vertex_buffer helper */52#include "util/u_draw_quad.h"53/* FREE & CALLOC_STRUCT */54#include "util/u_memory.h"55/* util_make_[fragment|vertex]_passthrough_shader */56#include "util/u_simple_shaders.h"57/* to get a hardware pipe driver */58#include "pipe-loader/pipe_loader.h"5960struct program61{62struct pipe_loader_device *dev;63struct pipe_screen *screen;64struct pipe_context *pipe;65struct cso_context *cso;6667struct pipe_blend_state blend;68struct pipe_depth_stencil_alpha_state depthstencil;69struct pipe_rasterizer_state rasterizer;70struct pipe_viewport_state viewport;71struct pipe_framebuffer_state framebuffer;72struct cso_velems_state velem;7374void *vs;75void *fs;7677union pipe_color_union clear_color;7879struct pipe_resource *vbuf;80struct pipe_resource *target;81};8283static void init_prog(struct program *p)84{85struct pipe_surface surf_tmpl;86ASSERTED int ret;8788/* find a hardware device */89ret = pipe_loader_probe(&p->dev, 1);90assert(ret);9192/* init a pipe screen */93p->screen = pipe_loader_create_screen(p->dev);94assert(p->screen);9596/* create the pipe driver context and cso context */97p->pipe = p->screen->context_create(p->screen, NULL, 0);98p->cso = cso_create_context(p->pipe, 0);99100/* set clear color */101p->clear_color.f[0] = 0.3;102p->clear_color.f[1] = 0.1;103p->clear_color.f[2] = 0.3;104p->clear_color.f[3] = 1.0;105106/* vertex buffer */107{108float vertices[4][2][4] = {109{110{ 0.0f, -0.9f, 0.0f, 1.0f },111{ 1.0f, 0.0f, 0.0f, 1.0f }112},113{114{ -0.9f, 0.9f, 0.0f, 1.0f },115{ 0.0f, 1.0f, 0.0f, 1.0f }116},117{118{ 0.9f, 0.9f, 0.0f, 1.0f },119{ 0.0f, 0.0f, 1.0f, 1.0f }120}121};122123p->vbuf = pipe_buffer_create(p->screen, PIPE_BIND_VERTEX_BUFFER,124PIPE_USAGE_DEFAULT, sizeof(vertices));125pipe_buffer_write(p->pipe, p->vbuf, 0, sizeof(vertices), vertices);126}127128/* render target texture */129{130struct pipe_resource tmplt;131memset(&tmplt, 0, sizeof(tmplt));132tmplt.target = PIPE_TEXTURE_2D;133tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */134tmplt.width0 = WIDTH;135tmplt.height0 = HEIGHT;136tmplt.depth0 = 1;137tmplt.array_size = 1;138tmplt.last_level = 0;139tmplt.bind = PIPE_BIND_RENDER_TARGET;140141p->target = p->screen->resource_create(p->screen, &tmplt);142}143144/* disabled blending/masking */145memset(&p->blend, 0, sizeof(p->blend));146p->blend.rt[0].colormask = PIPE_MASK_RGBA;147148/* no-op depth/stencil/alpha */149memset(&p->depthstencil, 0, sizeof(p->depthstencil));150151/* rasterizer */152memset(&p->rasterizer, 0, sizeof(p->rasterizer));153p->rasterizer.cull_face = PIPE_FACE_NONE;154p->rasterizer.half_pixel_center = 1;155p->rasterizer.bottom_edge_rule = 1;156p->rasterizer.depth_clip_near = 1;157p->rasterizer.depth_clip_far = 1;158159surf_tmpl.format = PIPE_FORMAT_B8G8R8A8_UNORM;160surf_tmpl.u.tex.level = 0;161surf_tmpl.u.tex.first_layer = 0;162surf_tmpl.u.tex.last_layer = 0;163/* drawing destination */164memset(&p->framebuffer, 0, sizeof(p->framebuffer));165p->framebuffer.width = WIDTH;166p->framebuffer.height = HEIGHT;167p->framebuffer.nr_cbufs = 1;168p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, p->target, &surf_tmpl);169170/* viewport, depth isn't really needed */171{172float x = 0;173float y = 0;174float z = NEAR;175float half_width = (float)WIDTH / 2.0f;176float half_height = (float)HEIGHT / 2.0f;177float half_depth = ((float)FAR - (float)NEAR) / 2.0f;178float scale, bias;179180if (FLIP) {181scale = -1.0f;182bias = (float)HEIGHT;183} else {184scale = 1.0f;185bias = 0.0f;186}187188p->viewport.scale[0] = half_width;189p->viewport.scale[1] = half_height * scale;190p->viewport.scale[2] = half_depth;191192p->viewport.translate[0] = half_width + x;193p->viewport.translate[1] = (half_height + y) * scale + bias;194p->viewport.translate[2] = half_depth + z;195196p->viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;197p->viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;198p->viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;199p->viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;200}201202/* vertex elements state */203memset(&p->velem, 0, sizeof(p->velem));204p->velem.count = 2;205206p->velem.velems[0].src_offset = 0 * 4 * sizeof(float); /* offset 0, first element */207p->velem.velems[0].instance_divisor = 0;208p->velem.velems[0].vertex_buffer_index = 0;209p->velem.velems[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;210211p->velem.velems[1].src_offset = 1 * 4 * sizeof(float); /* offset 16, second element */212p->velem.velems[1].instance_divisor = 0;213p->velem.velems[1].vertex_buffer_index = 0;214p->velem.velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;215216/* vertex shader */217{218const enum tgsi_semantic semantic_names[] =219{ TGSI_SEMANTIC_POSITION, TGSI_SEMANTIC_COLOR };220const uint semantic_indexes[] = { 0, 0 };221p->vs = util_make_vertex_passthrough_shader(p->pipe, 2, semantic_names, semantic_indexes, FALSE);222}223224/* fragment shader */225p->fs = util_make_fragment_passthrough_shader(p->pipe,226TGSI_SEMANTIC_COLOR, TGSI_INTERPOLATE_PERSPECTIVE, TRUE);227}228229static void close_prog(struct program *p)230{231cso_destroy_context(p->cso);232233p->pipe->delete_vs_state(p->pipe, p->vs);234p->pipe->delete_fs_state(p->pipe, p->fs);235236pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);237pipe_resource_reference(&p->target, NULL);238pipe_resource_reference(&p->vbuf, NULL);239240p->pipe->destroy(p->pipe);241p->screen->destroy(p->screen);242pipe_loader_release(&p->dev, 1);243244FREE(p);245}246247static void draw(struct program *p)248{249/* set the render target */250cso_set_framebuffer(p->cso, &p->framebuffer);251252/* clear the render target */253p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, NULL, &p->clear_color, 0, 0);254255/* set misc state we care about */256cso_set_blend(p->cso, &p->blend);257cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);258cso_set_rasterizer(p->cso, &p->rasterizer);259cso_set_viewport(p->cso, &p->viewport);260261/* shaders */262cso_set_fragment_shader_handle(p->cso, p->fs);263cso_set_vertex_shader_handle(p->cso, p->vs);264265/* vertex element data */266cso_set_vertex_elements(p->cso, &p->velem);267268util_draw_vertex_buffer(p->pipe, p->cso,269p->vbuf, 0, 0,270PIPE_PRIM_TRIANGLES,2713, /* verts */2722); /* attribs/vert */273274p->pipe->flush(p->pipe, NULL, 0);275276debug_dump_surface_bmp(p->pipe, "result.bmp", p->framebuffer.cbufs[0]);277}278279int main(int argc, char** argv)280{281struct program *p = CALLOC_STRUCT(program);282283init_prog(p);284draw(p);285close_prog(p);286287return 0;288}289290291