cs_scale.glsl (2177B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // This shader must remain compatible with ESSL 1, at least for the 6 // WR_FEATURE_TEXTURE_EXTERNAL_ESSL1 feature, so that it can be used to render 7 // video on GLES devices without GL_OES_EGL_image_external_essl3 support. 8 // This means we cannot use textureSize(), int inputs/outputs, etc. 9 10 #include shared 11 12 varying highp vec2 vUv; 13 flat varying highp vec4 vUvRect; 14 #ifdef WR_FEATURE_TEXTURE_EXTERNAL_ESSL1 15 uniform vec2 uTextureSize; 16 #endif 17 18 #ifdef WR_VERTEX_SHADER 19 20 PER_INSTANCE attribute vec4 aScaleTargetRect; 21 PER_INSTANCE attribute vec4 aScaleSourceRect; 22 PER_INSTANCE attribute float aSourceRectType; 23 24 void main(void) { 25 vec2 src_offset = aScaleSourceRect.xy; 26 vec2 src_size = aScaleSourceRect.zw - aScaleSourceRect.xy; 27 28 // The uvs may be inverted, so use the min and max for the bounds 29 vUvRect = vec4(min(aScaleSourceRect.xy, aScaleSourceRect.zw), 30 max(aScaleSourceRect.xy, aScaleSourceRect.zw)); 31 vUv = (src_offset + src_size * aPosition.xy); 32 33 if (int(aSourceRectType) == UV_TYPE_UNNORMALIZED) { 34 vUvRect = vec4(vUvRect.xy + vec2(0.5), vUvRect.zw - vec2(0.5)); 35 36 #ifdef WR_FEATURE_TEXTURE_RECT 37 // In WR_FEATURE_TEXTURE_RECT mode the UV coordinates used to sample 38 // from the texture should be unnormalized, so we leave them as is. 39 vec2 texture_size = vec2(1, 1); 40 #elif defined(WR_FEATURE_TEXTURE_EXTERNAL_ESSL1) 41 vec2 texture_size = uTextureSize; 42 #else 43 vec2 texture_size = vec2(TEX_SIZE(sColor0)); 44 #endif 45 vUvRect /= texture_size.xyxy; 46 vUv /= texture_size; 47 } 48 49 vec2 pos = mix(aScaleTargetRect.xy, aScaleTargetRect.zw, aPosition.xy); 50 gl_Position = uTransform * vec4(pos, 0.0, 1.0); 51 } 52 53 #endif 54 55 #ifdef WR_FRAGMENT_SHADER 56 57 void main(void) { 58 vec2 st = clamp(vUv, vUvRect.xy, vUvRect.zw); 59 oFragColor = TEX_SAMPLE(sColor0, st); 60 } 61 62 #ifdef SWGL_DRAW_SPAN 63 void swgl_drawSpanRGBA8() { 64 swgl_commitTextureLinearRGBA8(sColor0, vUv, vUvRect); 65 } 66 #endif 67 68 #endif