VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/gallium/ogltest/oglrender.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/* $Id: oglrender.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * OpenGL testcase. Simple OpenGL tests.
4 */
5
6/*
7 * Copyright (C) 2019-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#include "oglrender.h"
29
30
31/*
32 * Old style glBegin/glEnd colored triangle
33 */
34class OGLRenderTriangle : public OGLRender
35{
36 virtual HRESULT InitRender();
37 virtual HRESULT DoRender();
38};
39
40HRESULT OGLRenderTriangle::InitRender()
41{
42 return S_OK;
43}
44
45HRESULT OGLRenderTriangle::DoRender()
46{
47 glClear(GL_COLOR_BUFFER_BIT);
48 glBegin(GL_TRIANGLES);
49 glColor3f ( 1.0f, 0.0f, 0.0f);
50 glVertex2f(-1.0f, -1.0f);
51 glColor3f ( 0.0f, 1.0f, 0.0f);
52 glVertex2f( 0.0f, 1.0f);
53 glColor3f ( 0.0f, 0.0f, 1.0f);
54 glVertex2f( 1.0f, -1.0f);
55 glEnd();
56 glFlush();
57 return S_OK;
58}
59
60
61/*
62 * Texture2D.
63 */
64class OGLRenderTexture2D : public OGLRender
65{
66 virtual HRESULT InitRender();
67 virtual HRESULT DoRender();
68 GLuint texName;
69 static const int texWidth = 8;
70 static const int texHeight = 8;
71};
72
73HRESULT OGLRenderTexture2D::InitRender()
74{
75 static GLubyte texImage[texHeight][texWidth][4];
76 for (int y = 0; y < texHeight; ++y)
77 {
78 for (int x = 0; x < texWidth; ++x)
79 {
80 GLubyte v = 255;
81 if ( (texHeight/4 <= y && y < 3*texHeight/4)
82 && (texWidth/4 <= x && x < 3*texWidth/4))
83 {
84 if (y < x)
85 {
86 v = 0;
87 }
88 }
89
90 texImage[y][x][0] = v;
91 texImage[y][x][1] = 0;
92 texImage[y][x][2] = 0;
93 texImage[y][x][3] = 255;
94 }
95 }
96
97 glClearColor(0.0, 0.0, 1.0, 1.0);
98
99 glGenTextures(1, &texName);
100 glBindTexture(GL_TEXTURE_2D, texName);
101
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
104
105 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
106 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
107
108 glBindTexture(GL_TEXTURE_2D, 0);
109
110 return S_OK;
111}
112
113HRESULT OGLRenderTexture2D::DoRender()
114{
115 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
116
117 glEnable(GL_TEXTURE_2D);
118
119 glBindTexture(GL_TEXTURE_2D, texName);
120
121 glBegin(GL_TRIANGLES);
122 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
123 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
124 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
125
126 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
127 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
128 glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.0f, -1.0f, 0.0f);
129 glEnd();
130
131 glBindTexture(GL_TEXTURE_2D, 0);
132
133 glDisable(GL_TEXTURE_2D);
134
135 glFlush();
136
137 return S_OK;
138}
139
140
141/*
142 * DrawArraysInstanced. Uses shaders from a guest.
143 */
144class OGLRenderDrawArrays : public OGLRender
145{
146 static const int cArrays = 4;
147
148 struct VertexAttribDesc
149 {
150 GLint size;
151 GLenum type;
152 GLboolean normalized;
153 GLsizei stride;
154 };
155
156 static VertexAttribDesc aVertexAttribs[cArrays];
157 GLuint vbNames[cArrays];
158
159 GLuint vertexShader;
160 GLuint fragmentShader;
161 GLuint program;
162
163 virtual HRESULT InitRender();
164 virtual HRESULT DoRender();
165};
166
167static const GLchar *apszVertexShader[] =
168{
169 " #version 120\n"
170 " #extension GL_EXT_gpu_shader4 : enable\n"
171 " uniform vec4 VC[2048];\n"
172 " uniform vec4 posFixup;\n"
173 " void order_ps_input(in vec4[12]);\n"
174 " vec4 OUT[12];\n"
175 " vec4 R0;\n"
176 " vec4 R1;\n"
177 " vec4 R2;\n"
178 " attribute vec4 attrib0;\n"
179 " attribute vec4 attrib1;\n"
180 " attribute vec4 attrib2;\n"
181 " attribute vec4 attrib3;\n"
182 " vec4 tmp0;\n"
183 " vec4 tmp1;\n"
184 " bool p0[4];\n"
185 " uniform vec4 VC1 = { 0.000000, 0.000000, 1.00000, 1.000000 };\n"
186 " uniform vec4 VLC2 = { 1.000000, -1.000000, 0.500000, 0.000000 };\n"
187 " const float FLT_MAX = 1e38;\n"
188 " void main() {"
189 " R0.xy = (attrib0.xy);\n"
190 " R0.yzw = (R0.yyy * attrib2.xyz);\n"
191 " R0.xyz = ((attrib1.xyz * R0.xxx) + R0.yzw);\n"
192 " R0.xyz = (R0.xyz + attrib3.xyz);\n"
193 " R1.xyzw = (R0.xzyz * VC1.zxwy); // (R0.xzyz * VC[1].zxwy);\n"
194 " R1.xy = (R1.yw + R1.xz);\n"
195 " R2.xy = (R1.xy * VLC2.xy);\n"
196 " R2.zw = (R0.zz * VLC2.zx);\n"
197 " OUT[1].xyw = (R0.xyz);\n"
198 " OUT[1].z = (VLC2.w);\n"
199 " OUT[0].xyzw = (R2.xyzw);\n"
200 " gl_Position.xyzw = OUT[0].xyzw;\n"
201 " gl_FogFragCoord = 0.0;\n"
202 " //gl_Position.y = gl_Position.y * posFixup.y;\n"
203 " //gl_Position.xy += posFixup.zw * gl_Position.ww;\n"
204 " //gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n"
205 " }\n"
206};
207
208static const char *passthrough_vshader[] =
209{
210 " #version 120\n"
211 " vec4 R0;\n"
212 " attribute vec4 attrib0;\n"
213 " attribute vec4 attrib1;\n"
214 " attribute vec4 attrib2;\n"
215 " attribute vec4 attrib3;\n"
216 " void main(void)\n"
217 " {\n"
218 " R0 = attrib0;\n"
219 " R0.w = 1.0;\n"
220 " R0.z = 0.0;\n"
221 " gl_Position = R0;\n"
222 " }\n"
223};
224
225static const GLchar *apszFragmentShader[] =
226{
227 " #version 120\n"
228 " #extension GL_EXT_gpu_shader4 : enable\n"
229 " uniform vec4 PC[2048];\n"
230 " varying vec4 IN[31];\n"
231 " vec4 tmp0;\n"
232 " vec4 tmp1;\n"
233 " bool p0[4];\n"
234 " uniform vec4 PLC0;\n"
235 " const float FLT_MAX = 1e38;\n"
236 " void main() {"
237 " gl_FragData[0].xyzw = vec4(1.0, 1.0, 1.0, 1.0); //(PLC0.xyzw);\n"
238 " }\n"
239};
240
241/* static */ OGLRenderDrawArrays::VertexAttribDesc OGLRenderDrawArrays::aVertexAttribs[OGLRenderDrawArrays::cArrays] =
242{
243 {2, GL_FLOAT, GL_FALSE, 8 },
244 {4, GL_FLOAT, GL_FALSE, 0 },
245 {4, GL_FLOAT, GL_FALSE, 0 },
246 {4, GL_FLOAT, GL_FALSE, 0 }
247};
248
249/* Triangle fan. */
250static float aAttrib0[] =
251{
252 0.0f, 200.0f,
253 300.0f, 200.0f,
254 300.0f, 400.0f,
255 0.0f, 400.0f
256};
257
258static float aAttrib0a[] =
259{
260 -1.0f, -1.0f,
261 1.0f, -1.0f,
262 0.0f, 0.0f,
263 0.0f, 2.0f
264};
265
266static float aAttrib1[] =
267{
268 // 1 / (w / 2)
269 0.001556f, 0.000000f, 0.000000f, 1.000000f,
270};
271
272static float aAttrib2[] =
273{
274 // 1/ (h / 2)
275 0.000000f, -0.001874f, 0.000000f, 1.000000f,
276};
277
278static float aAttrib3[] =
279{
280 -1.000000f, 1.000000f, 1.000000f, 1.000000f,
281};
282
283HRESULT OGLRenderDrawArrays::InitRender()
284{
285 glClearColor(0.0, 0.0, 1.0, 1.0);
286
287 int success;
288 char szInfoLog[1024];
289
290 vertexShader = glCreateShader(GL_VERTEX_SHADER);
291 GL_CHECK_ERROR();
292 glShaderSource(vertexShader, 1, apszVertexShader, NULL);
293 GL_CHECK_ERROR();
294 glCompileShader(vertexShader);
295 GL_CHECK_ERROR();
296 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
297 GL_CHECK_ERROR();
298 if (!success)
299 {
300 glGetShaderInfoLog(vertexShader, sizeof(szInfoLog), NULL, szInfoLog);
301 GL_CHECK_ERROR();
302 TestShowError(E_FAIL, szInfoLog);
303 };
304
305 fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
306 GL_CHECK_ERROR();
307 glShaderSource(fragmentShader, 1, apszFragmentShader, NULL);
308 GL_CHECK_ERROR();
309 glCompileShader(fragmentShader);
310 GL_CHECK_ERROR();
311 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
312 GL_CHECK_ERROR();
313 if (!success)
314 {
315 glGetShaderInfoLog(fragmentShader, sizeof(szInfoLog), NULL, szInfoLog);
316 GL_CHECK_ERROR();
317 TestShowError(E_FAIL, szInfoLog);
318 };
319
320 program = glCreateProgram();
321 GL_CHECK_ERROR();
322 glAttachShader(program, vertexShader);
323 GL_CHECK_ERROR();
324 glAttachShader(program, fragmentShader);
325 GL_CHECK_ERROR();
326 glLinkProgram(program);
327 GL_CHECK_ERROR();
328 glGetProgramiv(program, GL_LINK_STATUS, &success);
329 if(!success)
330 {
331 glGetProgramInfoLog(program, sizeof(szInfoLog), NULL, szInfoLog);
332 GL_CHECK_ERROR();
333 TestShowError(E_FAIL, szInfoLog);
334 }
335
336 glUseProgram(program);
337 GL_CHECK_ERROR();
338
339 glGenBuffers(cArrays, vbNames);
340 GL_CHECK_ERROR();
341
342 struct AttribData
343 {
344 GLsizeiptr size;
345 const GLvoid * data;
346 };
347
348 static struct AttribData attribData[cArrays] =
349 {
350 { sizeof(aAttrib0), aAttrib0 },
351 { sizeof(aAttrib1), aAttrib1 },
352 { sizeof(aAttrib2), aAttrib2 },
353 { sizeof(aAttrib3), aAttrib3 },
354 };
355
356 GLuint index;
357 for (index = 0; index < cArrays; ++index)
358 {
359 glBindBuffer(GL_ARRAY_BUFFER, vbNames[index]);
360 GL_CHECK_ERROR();
361
362 glBufferData(GL_ARRAY_BUFFER, attribData[index].size, attribData[index].data, GL_DYNAMIC_DRAW);
363 GL_CHECK_ERROR();
364
365 glEnableVertexAttribArray(index);
366 GL_CHECK_ERROR();
367 glVertexAttribPointer(index, aVertexAttribs[index].size, aVertexAttribs[index].type,
368 aVertexAttribs[index].normalized, aVertexAttribs[index].stride,
369 (const GLvoid *)(uintptr_t)0);
370 GL_CHECK_ERROR();
371
372 GLuint const divisor = aVertexAttribs[index].stride ?
373 0 : /* Once per vertex. */
374 1; /* Once for one (1) set of vertices (or instance). */
375 glVertexAttribDivisor(index, divisor);
376 GL_CHECK_ERROR();
377 }
378
379 glBindBuffer(GL_ARRAY_BUFFER, 0);
380 GL_CHECK_ERROR();
381
382 return S_OK;
383}
384
385HRESULT OGLRenderDrawArrays::DoRender()
386{
387 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
388
389 glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, 1);
390 GL_CHECK_ERROR();
391
392 glFlush();
393 return S_OK;
394}
395
396
397OGLRender *CreateRender(int iRenderId)
398{
399 OGLRender *pRender = NULL;
400 switch (iRenderId)
401 {
402 case 0: pRender = new OGLRenderTriangle(); break;
403 case 1: pRender = new OGLRenderTexture2D(); break;
404 case 2: pRender = new OGLRenderDrawArrays(); break;
405 default:
406 break;
407 }
408 return pRender;
409}
410
411void DeleteRender(OGLRender *pRender)
412{
413 if (pRender)
414 {
415 delete pRender;
416 }
417}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use