Add more structure constructor tests.
[piglit/hramrach.git] / tests / shaders / fp-abs-01.c
blob3f8f85e7473396d8e6902dcd4d7760f738de8abd
1 /*
2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
24 /**
25 * \file fp-abs-01.c
26 * Validate the ABS instruction in GL_ARB_fragment_program
28 * \author Ian Romanick <ian.d.romanick@intel.com>
31 #include "piglit-util.h"
32 #include "piglit-framework.h"
34 #define TEST_ROWS 1
35 #define TEST_COLS 2
36 #define BOX_SIZE 32
38 #define ELEMENTS(x) (sizeof(x) / sizeof(x[0]))
40 int piglit_window_mode = GLUT_DOUBLE;
41 int piglit_width = (((BOX_SIZE+1)*TEST_COLS)+1);
42 int piglit_height = (((BOX_SIZE+1)*TEST_ROWS)+1);
46 static const char cos_shader_source[] =
47 "!!ARBfp1.0\n"
48 "ATTRIB input0 = fragment.texcoord[0];\n"
49 "ATTRIB input1 = fragment.texcoord[1];\n"
50 "TEMP R0, R1, R2;\n"
51 "\n"
52 "MOV R2, {0.0, 0.0, 0.0, 1.0};\n"
53 "\n"
54 "# Assume that input1.x is 1.0. COS(PI) is -1. This means\n"
55 "# that R2.y should end up with -1.0.\n"
56 "MUL R0, input1.x, 3.14159265358979323846;\n"
57 "COS R0, R0.x;\n"
58 "DP4 R2.y, R0, 0.25;\n"
59 "\n"
60 "ABS result.color, R2;\n"
61 "END\n"
64 static const char sge_shader_source[] =
65 "!!ARBfp1.0\n"
66 "ATTRIB input0 = fragment.texcoord[0];\n"
67 "ATTRIB input1 = fragment.texcoord[1];\n"
68 "TEMP R0, R1, R2;\n"
69 "\n"
70 "MOV R2, {0.0, 1.0, 0.0, 1.0};\n"
71 "\n"
72 "# By convention, all components of input0 are < 0.0, and\n"
73 "# input0 = -input1.\n"
74 "# The dot-product compacts the four components into a single\n"
75 "# component. R2.x should be 0.0.\n"
76 "ABS R1, input0;\n"
77 "ADD R0, -input1, R1;\n"
78 "SGE R1, R0, 1e-15;\n"
79 "SLT R0, R0, -1e-15;\n"
80 "DP4 R1, R1, 1.0;\n"
81 "DP4 R0, R0, 1.0;\n"
82 "ADD R2.x, R1, R2;\n"
83 "\n"
84 "# If R2.x is not 0.0 as it should be, set R2.y != 1.0\n"
85 "DP3 R1, R2.xxxx, 1.0;\n"
86 "SUB R2.y, R2, R1;\n"
87 "\n"
88 "MOV result.color, R2;\n"
89 "END\n"
93 static const char vert_shader_source[] =
94 "!!ARBvp1.0\n"
95 "ATTRIB iPos = vertex.position;\n"
96 "OUTPUT oPos = result.position;\n"
97 "PARAM mvp[4] = { state.matrix.mvp };\n"
98 "DP4 oPos.x, mvp[0], iPos;\n"
99 "DP4 oPos.y, mvp[1], iPos;\n"
100 "DP4 oPos.z, mvp[2], iPos;\n"
101 "DP4 oPos.w, mvp[3], iPos;\n"
102 "MOV result.texcoord[0], -vertex.color;\n"
103 "MOV result.texcoord[1], vertex.color;\n"
104 "END"
108 * \name Handles to fragment programs.
110 /*@{*/
111 static GLint progs[TEST_COLS];
112 static GLint vert_prog;
113 /*@}*/
116 enum piglit_result
117 piglit_display(void)
119 static const GLfloat color[4] = { 0.0, 1.0, 0.0, 0.0 };
120 enum piglit_result result = PIGLIT_SUCCESS;
121 unsigned i;
123 glClear(GL_COLOR_BUFFER_BIT);
124 glEnable(GL_FRAGMENT_PROGRAM_ARB);
125 glEnable(GL_VERTEX_PROGRAM_ARB);
127 glColor4f(1.0, 0.6, 0.3, 0.1);
129 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vert_prog);
131 for (i = 0; i < ELEMENTS(progs); i++) {
132 const int x = 1 + (i * (BOX_SIZE + 1));
134 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, progs[i]);
136 piglit_draw_rect(x, 1, BOX_SIZE, BOX_SIZE);
138 if (!piglit_probe_pixel_rgb(x + (BOX_SIZE / 2),
139 1 + (BOX_SIZE / 2),
140 color)) {
141 result = PIGLIT_FAILURE;
145 glutSwapBuffers();
146 return result;
150 void
151 piglit_init(int argc, char **argv)
153 (void) argc;
154 (void) argv;
156 piglit_require_vertex_program();
157 piglit_require_fragment_program();
158 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
160 progs[0] = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB,
161 cos_shader_source);
162 progs[1] = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB,
163 sge_shader_source);
165 vert_prog = piglit_compile_program(GL_VERTEX_PROGRAM_ARB,
166 vert_shader_source);
168 glClearColor(0.5, 0.5, 0.5, 1.0);