|
| 1 | +#version 450 |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2019 The GraphicsFuzz Project Authors |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +precision highp float; |
| 20 | + |
| 21 | +layout(location = 0) out vec4 _GLF_color; |
| 22 | + |
| 23 | +uniform vec2 injectionSwitch; |
| 24 | + |
| 25 | +uniform vec2 resolution; |
| 26 | + |
| 27 | +struct BinarySearchObject{ |
| 28 | + int prime_numbers[10]; |
| 29 | +}; |
| 30 | + |
| 31 | +vec2 brick(vec2 uv) { |
| 32 | + int a = 4; |
| 33 | + do { |
| 34 | + uv.y -= step(injectionSwitch.y, uv.x); |
| 35 | + uv.x -= fract(tanh(uv.x)) / ldexp(injectionSwitch.y, findMSB(a)); |
| 36 | + a--; |
| 37 | + } while (a > int(injectionSwitch.x)); |
| 38 | + int b = 3; |
| 39 | + do { |
| 40 | + uv.y -= step(injectionSwitch.y, uv.x) + float(a); |
| 41 | + uv.x *= (isnan(uv.y) ? cosh(gl_FragCoord.y) : tanh(gl_FragCoord.x)); |
| 42 | + b--; |
| 43 | + } while (b > int(injectionSwitch.x)); |
| 44 | + int c = 2; |
| 45 | + do { |
| 46 | + uv.y -= step(injectionSwitch.y, uv.x) + float(a) + float(b); |
| 47 | + uv.x += ldexp(injectionSwitch.y, isinf(uv.y + uv.x) ? findMSB(b) : findMSB(a)); |
| 48 | + c--; |
| 49 | + } while (c > int(injectionSwitch.x)); |
| 50 | + int d = 1; |
| 51 | + do { |
| 52 | + uv.y -= step(injectionSwitch.y, uv.x) + float(a) + float(b) + float(c); |
| 53 | + d--; |
| 54 | + } while (d > int(injectionSwitch.x)); |
| 55 | + return fract(uv); |
| 56 | +} |
| 57 | + |
| 58 | +float patternize(vec2 uv) { |
| 59 | + vec2 size = vec2(0.45); |
| 60 | + vec2 st = smoothstep(size, size, uv); |
| 61 | + switch (int(mod(gl_FragCoord.y, 5.0))) { |
| 62 | + case 0: |
| 63 | + return mix(pow(st.x, injectionSwitch.y), st.x, size.y); |
| 64 | + break; |
| 65 | + case 1: |
| 66 | + return mix(pow(uv.y, injectionSwitch.y), st.y, size.x); |
| 67 | + break; |
| 68 | + case 2: |
| 69 | + discard; |
| 70 | + break; |
| 71 | + case 3: |
| 72 | + return mix(pow(uv.y, injectionSwitch.y), uv.y, size.y); |
| 73 | + break; |
| 74 | + case 4: |
| 75 | + return mix(pow(st.y, injectionSwitch.y), st.x, size.x); |
| 76 | + break; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +int binarySearch(BinarySearchObject obj, int x) { |
| 81 | + int l = 0, r = 9; |
| 82 | + while (l <= r) { |
| 83 | + int m = (l + r) / 2; |
| 84 | + if (obj.prime_numbers[m] == x) { |
| 85 | + return m; |
| 86 | + } |
| 87 | + |
| 88 | + if (obj.prime_numbers[m] < x) { |
| 89 | + l = m + 1; |
| 90 | + } else { |
| 91 | + r = m - 1; |
| 92 | + } |
| 93 | + } |
| 94 | + // If an element is not present in the array we return -1. |
| 95 | + return -1; |
| 96 | +} |
| 97 | + |
| 98 | +void main() { |
| 99 | + BinarySearchObject obj; |
| 100 | + // Initialize first 10 prime numbers to the array. |
| 101 | + for (int i = 0; i < 10; i++) { |
| 102 | + if (i == 0) { |
| 103 | + obj.prime_numbers[i] = 2; |
| 104 | + } else if (i == 1) { |
| 105 | + obj.prime_numbers[i] = 3; |
| 106 | + } else if (i == 2) { |
| 107 | + obj.prime_numbers[i] = 5; |
| 108 | + } else if (i == 3) { |
| 109 | + obj.prime_numbers[i] = 7; |
| 110 | + } else if (i == 4) { |
| 111 | + obj.prime_numbers[i] = 11; |
| 112 | + } else if (i == 5) { |
| 113 | + obj.prime_numbers[i] = 13; |
| 114 | + } else if (i == 6) { |
| 115 | + obj.prime_numbers[i] = 17; |
| 116 | + } else if (i == 7) { |
| 117 | + obj.prime_numbers[i] = 19; |
| 118 | + } else if (i == 8) { |
| 119 | + obj.prime_numbers[i] = 23; |
| 120 | + } else if (i == 9) { |
| 121 | + obj.prime_numbers[i] = 29; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + vec2 uv = (gl_FragCoord.xy / resolution.x) * vec2(resolution.x / resolution.y, 1.0); |
| 126 | + vec2 b = brick(uv * 7.0); |
| 127 | + vec3 color = vec3(patternize(b)); |
| 128 | + |
| 129 | + if (gl_FragCoord.y < resolution.y / 1.1) { |
| 130 | + // We are going to search the item in array by giving the value of the item index 4 and 0 in an array. |
| 131 | + if (binarySearch(obj, obj.prime_numbers[4]) != -(int(resolution.y)) && binarySearch(obj, obj.prime_numbers[0]) >= -(int(resolution.x))) { |
| 132 | + color.yz -= dot(float(binarySearch(obj, obj.prime_numbers[4])), float(binarySearch(obj, obj.prime_numbers[0]))); |
| 133 | + } else { |
| 134 | + discard; |
| 135 | + } |
| 136 | + } else { |
| 137 | + // The following condition is true as there is no value 1 in the array. |
| 138 | + if (binarySearch(obj, 1) == -1) { |
| 139 | + discard; |
| 140 | + } else { |
| 141 | + color.yz += color.yz; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + _GLF_color = vec4(color, injectionSwitch.y); |
| 146 | + |
| 147 | +} |
0 commit comments