Path: blob/21.2-virgl/src/gallium/tests/graw/tri-gs.c
4565 views
/* Display a cleared blue window. This demo has no dependencies on1* any utility code, just the graw interface and gallium.2*/34#include <stdio.h>5#include "frontend/graw.h"6#include "pipe/p_screen.h"7#include "pipe/p_context.h"8#include "pipe/p_state.h"9#include "pipe/p_defines.h"1011#include "util/u_memory.h" /* Offset() */12#include "util/u_draw_quad.h"13#include "util/u_inlines.h"1415enum pipe_format formats[] = {16PIPE_FORMAT_RGBA8888_UNORM,17PIPE_FORMAT_BGRA8888_UNORM,18PIPE_FORMAT_NONE19};2021static const int WIDTH = 300;22static const int HEIGHT = 300;2324static struct pipe_screen *screen = NULL;25static struct pipe_context *ctx = NULL;26static struct pipe_surface *surf = NULL;27static struct pipe_resource *tex = NULL;28static void *window = NULL;2930struct vertex {31float position[4];32float color[4];33};3435static struct vertex vertices[4] =36{37{ { 0.0f, -0.9f, 0.0f, 1.0f },38{ 1.0f, 0.0f, 0.0f, 1.0f }39},40{ { -0.9f, 0.9f, 0.0f, 1.0f },41{ 0.0f, 1.0f, 0.0f, 1.0f }42},43{ { 0.9f, 0.9f, 0.0f, 1.0f },44{ 0.0f, 0.0f, 1.0f, 1.0f }45}46};4748495051static void set_viewport( float x, float y,52float width, float height,53float zNear, float zFar)54{55float z = zFar;56float half_width = (float)width / 2.0f;57float half_height = (float)height / 2.0f;58float half_depth = ((float)zFar - (float)zNear) / 2.0f;59struct pipe_viewport_state vp;6061vp.scale[0] = half_width;62vp.scale[1] = half_height;63vp.scale[2] = half_depth;6465vp.translate[0] = half_width + x;66vp.translate[1] = half_height + y;67vp.translate[2] = half_depth + z;6869vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;70vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;71vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;72vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;7374ctx->set_viewport_states( ctx, 0, 1, &vp );75}7677static void set_vertices( void )78{79struct pipe_vertex_element ve[2];80struct pipe_vertex_buffer vbuf;81void *handle;8283memset(ve, 0, sizeof ve);8485ve[0].src_offset = Offset(struct vertex, position);86ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;87ve[1].src_offset = Offset(struct vertex, color);88ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;8990handle = ctx->create_vertex_elements_state(ctx, 2, ve);91ctx->bind_vertex_elements_state(ctx, handle);9293memset(&vbuf, 0, sizeof vbuf);9495vbuf.stride = sizeof( struct vertex );96vbuf.buffer_offset = 0;97vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,98PIPE_BIND_VERTEX_BUFFER,99PIPE_USAGE_DEFAULT,100sizeof(vertices),101vertices);102103ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf);104}105106static void set_vertex_shader( void )107{108void *handle;109const char *text =110"VERT\n"111"DCL IN[0]\n"112"DCL IN[1]\n"113"DCL OUT[0], POSITION\n"114"DCL OUT[1], COLOR\n"115" 0: MOV OUT[1], IN[1]\n"116" 1: MOV OUT[0], IN[0]\n"117" 2: END\n";118119handle = graw_parse_vertex_shader(ctx, text);120ctx->bind_vs_state(ctx, handle);121}122123static void set_fragment_shader( void )124{125void *handle;126const char *text =127"FRAG\n"128"DCL IN[0], COLOR, LINEAR\n"129"DCL OUT[0], COLOR\n"130" 0: MOV OUT[0], IN[0]\n"131" 1: END\n";132133handle = graw_parse_fragment_shader(ctx, text);134ctx->bind_fs_state(ctx, handle);135}136137138static void set_geometry_shader( void )139{140void *handle;141const char *text =142"GEOM\n"143"PROPERTY GS_INPUT_PRIMITIVE TRIANGLES\n"144"PROPERTY GS_OUTPUT_PRIMITIVE TRIANGLE_STRIP\n"145"DCL IN[][0], POSITION, CONSTANT\n"146"DCL IN[][1], COLOR, CONSTANT\n"147"DCL OUT[0], POSITION, CONSTANT\n"148"DCL OUT[1], COLOR, CONSTANT\n"149"0:MOV OUT[0], IN[0][0]\n"150"1:MOV OUT[1], IN[0][1]\n"151"2:EMIT\n"152"3:MOV OUT[0], IN[1][0]\n"153"4:MOV OUT[1], IN[0][1]\n" /* copy color from input vertex 0 */154"5:EMIT\n"155"6:MOV OUT[0], IN[2][0]\n"156"7:MOV OUT[1], IN[2][1]\n"157"8:EMIT\n"158"9:ENDPRIM\n"159"10:END\n";160161handle = graw_parse_geometry_shader(ctx, text);162ctx->bind_gs_state(ctx, handle);163}164165static void draw( void )166{167union pipe_color_union clear_color = { {1,0,1,1} };168169ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);170util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);171ctx->flush(ctx, NULL, 0);172173screen->flush_frontbuffer(screen, ctx, tex, 0, 0, window, NULL);174}175176177static void init( void )178{179struct pipe_framebuffer_state fb;180struct pipe_resource templat;181struct pipe_surface surf_tmpl;182int i;183184/* It's hard to say whether window or screen should be created185* first. Different environments would prefer one or the other.186*187* Also, no easy way of querying supported formats if the screen188* cannot be created first.189*/190for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {191screen = graw_create_window_and_screen(0, 0, 300, 300,192formats[i],193&window);194if (window && screen)195break;196}197if (!screen || !window) {198fprintf(stderr, "Unable to create window\n");199exit(1);200}201202ctx = screen->context_create(screen, NULL, 0);203if (ctx == NULL)204exit(3);205206memset(&templat, 0, sizeof(templat));207templat.target = PIPE_TEXTURE_2D;208templat.format = formats[i];209templat.width0 = WIDTH;210templat.height0 = HEIGHT;211templat.depth0 = 1;212templat.array_size = 1;213templat.last_level = 0;214templat.bind = (PIPE_BIND_RENDER_TARGET |215PIPE_BIND_DISPLAY_TARGET);216217tex = screen->resource_create(screen,218&templat);219if (tex == NULL)220exit(4);221222surf_tmpl.format = templat.format;223surf_tmpl.u.tex.level = 0;224surf_tmpl.u.tex.first_layer = 0;225surf_tmpl.u.tex.last_layer = 0;226surf = ctx->create_surface(ctx, tex, &surf_tmpl);227if (surf == NULL)228exit(5);229230memset(&fb, 0, sizeof fb);231fb.nr_cbufs = 1;232fb.width = WIDTH;233fb.height = HEIGHT;234fb.cbufs[0] = surf;235236ctx->set_framebuffer_state(ctx, &fb);237238{239struct pipe_blend_state blend;240void *handle;241memset(&blend, 0, sizeof blend);242blend.rt[0].colormask = PIPE_MASK_RGBA;243handle = ctx->create_blend_state(ctx, &blend);244ctx->bind_blend_state(ctx, handle);245}246247{248struct pipe_depth_stencil_alpha_state depthstencil;249void *handle;250memset(&depthstencil, 0, sizeof depthstencil);251handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);252ctx->bind_depth_stencil_alpha_state(ctx, handle);253}254255{256struct pipe_rasterizer_state rasterizer;257void *handle;258memset(&rasterizer, 0, sizeof rasterizer);259rasterizer.cull_face = PIPE_FACE_NONE;260rasterizer.half_pixel_center = 1;261rasterizer.bottom_edge_rule = 1;262rasterizer.depth_clip_near = 1;263rasterizer.depth_clip_far = 1;264handle = ctx->create_rasterizer_state(ctx, &rasterizer);265ctx->bind_rasterizer_state(ctx, handle);266}267268set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);269set_vertices();270set_vertex_shader();271set_fragment_shader();272set_geometry_shader();273}274275276int main( int argc, char *argv[] )277{278init();279280graw_set_display_func( draw );281graw_main_loop();282return 0;283}284285286