<div dir="ltr">On 25 October 2013 16:49, Anuj Phogat <span dir="ltr"><<a href="mailto:anuj.phogat@gmail.com" target="_blank">anuj.phogat@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">V2: Use better technique to verify sample positions in fragment shader.<br>
    Get rid of redundant projection matrix.<br>
Signed-off-by: Anuj Phogat <<a href="mailto:anuj.phogat@gmail.com">anuj.phogat@gmail.com</a>><br>
---<br>
 tests/all.tests                                    |   5 +<br>
 .../arb_sample_shading/execution/CMakeLists.gl.txt |   1 +<br>
 .../execution/builtin-gl-sample-position.cpp       | 231 +++++++++++++++++++++<br>
 3 files changed, 237 insertions(+)<br>
 create mode 100644 tests/spec/arb_sample_shading/execution/builtin-gl-sample-position.cpp<br>
<br>
diff --git a/tests/all.tests b/tests/all.tests<br>
index c5dd80c..9df5a94 100644<br>
--- a/tests/all.tests<br>
+++ b/tests/all.tests<br>
@@ -1352,6 +1352,11 @@ for num_samples in TEST_SAMPLE_COUNTS:<br>
     executable = 'arb_sample_shading-{0} -auto'.format(test_name)<br>
     arb_sample_shading[test_name] = PlainExecTest(executable)<br>
<br>
+for num_samples in TEST_SAMPLE_COUNTS:<br>
+    test_name = 'builtin-gl-sample-position {0}'.format(num_samples)<br>
+    executable = 'arb_sample_shading-{0} -auto'.format(test_name)<br>
+    arb_sample_shading[test_name] = PlainExecTest(executable)<br>
+<br>
 # Group ARB_debug_output<br>
 arb_debug_output = Group()<br>
 spec['ARB_debug_output'] = arb_debug_output<br>
diff --git a/tests/spec/arb_sample_shading/execution/CMakeLists.gl.txt b/tests/spec/arb_sample_shading/execution/CMakeLists.gl.txt<br>
index d2f1f4a..a832189 100644<br>
--- a/tests/spec/arb_sample_shading/execution/CMakeLists.gl.txt<br>
+++ b/tests/spec/arb_sample_shading/execution/CMakeLists.gl.txt<br>
@@ -14,4 +14,5 @@ piglit_add_executable (arb_sample_shading-api api.c)<br>
 piglit_add_executable (arb_sample_shading-builtin-gl-num-samples builtin-gl-num-samples.cpp)<br>
 piglit_add_executable (arb_sample_shading-builtin-gl-sample-id builtin-gl-sample-id.cpp)<br>
 piglit_add_executable (arb_sample_shading-builtin-gl-sample-mask builtin-gl-sample-mask.cpp)<br>
+piglit_add_executable (arb_sample_shading-builtin-gl-sample-position builtin-gl-sample-position.cpp)<br>
 # vim: ft=cmake:<br>
diff --git a/tests/spec/arb_sample_shading/execution/builtin-gl-sample-position.cpp b/tests/spec/arb_sample_shading/execution/builtin-gl-sample-position.cpp<br>
new file mode 100644<br>
index 0000000..17ca45a<br>
--- /dev/null<br>
+++ b/tests/spec/arb_sample_shading/execution/builtin-gl-sample-position.cpp<br>
@@ -0,0 +1,231 @@<br>
+/*<br>
+ * Copyright © 2013 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+/** \file builtin-gl-sample-position.cpp<br>
+ *  This test verifies that using gl_SamplePosition in fragment shader<br>
+ *  program works as per ARB_sample_shading specification.<br>
+ *<br>
+ **/<br>
+<br>
+#include "piglit-fbo.h"<br>
+using namespace piglit_util_fbo;<br>
+<br>
+const int pattern_width = 128; const int pattern_height = 128;<br>
+<br>
+PIGLIT_GL_TEST_CONFIG_BEGIN<br>
+<br>
+       config.supports_gl_compat_version = 10;<br>
+<br>
+       config.window_width = pattern_width;<br>
+       config.window_height = pattern_height;<br>
+       config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;<br>
+<br>
+PIGLIT_GL_TEST_CONFIG_END<br>
+<br>
+static int  num_samples;<br>
+static unsigned prog, vao, vertex_buf;<br>
+static Fbo multisampled_fbo, singlesampled_fbo;<br>
+<br>
+static void<br>
+print_usage_and_exit(char *prog_name)<br>
+{<br>
+       printf("Usage: %s <num_samples>\n", prog_name);<br>
+       piglit_report_result(PIGLIT_FAIL);<br>
+}<br>
+<br>
+void<br>
+compile_shader(void)<br>
+{<br>
+       static const char *vert =<br>
+               "#version 130\n"<br>
+               "in vec2 pos;\n"<br>
+               "void main()\n"<br>
+               "{\n"<br>
+               "  gl_Position = vec4(pos, 0.0, 1.0);\n"<br>
+               "}\n";<br>
+       static const char *frag =<br>
+               "#version 130\n"<br>
+               "#extension GL_ARB_sample_shading : enable\n"<br>
+               "uniform int samples;\n"<br>
+               "uniform vec2 expected_pos[8];\n"<br></blockquote><div><br></div><div>This hardcoded 8 will prevent the test from working if num_samples > 8.  Since you've set up all.tests to test up to num_samples = 32, that's a problem.<br>
<br>I'd recommend adding<br><br></div><div>#define MAX_NUM_SAMPLES 32<br></div><div>#define MAX_NUM_SAMPLES_STR "32"<br></div><div><br>to the top of the file.  Then, here in the shader, you can do:<br><br></div>
<div>"uniform vec2 expected_pos[" MAX_NUM_SAMPLES_STR "];\n"<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+               "out vec4 out_color;\n"<br>
+               "void main()\n"<br>
+               "{\n"<br>
+               "  if ((expected_pos[gl_SampleID] == gl_SamplePosition) ||\n"<br>
+               "      (samples == 0 && gl_SamplePosition == vec2(0.5, 0.5)))\n"<br>
+               "    out_color = vec4(0.0, 1.0, 0.0, 1.0);\n"<br>
+               "  else\n"<br>
+               "    out_color = vec4(1.0, 0.0, 0.0, 1.0);\n"<br>
+               "}\n";<br>
+       /* Compile program */<br>
+       prog = glCreateProgram();<br>
+       GLint vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vert);<br>
+       glAttachShader(prog, vs);<br>
+       piglit_check_gl_error(GL_NO_ERROR);<br>
+       GLint fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, frag);<br>
+       glAttachShader(prog, fs);<br>
+       glBindAttribLocation(prog, 0, "pos");<br>
+       glLinkProgram(prog);<br>
+       if (!piglit_link_check_status(prog)) {<br>
+               piglit_report_result(PIGLIT_FAIL);<br>
+       }<br>
+<br>
+       /* Set up vertex array object */<br>
+       glGenVertexArrays(1, &vao);<br>
+       glBindVertexArray(vao);<br>
+<br>
+       /* Set up vertex input buffer */<br>
+       glGenBuffers(1, &vertex_buf);<br>
+       glBindBuffer(GL_ARRAY_BUFFER, vertex_buf);<br>
+       glEnableVertexAttribArray(0);<br>
+       glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float),<br>
+                             (void *) 0);<br>
+<br>
+       /* Set up element input buffer to tesselate a quad into<br>
+        * triangles<br>
+        */<br>
+       unsigned int indices[6] = { 0, 1, 2, 0, 2, 3 };<br>
+       GLuint element_buf;<br>
+       glGenBuffers(1, &element_buf);<br>
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buf);<br>
+       glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,<br>
+                    GL_STATIC_DRAW);<br>
+}<br>
+<br>
+void<br>
+draw_pattern(GLint samples, GLfloat* sample_pos)<br>
+{<br>
+       float vertex_data[4][4] = {<br>
+               { -1, -1 },<br>
+               { -1,  1 },<br>
+               {  1,  1 },<br>
+               {  1, -1 }};<br>
+<br>
+       glUseProgram(prog);<br>
+       glBindVertexArray(vao);<br>
+<br>
+       /* Set up uniforms */<br>
+       glUseProgram(prog);<br>
+       glUniform1i(glGetUniformLocation(prog, "samples"), samples);<br>
+       glUniform2fv(glGetUniformLocation(prog, "expected_pos"), 8, sample_pos);<br></blockquote><div><br></div><div>This hardcoded 8 should be changed to MAX_NUM_SAMPLES.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+<br>
+       glBindBuffer(GL_ARRAY_BUFFER, vertex_buf);<br>
+       glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data,<br>
+                    GL_STREAM_DRAW);<br>
+       glDrawElements(GL_TRIANGLES, 6 , GL_UNSIGNED_INT, (void *) 0);<br></blockquote><div><br></div><div>As with the other patches, this is going to a lot of duplicate work to draw a rectangle.  Just use piglit_draw_rect().  <br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+}<br>
+<br>
+void<br>
+piglit_init(int argc, char **argv)<br>
+{<br>
+       if (argc != 2)<br>
+               print_usage_and_exit(argv[0]);<br>
+<br>
+       /* 1st arg: num_samples */<br>
+       char *endptr = NULL;<br>
+       num_samples = strtol(argv[1], &endptr, 0);<br>
+       if (endptr != argv[1] + strlen(argv[1]))<br>
+               print_usage_and_exit(argv[0]);<br>
+<br>
+       piglit_require_gl_version(21);<br>
+       piglit_require_extension("GL_ARB_vertex_array_object");<br>
+       piglit_require_extension("GL_ARB_sample_shading");<br>
+<br>
+       /* Skip the test if num_samples > GL_MAX_SAMPLES */<br>
+       GLint max_samples;<br>
+       glGetIntegerv(GL_MAX_SAMPLES, &max_samples);<br>
+       if (num_samples > max_samples)<br>
+               piglit_report_result(PIGLIT_SKIP);<br></blockquote><div><br></div><div>Since the correctness of the test relies on num_samples being no larger than MAX_NUM_SAMPLES, I'd recommend adding an error check here to check that num_samples <= MAX_NUM_SAMPLES, and otherwise report PIGLIT_FAIL.<br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+       singlesampled_fbo.setup(FboConfig(0,<br>
+                                         pattern_width,<br>
+                                         pattern_height));<br>
+<br>
+       FboConfig msConfig(num_samples, pattern_width, pattern_height);<br>
+       multisampled_fbo.setup(msConfig);<br>
+<br>
+       compile_shader();<br>
+       if (!piglit_check_gl_error(GL_NO_ERROR)) {<br>
+               piglit_report_result(PIGLIT_FAIL);<br>
+       }<br>
+}<br>
+<br>
+bool test_ms_blit_scaled(Fbo ms_fbo)<br>
+{<br>
+       GLint samples;<br>
+       GLfloat sample_pos[16];<br></blockquote><div><br></div><div>Array size should be MAX_NUM_SAMPLES * 2<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+        GLfloat expected[4] = {0.0, 1.0, 0.0, 1.0};<br>
+       bool pass = true;<br>
+<br>
+       glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ms_fbo.handle);<br>
+       glGetIntegerv(GL_SAMPLES, &samples);<br>
+<br>
+       /* Expected color is computed after taking in to account multisample<br>
+        * color resolve:<br>
+        * ((sample_pos0 + sample_pos1 + sample_pos2 + sample_pos3) / 4.0)<br>
+        */<br>
+        for(int i = 0; i < samples; i++) {<br>
+               glGetMultisamplefv(GL_SAMPLE_POSITION, i, &(sample_pos[2 * i]));<br>
+               printf("sample[%d]_pos = {%f, %f}\n",<br>
+                       i, sample_pos[2 * i], sample_pos[2 * i + 1]);<br>
+       }<br>
+<br>
+       glClear(GL_COLOR_BUFFER_BIT);<br>
+       draw_pattern(samples, sample_pos);<br>
+<br>
+       glBindFramebuffer(GL_READ_FRAMEBUFFER, ms_fbo.handle);<br>
+       glBindFramebuffer(GL_DRAW_FRAMEBUFFER, singlesampled_fbo.handle);<br>
+       glClear(GL_COLOR_BUFFER_BIT);<br>
+       glBlitFramebuffer(0, 0,<br>
+                         pattern_width, pattern_height,<br>
+                         0, 0,<br>
+                         pattern_width, pattern_height,<br>
+                         GL_COLOR_BUFFER_BIT,<br>
+                         GL_NEAREST);<br>
+<br>
+       glBindFramebuffer(GL_READ_FRAMEBUFFER, singlesampled_fbo.handle);<br>
+       glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);<br>
+<br>
+       pass = piglit_probe_rect_rgba(0, 0, pattern_width,<br>
+                                      pattern_width, expected)<br>
+               && pass;<br>
+       glClear(GL_COLOR_BUFFER_BIT);<br>
+       glBlitFramebuffer(0, 0,<br>
+                         pattern_width, pattern_height,<br>
+                         0, 0,<br>
+                         pattern_width, pattern_height,<br>
+                         GL_COLOR_BUFFER_BIT,<br>
+                         GL_NEAREST);<br>
+<br>
+       piglit_present_results();<br>
+       return pass;<br>
+}<br>
+<br>
+enum piglit_result<br>
+piglit_display()<br>
+{<br>
+       bool pass = true;<br>
+       pass = test_ms_blit_scaled(multisampled_fbo)<br>
+               && pass;<br>
+       return pass ? PIGLIT_PASS : PIGLIT_FAIL;<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.1.4</font></span></blockquote><div><br></div><div>With those changes, the patch is:<br><br>Reviewed-by: Paul Berry <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>> <br></div></div></div></div>