Hash :
5f9548c3
Author :
Date :
2023-05-19T11:51:04
Vulkan: Free the garbage memory before realloc
Currently image allocations fall back to system memory in case of
a device OOM. However, in some cases, it is also possible to gain
some memory by freeing garbage memory from the device. This allows
us to keep the allocation on the device memory.
* Updated the image allocation fallback, so we will try cleaning the
garbage memory through the renderer before retrying the allocation.
* finishOneCommandBatchAndCleanup() in RendererVk, which will call a
similar function in its CommandQueue. It will be called until there
are no more in-flight submissions.
* The existing finishOneCommandBatchAndCleanup() in CommandQueue has
been renamed to finishOneCommandBatchAndCleanupImpl().
* Updated the flags used for VMA image allocations. If any device memory
is freed after garbage cleanup to make enough space for the new
allocation, it will take precedence over the system memory.
* Added unit tests in which a new image allocation could happen on the
device after freeing the garbage memory.
* They use a 2D texture and a 2D texture array for garbage.
Bug: b/280304441
Change-Id: Ia5e605e180833b44af8c77550ab1b0b8ba21724e
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4547941
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Charlie Lao <cclao@google.com>
Commit-Queue: Amirali Abdolrashidi <abdolrashidi@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
//
// Copyright 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "util/shader_utils.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
#include "common/utilities.h"
#include "util/test_utils.h"
namespace
{
GLuint CompileProgramInternal(const char *vsSource,
const char *tcsSource,
const char *tesSource,
const char *gsSource,
const char *fsSource,
const std::function<void(GLuint)> &preLinkCallback)
{
GLuint program = glCreateProgram();
GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
if (vs == 0 || fs == 0)
{
glDeleteShader(fs);
glDeleteShader(vs);
glDeleteProgram(program);
return 0;
}
glAttachShader(program, vs);
glDeleteShader(vs);
glAttachShader(program, fs);
glDeleteShader(fs);
GLuint tcs = 0;
GLuint tes = 0;
GLuint gs = 0;
if (strlen(tcsSource) > 0)
{
tcs = CompileShader(GL_TESS_CONTROL_SHADER_EXT, tcsSource);
if (tcs == 0)
{
glDeleteShader(vs);
glDeleteShader(fs);
glDeleteProgram(program);
return 0;
}
glAttachShader(program, tcs);
glDeleteShader(tcs);
}
if (strlen(tesSource) > 0)
{
tes = CompileShader(GL_TESS_EVALUATION_SHADER_EXT, tesSource);
if (tes == 0)
{
glDeleteShader(vs);
glDeleteShader(fs);
glDeleteShader(tcs);
glDeleteProgram(program);
return 0;
}
glAttachShader(program, tes);
glDeleteShader(tes);
}
if (strlen(gsSource) > 0)
{
gs = CompileShader(GL_GEOMETRY_SHADER_EXT, gsSource);
if (gs == 0)
{
glDeleteShader(vs);
glDeleteShader(fs);
glDeleteShader(tcs);
glDeleteShader(tes);
glDeleteProgram(program);
return 0;
}
glAttachShader(program, gs);
glDeleteShader(gs);
}
if (preLinkCallback)
{
preLinkCallback(program);
}
glLinkProgram(program);
return CheckLinkStatusAndReturnProgram(program, true);
}
const void *gCallbackChainUserParam;
void KHRONOS_APIENTRY DebugMessageCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar *message,
const void *userParam)
{
std::string sourceText = gl::GetDebugMessageSourceString(source);
std::string typeText = gl::GetDebugMessageTypeString(type);
std::string severityText = gl::GetDebugMessageSeverityString(severity);
std::cerr << sourceText << ", " << typeText << ", " << severityText << ": " << message << "\n";
GLDEBUGPROC callbackChain = reinterpret_cast<GLDEBUGPROC>(const_cast<void *>(userParam));
if (callbackChain)
{
callbackChain(source, type, id, severity, length, message, gCallbackChainUserParam);
}
}
void GetPerfCounterValue(const CounterNameToIndexMap &counterIndexMap,
std::vector<angle::PerfMonitorTriplet> &triplets,
const char *name,
GLuint64 *counterOut)
{
auto iter = counterIndexMap.find(name);
ASSERT(iter != counterIndexMap.end());
GLuint counterIndex = iter->second;
for (const angle::PerfMonitorTriplet &triplet : triplets)
{
ASSERT(triplet.group == 0);
if (triplet.counter == counterIndex)
{
*counterOut = triplet.value;
return;
}
}
UNREACHABLE();
}
} // namespace
GLuint CompileShader(GLenum type, const char *source)
{
GLuint shader = glCreateShader(type);
const char *sourceArray[1] = {source};
glShaderSource(shader, 1, sourceArray, nullptr);
glCompileShader(shader);
GLint compileResult;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
if (compileResult == 0)
{
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
// Info log length includes the null terminator, so 1 means that the info log is an empty
// string.
if (infoLogLength > 1)
{
std::vector<GLchar> infoLog(infoLogLength);
glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), nullptr, &infoLog[0]);
std::cerr << "shader compilation failed: " << &infoLog[0];
}
else
{
std::cerr << "shader compilation failed. <Empty log message>";
}
std::cerr << std::endl;
glDeleteShader(shader);
shader = 0;
}
return shader;
}
GLuint CompileShaderFromFile(GLenum type, const std::string &sourcePath)
{
std::string source;
if (!angle::ReadEntireFileToString(sourcePath.c_str(), &source))
{
std::cerr << "Error reading shader file: " << sourcePath << "\n";
return 0;
}
return CompileShader(type, source.c_str());
}
GLuint CheckLinkStatusAndReturnProgram(GLuint program, bool outputErrorMessages)
{
if (glGetError() != GL_NO_ERROR)
return 0;
GLint linkStatus;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus == 0)
{
if (outputErrorMessages)
{
GLint infoLogLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
// Info log length includes the null terminator, so 1 means that the info log is an
// empty string.
if (infoLogLength > 1)
{
std::vector<GLchar> infoLog(infoLogLength);
glGetProgramInfoLog(program, static_cast<GLsizei>(infoLog.size()), nullptr,
&infoLog[0]);
std::cerr << "program link failed: " << &infoLog[0];
}
else
{
std::cerr << "program link failed. <Empty log message>";
}
}
glDeleteProgram(program);
return 0;
}
return program;
}
GLuint GetProgramShader(GLuint program, GLint requestedType)
{
static constexpr GLsizei kMaxShaderCount = 16;
GLuint attachedShaders[kMaxShaderCount] = {0u};
GLsizei count = 0;
glGetAttachedShaders(program, kMaxShaderCount, &count, attachedShaders);
for (int i = 0; i < count; ++i)
{
GLint type = 0;
glGetShaderiv(attachedShaders[i], GL_SHADER_TYPE, &type);
if (type == requestedType)
{
return attachedShaders[i];
}
}
return 0;
}
GLuint CompileProgramWithTransformFeedback(
const char *vsSource,
const char *fsSource,
const std::vector<std::string> &transformFeedbackVaryings,
GLenum bufferMode)
{
auto preLink = [&](GLuint program) {
if (transformFeedbackVaryings.size() > 0)
{
std::vector<const char *> constCharTFVaryings;
for (const std::string &transformFeedbackVarying : transformFeedbackVaryings)
{
constCharTFVaryings.push_back(transformFeedbackVarying.c_str());
}
glTransformFeedbackVaryings(program,
static_cast<GLsizei>(transformFeedbackVaryings.size()),
&constCharTFVaryings[0], bufferMode);
}
};
return CompileProgramInternal(vsSource, "", "", "", fsSource, preLink);
}
GLuint CompileProgram(const char *vsSource, const char *fsSource)
{
return CompileProgramInternal(vsSource, "", "", "", fsSource, nullptr);
}
GLuint CompileProgram(const char *vsSource,
const char *fsSource,
const std::function<void(GLuint)> &preLinkCallback)
{
return CompileProgramInternal(vsSource, "", "", "", fsSource, preLinkCallback);
}
GLuint CompileProgramWithGS(const char *vsSource, const char *gsSource, const char *fsSource)
{
return CompileProgramInternal(vsSource, "", "", gsSource, fsSource, nullptr);
}
GLuint CompileProgramWithTESS(const char *vsSource,
const char *tcsSource,
const char *tesSource,
const char *fsSource)
{
return CompileProgramInternal(vsSource, tcsSource, tesSource, "", fsSource, nullptr);
}
GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath)
{
std::string vsSource;
if (!angle::ReadEntireFileToString(vsPath.c_str(), &vsSource))
{
std::cerr << "Error reading shader: " << vsPath << "\n";
return 0;
}
std::string fsSource;
if (!angle::ReadEntireFileToString(fsPath.c_str(), &fsSource))
{
std::cerr << "Error reading shader: " << fsPath << "\n";
return 0;
}
return CompileProgram(vsSource.c_str(), fsSource.c_str());
}
GLuint CompileComputeProgram(const char *csSource, bool outputErrorMessages)
{
GLuint program = glCreateProgram();
GLuint cs = CompileShader(GL_COMPUTE_SHADER, csSource);
if (cs == 0)
{
glDeleteProgram(program);
return 0;
}
glAttachShader(program, cs);
glLinkProgram(program);
return CheckLinkStatusAndReturnProgram(program, outputErrorMessages);
}
GLuint LoadBinaryProgramOES(const std::vector<uint8_t> &binary, GLenum binaryFormat)
{
GLuint program = glCreateProgram();
glProgramBinaryOES(program, binaryFormat, binary.data(), static_cast<GLint>(binary.size()));
return CheckLinkStatusAndReturnProgram(program, true);
}
GLuint LoadBinaryProgramES3(const std::vector<uint8_t> &binary, GLenum binaryFormat)
{
GLuint program = glCreateProgram();
glProgramBinary(program, binaryFormat, binary.data(), static_cast<GLint>(binary.size()));
return CheckLinkStatusAndReturnProgram(program, true);
}
bool LinkAttachedProgram(GLuint program)
{
glLinkProgram(program);
return (CheckLinkStatusAndReturnProgram(program, true) != 0);
}
void EnableDebugCallback(GLDEBUGPROC callbackChain, const void *userParam)
{
gCallbackChainUserParam = userParam;
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
// Enable medium and high priority messages.
glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH, 0, nullptr,
GL_TRUE);
glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_MEDIUM, 0, nullptr,
GL_TRUE);
// Disable low and notification priority messages.
glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, nullptr,
GL_FALSE);
glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, nullptr,
GL_FALSE);
// Disable performance messages to reduce spam.
glDebugMessageControlKHR(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE, GL_DONT_CARE, 0, nullptr,
GL_FALSE);
glDebugMessageCallbackKHR(DebugMessageCallback, reinterpret_cast<const void *>(callbackChain));
}
CounterNameToIndexMap BuildCounterNameToIndexMap()
{
GLint numCounters = 0;
glGetPerfMonitorCountersAMD(0, &numCounters, nullptr, 0, nullptr);
if (glGetError() != GL_NO_ERROR)
{
return {};
}
std::vector<GLuint> counterIndexes(numCounters, 0);
glGetPerfMonitorCountersAMD(0, nullptr, nullptr, numCounters, counterIndexes.data());
if (glGetError() != GL_NO_ERROR)
{
return {};
}
CounterNameToIndexMap indexMap;
for (GLuint counterIndex : counterIndexes)
{
static constexpr size_t kBufSize = 1000;
char buffer[kBufSize] = {};
glGetPerfMonitorCounterStringAMD(0, counterIndex, kBufSize, nullptr, buffer);
if (glGetError() != GL_NO_ERROR)
{
return {};
}
indexMap[buffer] = counterIndex;
}
return indexMap;
}
std::vector<angle::PerfMonitorTriplet> GetPerfMonitorTriplets()
{
GLuint resultSize = 0;
glGetPerfMonitorCounterDataAMD(0, GL_PERFMON_RESULT_SIZE_AMD, sizeof(GLuint), &resultSize,
nullptr);
if (glGetError() != GL_NO_ERROR || resultSize == 0)
{
return {};
}
std::vector<angle::PerfMonitorTriplet> perfResults(resultSize /
sizeof(angle::PerfMonitorTriplet));
glGetPerfMonitorCounterDataAMD(
0, GL_PERFMON_RESULT_AMD, static_cast<GLsizei>(perfResults.size() * sizeof(perfResults[0])),
&perfResults.data()->group, nullptr);
if (glGetError() != GL_NO_ERROR)
{
return {};
}
return perfResults;
}
angle::VulkanPerfCounters GetPerfCounters(const CounterNameToIndexMap &indexMap)
{
std::vector<angle::PerfMonitorTriplet> perfResults = GetPerfMonitorTriplets();
angle::VulkanPerfCounters counters;
#define ANGLE_UNPACK_PERF_COUNTER(COUNTER) \
GetPerfCounterValue(indexMap, perfResults, #COUNTER, &counters.COUNTER);
ANGLE_VK_PERF_COUNTERS_X(ANGLE_UNPACK_PERF_COUNTER)
#undef ANGLE_UNPACK_PERF_COUNTER
return counters;
}
CounterNameToValueMap BuildCounterNameToValueMap()
{
CounterNameToIndexMap indexMap = BuildCounterNameToIndexMap();
std::vector<angle::PerfMonitorTriplet> perfResults = GetPerfMonitorTriplets();
CounterNameToValueMap valueMap;
for (const auto &iter : indexMap)
{
const std::string &name = iter.first;
GLuint index = iter.second;
valueMap[name] = perfResults[index].value;
}
return valueMap;
}
namespace angle
{
namespace essl1_shaders
{
const char *PositionAttrib()
{
return "a_position";
}
const char *ColorUniform()
{
return "u_color";
}
const char *Texture2DUniform()
{
return "u_tex2D";
}
namespace vs
{
// A shader that sets gl_Position to zero.
const char *Zero()
{
return R"(void main()
{
gl_Position = vec4(0);
})";
}
// A shader that sets gl_Position to attribute a_position.
const char *Simple()
{
return R"(precision highp float;
attribute vec4 a_position;
void main()
{
gl_Position = a_position;
})";
}
// A shader that sets gl_Position to attribute a_position, and sets gl_PointSize to 1.
const char *SimpleForPoints()
{
return R"(precision highp float;
attribute vec4 a_position;
void main()
{
gl_Position = a_position;
gl_PointSize = 1.0;
})";
}
// A shader that simply passes through attribute a_position, setting it to gl_Position and varying
// v_position.
const char *Passthrough()
{
return R"(precision highp float;
attribute vec4 a_position;
varying vec4 v_position;
void main()
{
gl_Position = a_position;
v_position = a_position;
})";
}
// A shader that simply passes through attribute a_position, setting it to gl_Position and varying
// texcoord.
const char *Texture2D()
{
return R"(precision highp float;
attribute vec4 a_position;
varying vec2 v_texCoord;
void main()
{
gl_Position = a_position;
v_texCoord = a_position.xy * 0.5 + vec2(0.5);
})";
}
const char *Texture2DArray()
{
return R"(#version 300 es
out vec2 v_texCoord;
in vec4 a_position;
void main()
{
gl_Position = vec4(a_position.xy, 0.0, 1.0);
v_texCoord = (a_position.xy * 0.5) + 0.5;
})";
}
} // namespace vs
namespace fs
{
// A shader that renders a simple checker pattern of red and green. X axis and y axis separate the
// different colors. Needs varying v_position.
const char *Checkered()
{
return R"(precision highp float;
varying vec4 v_position;
void main()
{
bool isLeft = v_position.x < 0.0;
bool isTop = v_position.y < 0.0;
if (isLeft)
{
if (isTop)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
else
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
}
else
{
if (isTop)
{
gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
}
else
{
gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}
}
})";
}
// A shader that fills with color taken from uniform named "color".
const char *UniformColor()
{
return R"(uniform mediump vec4 u_color;
void main(void)
{
gl_FragColor = u_color;
})";
}
// A shader that fills with 100% opaque red.
const char *Red()
{
return R"(precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
})";
}
// A shader that fills with 100% opaque green.
const char *Green()
{
return R"(precision mediump float;
void main()
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
})";
}
// A shader that fills with 100% opaque blue.
const char *Blue()
{
return R"(precision mediump float;
void main()
{
gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
})";
}
// A shader that samples the texture.
const char *Texture2D()
{
return R"(precision mediump float;
uniform sampler2D u_tex2D;
varying vec2 v_texCoord;
void main()
{
gl_FragColor = texture2D(u_tex2D, v_texCoord);
})";
}
const char *Texture2DArray()
{
return R"(#version 300 es
precision highp float;
uniform highp sampler2DArray tex2DArray;
uniform int slice;
in vec2 v_texCoord;
out vec4 fragColor;
void main()
{
fragColor = texture(tex2DArray, vec3(v_texCoord, float(slice)));
})";
}
} // namespace fs
} // namespace essl1_shaders
namespace essl3_shaders
{
const char *PositionAttrib()
{
return "a_position";
}
const char *Texture2DUniform()
{
return "u_tex2D";
}
const char *LodUniform()
{
return "u_lod";
}
namespace vs
{
// A shader that sets gl_Position to zero.
const char *Zero()
{
return R"(#version 300 es
void main()
{
gl_Position = vec4(0);
})";
}
// A shader that sets gl_Position to attribute a_position.
const char *Simple()
{
return R"(#version 300 es
in vec4 a_position;
void main()
{
gl_Position = a_position;
})";
}
// A shader that sets gl_Position to attribute a_position, and sets gl_PointSize to 1.
const char *SimpleForPoints()
{
return R"(#version 300 es
in vec4 a_position;
void main()
{
gl_Position = a_position;
gl_PointSize = 1.0;
})";
}
// A shader that simply passes through attribute a_position, setting it to gl_Position and varying
// v_position.
const char *Passthrough()
{
return R"(#version 300 es
in vec4 a_position;
out vec4 v_position;
void main()
{
gl_Position = a_position;
v_position = a_position;
})";
}
// A shader that simply passes through attribute a_position, setting it to gl_Position and varying
// texcoord.
const char *Texture2DLod()
{
return R"(#version 300 es
in vec4 a_position;
out vec2 v_texCoord;
void main()
{
gl_Position = vec4(a_position.xy, 0.0, 1.0);
v_texCoord = a_position.xy * 0.5 + vec2(0.5);
})";
}
} // namespace vs
namespace fs
{
// A shader that fills with 100% opaque red.
const char *Red()
{
return R"(#version 300 es
precision highp float;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
})";
}
// A shader that fills with 100% opaque green.
const char *Green()
{
return R"(#version 300 es
precision highp float;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
})";
}
// A shader that fills with 100% opaque blue.
const char *Blue()
{
return R"(#version 300 es
precision highp float;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
})";
}
// A shader that samples the texture at a given lod.
const char *Texture2DLod()
{
return R"(#version 300 es
precision mediump float;
uniform sampler2D u_tex2D;
uniform float u_lod;
in vec2 v_texCoord;
out vec4 my_FragColor;
void main()
{
my_FragColor = textureLod(u_tex2D, v_texCoord, u_lod);
})";
}
} // namespace fs
} // namespace essl3_shaders
namespace essl31_shaders
{
const char *PositionAttrib()
{
return "a_position";
}
namespace vs
{
// A shader that sets gl_Position to zero.
const char *Zero()
{
return R"(#version 310 es
void main()
{
gl_Position = vec4(0);
})";
}
// A shader that sets gl_Position to attribute a_position.
const char *Simple()
{
return R"(#version 310 es
in vec4 a_position;
void main()
{
gl_Position = a_position;
})";
}
// A shader that simply passes through attribute a_position, setting it to gl_Position and varying
// v_position.
const char *Passthrough()
{
return R"(#version 310 es
in vec4 a_position;
out vec4 v_position;
void main()
{
gl_Position = a_position;
v_position = a_position;
})";
}
} // namespace vs
namespace fs
{
// A shader that fills with 100% opaque red.
const char *Red()
{
return R"(#version 310 es
precision highp float;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
})";
}
// A shader that fills with 100% opaque green.
const char *Green()
{
return R"(#version 310 es
precision highp float;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
})";
}
// A shader that renders a simple gradient of red to green. Needs varying v_position.
const char *RedGreenGradient()
{
return R"(#version 310 es
precision highp float;
in vec4 v_position;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(v_position.xy * 0.5 + vec2(0.5), 0.0, 1.0);
})";
}
} // namespace fs
} // namespace essl31_shaders
} // namespace angle