pathfinder/shaders/gles2/common.inc.glsl

63 lines
1.9 KiB
Plaintext
Raw Normal View History

// pathfinder/shaders/gles2/common.inc.glsl
//
// Copyright (c) 2017 Mozilla Foundation
2017-08-13 16:39:51 -04:00
#version 100
#extension GL_EXT_draw_buffers : require
#extension GL_EXT_frag_depth : require
#define MAX_PATHS 65536
2017-08-13 16:39:51 -04:00
precision highp float;
// https://stackoverflow.com/a/36078859
int imod(int ia, int ib) {
float a = float(ia), b = float(ib);
float m = a - floor((a + 0.5) / b) * b;
return int(floor(m + 0.5));
}
vec2 transformVertexPosition(vec2 position, mat4 transform) {
return (transform * vec4(position, 0.0, 1.0)).xy;
}
vec2 convertScreenToClipSpace(vec2 position, ivec2 framebufferSize) {
return position / vec2(framebufferSize) * 2.0 - 1.0;
}
float convertPathIndexToDepthValue(int pathIndex) {
2017-08-16 19:37:39 -04:00
return mix(-1.0, 1.0, float(pathIndex) / float(MAX_PATHS));
}
int unpackUInt16(vec2 packedValue) {
ivec2 valueBytes = ivec2(floor(packedValue * 255.0));
return valueBytes.y * 256 + valueBytes.x;
}
vec4 fetchFloat4Data(sampler2D dataTexture, int index, ivec2 dimensions) {
2017-08-13 16:39:51 -04:00
ivec2 pixelCoord = ivec2(imod(index, dimensions.x), index / dimensions.x);
return texture2D(dataTexture, (vec2(pixelCoord) + 0.5) / vec2(dimensions));
}
vec4 fetchFloat4NormIndexedData(sampler2D dataTexture, float normIndex, ivec2 dimensions) {
return fetchFloat4Data(dataTexture, int(normIndex * float(dimensions.x)), dimensions);
}
2017-08-16 01:09:09 -04:00
vec2 fetchFloat2Data(sampler2D dataTexture, int index, ivec2 dimensions) {
vec4 float4Data = fetchFloat4Data(dataTexture, index / 2, dimensions);
return index / 2 * 2 == index ? float4Data.xy : float4Data.zw;
}
int fetchUInt16Data(sampler2D dataTexture, int index, ivec2 dimensions) {
return unpackUInt16(fetchFloat2Data(dataTexture, index, dimensions));
}
2017-08-16 01:09:09 -04:00
vec2 packPathID(int pathID) {
return vec2(imod(pathID, 256), pathID / 256) / 255.0;
}
2017-08-16 19:37:39 -04:00
int unpackPathID(vec2 packedPathID) {
return unpackUInt16(packedPathID);
2017-08-16 19:37:39 -04:00
}