arb_framebuffer_object: add missing MSAA alpha-to-coverage and alpha-to-one tests
[piglit.git] / tests / spec / ext_image_dma_buf_import / sample_common.c
blob7f3ac767d5243fb90061214af1d04daa3815112c
1 /*
2 * Copyright © 2013 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 #include <unistd.h>
26 #include "piglit-framework-gl/piglit_drm_dma_buf.h"
28 #include "image_common.h"
29 #include "sample_common.h"
31 static const char fs_src_external[] =
32 "#extension GL_OES_EGL_image_external : require\n"
33 "precision mediump float;\n"
34 "uniform samplerExternalOES sampler;\n"
35 "varying vec2 texcoords;\n"
36 "\n"
37 "void main()\n"
38 "{\n"
39 "gl_FragColor = texture2D(sampler, texcoords);\n"
40 "}\n";
41 static const char fs_src[] =
42 "precision mediump float;\n"
43 "uniform sampler2D sampler;\n"
44 "varying vec2 texcoords;\n"
45 "\n"
46 "void main()\n"
47 "{\n"
48 "gl_FragColor = texture2D(sampler, texcoords);\n"
49 "}\n";
50 static const char vs_src[] =
51 "attribute vec4 piglit_vertex;\n"
52 "attribute vec4 piglit_texcoords;\n"
53 "varying vec2 texcoords;\n"
54 "\n"
55 "void main()\n"
56 "{\n"
57 " texcoords = piglit_texcoords.xy;\n"
58 " gl_Position = piglit_vertex;\n"
59 "}\n";
61 enum piglit_result
62 texture_for_egl_image(EGLImageKHR img, GLuint *out_tex, bool external)
64 GLuint tex;
65 GLenum error;
66 GLenum target = external ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
68 glGenTextures(1, &tex);
69 glBindTexture(target, tex);
71 /* Set the image as level zero */
72 glEGLImageTargetTexStorageEXT(target, (GLeglImageOES)img, NULL);
73 error = glGetError();
75 /**
76 * EGL may not support binding of external textures, this is not an
77 * error.
79 if (error == GL_INVALID_OPERATION)
80 return PIGLIT_SKIP;
82 if (error != GL_NO_ERROR) {
83 printf("glEGLImageTargetTexStorageEXT() failed: %s 0x%x\n",
84 piglit_get_gl_error_name(error), error);
85 return PIGLIT_FAIL;
88 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
89 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
91 *out_tex = tex;
93 return PIGLIT_PASS;
96 void
97 sample_tex(GLuint tex, unsigned x, unsigned y, unsigned w, unsigned h,
98 bool external)
100 GLuint prog;
102 prog = piglit_build_simple_program(vs_src, external ?
103 fs_src_external : fs_src);
105 glUseProgram(prog);
107 glBindTexture(external ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D, tex);
108 glUniform1i(glGetUniformLocation(prog, "sampler"), 0);
110 glViewport(x, y, w, h);
111 piglit_draw_rect_tex(-1, -1, 2, 2,
112 0, 0, 1, 1);
114 glDeleteProgram(prog);
115 glUseProgram(0);
118 enum piglit_result
119 egl_image_for_dma_buf_fd(struct piglit_dma_buf *buf, int fd, int fourcc, EGLImageKHR *out_img)
121 EGLint error;
122 EGLImageKHR img;
123 EGLint attr_packed[] = {
124 EGL_WIDTH, buf->w,
125 EGL_HEIGHT, buf->h,
126 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
127 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
128 EGL_DMA_BUF_PLANE0_OFFSET_EXT, buf->offset[0],
129 EGL_DMA_BUF_PLANE0_PITCH_EXT, buf->stride[0],
130 EGL_NONE
133 EGLint attr_nv12[] = {
134 EGL_WIDTH, buf->w,
135 EGL_HEIGHT, buf->h,
136 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
137 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
138 EGL_DMA_BUF_PLANE0_OFFSET_EXT, buf->offset[0],
139 EGL_DMA_BUF_PLANE0_PITCH_EXT, buf->stride[0],
140 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
141 EGL_DMA_BUF_PLANE1_OFFSET_EXT, buf->offset[1],
142 EGL_DMA_BUF_PLANE1_PITCH_EXT, buf->stride[1],
143 EGL_NONE
146 EGLint attr_yuv420[] = {
147 EGL_WIDTH, buf->w,
148 EGL_HEIGHT, buf->h,
149 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
150 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
151 EGL_DMA_BUF_PLANE0_OFFSET_EXT, buf->offset[0],
152 EGL_DMA_BUF_PLANE0_PITCH_EXT, buf->stride[0],
153 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
154 EGL_DMA_BUF_PLANE1_OFFSET_EXT, buf->offset[1],
155 EGL_DMA_BUF_PLANE1_PITCH_EXT, buf->stride[1],
156 EGL_DMA_BUF_PLANE2_FD_EXT, fd,
157 EGL_DMA_BUF_PLANE2_OFFSET_EXT, buf->offset[2],
158 EGL_DMA_BUF_PLANE2_PITCH_EXT, buf->stride[2],
159 EGL_NONE
162 EGLint *attr;
163 switch (fourcc) {
164 case DRM_FORMAT_NV12:
165 case DRM_FORMAT_NV21:
166 case DRM_FORMAT_P010:
167 case DRM_FORMAT_P012:
168 case DRM_FORMAT_P016:
169 attr = attr_nv12;
170 break;
171 case DRM_FORMAT_YUV420:
172 case DRM_FORMAT_YVU420:
173 attr = attr_yuv420;
174 break;
175 default:
176 attr = attr_packed;
177 break;
180 img = eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT,
181 EGL_LINUX_DMA_BUF_EXT, (EGLClientBuffer)0,
182 attr);
184 error = eglGetError();
186 /* EGL may not support the format, this is not an error. */
187 if (!img && error == EGL_BAD_MATCH)
188 return PIGLIT_SKIP;
190 if (error != EGL_SUCCESS) {
191 printf("eglCreateImageKHR() failed: %s 0x%x\n",
192 piglit_get_egl_error_name(error), error);
194 return PIGLIT_FAIL;
197 if (!img) {
198 fprintf(stderr, "image creation succeeded but returned NULL\n");
199 return PIGLIT_FAIL;
202 *out_img = img;
203 return PIGLIT_PASS;
206 static enum piglit_result
207 sample_buffer(struct piglit_dma_buf *buf, int fourcc)
209 enum piglit_result res;
210 EGLImageKHR img;
211 GLuint tex;
212 int w = buf->w;
213 int h = buf->h;
215 res = egl_image_for_dma_buf_fd(buf, buf->fd, fourcc, &img);
217 /* Release the creator side of the buffer. */
218 piglit_destroy_dma_buf(buf);
220 if (!img) {
221 /* Close the descriptor also, EGL does not have ownership */
222 close(buf->fd);
225 if (res != PIGLIT_PASS)
226 return res;
228 res = texture_for_egl_image(img, &tex, true);
229 if (res != PIGLIT_PASS)
230 goto destroy;
232 sample_tex(tex, 0, 0, w, h, true);
234 destroy:
235 glDeleteTextures(1, &tex);
236 eglDestroyImageKHR(eglGetCurrentDisplay(), img);
238 return res;
241 enum piglit_result
242 dma_buf_create_and_sample_32bpp(unsigned w, unsigned h,
243 int fourcc, const unsigned char *src)
245 struct piglit_dma_buf *buf;
246 enum piglit_result res;
248 res = piglit_create_dma_buf(w, h, fourcc, src, &buf);
249 if (res != PIGLIT_PASS)
250 return res;
252 return sample_buffer(buf, fourcc);
255 void
256 usage(const char *name, const char *color_space)
258 fprintf(stderr,
259 "Usage:\n"
260 " %s -fmt=<format>\n"
261 "\n"
262 "Where <format> is a fourCC code for a %s format.\n",
263 name, color_space);