ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / ext_image_dma_buf_import / sample_common.c
blob249840f01f4d1827062936cfadb8f731589d01d1
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[] =
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 vs_src[] =
42 "attribute vec4 piglit_vertex;\n"
43 "attribute vec4 piglit_texcoords;\n"
44 "varying vec2 texcoords;\n"
45 "\n"
46 "void main()\n"
47 "{\n"
48 " texcoords = piglit_texcoords.xy;\n"
49 " gl_Position = piglit_vertex;\n"
50 "}\n";
52 enum piglit_result
53 texture_for_egl_image(EGLImageKHR img, GLuint *out_tex)
55 GLuint tex;
56 GLenum error;
58 glGenTextures(1, &tex);
59 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
61 /* Set the image as level zero */
62 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES,
63 (GLeglImageOES)img);
64 error = glGetError();
66 /**
67 * EGL may not support binding of external textures, this is not an
68 * error.
70 if (error == GL_INVALID_OPERATION)
71 return PIGLIT_SKIP;
73 if (error != GL_NO_ERROR) {
74 printf("glEGLImageTargetTexture2DOES() failed: %s 0x%x\n",
75 piglit_get_gl_error_name(error), error);
76 return PIGLIT_FAIL;
79 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
80 GL_NEAREST);
81 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
82 GL_NEAREST);
84 *out_tex = tex;
86 return PIGLIT_PASS;
89 void
90 sample_tex(GLuint tex, unsigned x, unsigned y, unsigned w, unsigned h)
92 GLuint prog;
94 prog = piglit_build_simple_program(vs_src, fs_src);
96 glUseProgram(prog);
98 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
99 glUniform1i(glGetUniformLocation(prog, "sampler"), 0);
101 glViewport(x, y, w, h);
102 piglit_draw_rect_tex(-1, -1, 2, 2,
103 0, 0, 1, 1);
105 glDeleteProgram(prog);
106 glUseProgram(0);
109 enum piglit_result
110 egl_image_for_dma_buf_fd(struct piglit_dma_buf *buf, int fd, int fourcc, EGLImageKHR *out_img)
112 EGLint error;
113 EGLImageKHR img;
114 EGLint attr_packed[] = {
115 EGL_WIDTH, buf->w,
116 EGL_HEIGHT, buf->h,
117 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
118 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
119 EGL_DMA_BUF_PLANE0_OFFSET_EXT, buf->offset[0],
120 EGL_DMA_BUF_PLANE0_PITCH_EXT, buf->stride[0],
121 EGL_NONE
124 EGLint attr_nv12[] = {
125 EGL_WIDTH, buf->w,
126 EGL_HEIGHT, buf->h,
127 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
128 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
129 EGL_DMA_BUF_PLANE0_OFFSET_EXT, buf->offset[0],
130 EGL_DMA_BUF_PLANE0_PITCH_EXT, buf->stride[0],
131 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
132 EGL_DMA_BUF_PLANE1_OFFSET_EXT, buf->offset[1],
133 EGL_DMA_BUF_PLANE1_PITCH_EXT, buf->stride[1],
134 EGL_NONE
137 EGLint attr_yuv420[] = {
138 EGL_WIDTH, buf->w,
139 EGL_HEIGHT, buf->h,
140 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
141 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
142 EGL_DMA_BUF_PLANE0_OFFSET_EXT, buf->offset[0],
143 EGL_DMA_BUF_PLANE0_PITCH_EXT, buf->stride[0],
144 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
145 EGL_DMA_BUF_PLANE1_OFFSET_EXT, buf->offset[1],
146 EGL_DMA_BUF_PLANE1_PITCH_EXT, buf->stride[1],
147 EGL_DMA_BUF_PLANE2_FD_EXT, fd,
148 EGL_DMA_BUF_PLANE2_OFFSET_EXT, buf->offset[2],
149 EGL_DMA_BUF_PLANE2_PITCH_EXT, buf->stride[2],
150 EGL_NONE
153 EGLint *attr;
154 switch (fourcc) {
155 case DRM_FORMAT_NV12:
156 case DRM_FORMAT_P010:
157 case DRM_FORMAT_P012:
158 case DRM_FORMAT_P016:
159 attr = attr_nv12;
160 break;
161 case DRM_FORMAT_YUV420:
162 case DRM_FORMAT_YVU420:
163 attr = attr_yuv420;
164 break;
165 default:
166 attr = attr_packed;
167 break;
170 img = eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT,
171 EGL_LINUX_DMA_BUF_EXT, (EGLClientBuffer)0,
172 attr);
173 *out_img = img;
175 error = eglGetError();
177 /* EGL may not support the format, this is not an error. */
178 if (!img && error == EGL_BAD_MATCH)
179 return PIGLIT_SKIP;
181 if (error != EGL_SUCCESS) {
182 printf("eglCreateImageKHR() failed: %s 0x%x\n",
183 piglit_get_egl_error_name(error), error);
185 return PIGLIT_FAIL;
188 if (!img) {
189 fprintf(stderr, "image creation succeeded but returned NULL\n");
190 return PIGLIT_FAIL;
193 *out_img = img;
194 return PIGLIT_PASS;
197 static enum piglit_result
198 sample_buffer(struct piglit_dma_buf *buf, int fourcc)
200 enum piglit_result res;
201 EGLImageKHR img;
202 GLuint tex;
203 int w = buf->w;
204 int h = buf->h;
206 res = egl_image_for_dma_buf_fd(buf, buf->fd, fourcc, &img);
208 /* Release the creator side of the buffer. */
209 piglit_destroy_dma_buf(buf);
211 if (!img) {
212 /* Close the descriptor also, EGL does not have ownership */
213 close(buf->fd);
216 if (res != PIGLIT_PASS)
217 return res;
219 res = texture_for_egl_image(img, &tex);
220 if (res != PIGLIT_PASS)
221 goto destroy;
223 sample_tex(tex, 0, 0, w, h);
225 destroy:
226 glDeleteTextures(1, &tex);
227 eglDestroyImageKHR(eglGetCurrentDisplay(), img);
229 return res;
232 enum piglit_result
233 dma_buf_create_and_sample_32bpp(unsigned w, unsigned h,
234 int fourcc, const unsigned char *src)
236 struct piglit_dma_buf *buf;
237 enum piglit_result res;
239 res = piglit_create_dma_buf(w, h, fourcc, src, &buf);
240 if (res != PIGLIT_PASS)
241 return res;
243 return sample_buffer(buf, fourcc);