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
5uniform HIGHP_SAMPLER_FLOAT sampler2D sGpuCache;
6
7#define VECS_PER_IMAGE_RESOURCE     2
8
9// TODO(gw): This is here temporarily while we have
10//           both GPU store and cache. When the GPU
11//           store code is removed, we can change the
12//           PrimitiveInstance instance structure to
13//           use 2x unsigned shorts as vertex attributes
14//           instead of an int, and encode the UV directly
15//           in the vertices.
16ivec2 get_gpu_cache_uv(HIGHP_FS_ADDRESS int address) {
17    return ivec2(uint(address) % WR_MAX_VERTEX_TEXTURE_WIDTH,
18                 uint(address) / WR_MAX_VERTEX_TEXTURE_WIDTH);
19}
20
21vec4[2] fetch_from_gpu_cache_2_direct(ivec2 address) {
22    return vec4[2](
23        TEXEL_FETCH(sGpuCache, address, 0, ivec2(0, 0)),
24        TEXEL_FETCH(sGpuCache, address, 0, ivec2(1, 0))
25    );
26}
27
28vec4[2] fetch_from_gpu_cache_2(HIGHP_FS_ADDRESS int address) {
29    ivec2 uv = get_gpu_cache_uv(address);
30    return vec4[2](
31        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(0, 0)),
32        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(1, 0))
33    );
34}
35
36vec4 fetch_from_gpu_cache_1_direct(ivec2 address) {
37    return texelFetch(sGpuCache, address, 0);
38}
39
40vec4 fetch_from_gpu_cache_1(HIGHP_FS_ADDRESS int address) {
41    ivec2 uv = get_gpu_cache_uv(address);
42    return texelFetch(sGpuCache, uv, 0);
43}
44
45#ifdef WR_VERTEX_SHADER
46
47vec4[8] fetch_from_gpu_cache_8(int address) {
48    ivec2 uv = get_gpu_cache_uv(address);
49    return vec4[8](
50        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(0, 0)),
51        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(1, 0)),
52        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(2, 0)),
53        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(3, 0)),
54        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(4, 0)),
55        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(5, 0)),
56        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(6, 0)),
57        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(7, 0))
58    );
59}
60
61vec4[3] fetch_from_gpu_cache_3(int address) {
62    ivec2 uv = get_gpu_cache_uv(address);
63    return vec4[3](
64        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(0, 0)),
65        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(1, 0)),
66        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(2, 0))
67    );
68}
69
70vec4[3] fetch_from_gpu_cache_3_direct(ivec2 address) {
71    return vec4[3](
72        TEXEL_FETCH(sGpuCache, address, 0, ivec2(0, 0)),
73        TEXEL_FETCH(sGpuCache, address, 0, ivec2(1, 0)),
74        TEXEL_FETCH(sGpuCache, address, 0, ivec2(2, 0))
75    );
76}
77
78vec4[4] fetch_from_gpu_cache_4_direct(ivec2 address) {
79    return vec4[4](
80        TEXEL_FETCH(sGpuCache, address, 0, ivec2(0, 0)),
81        TEXEL_FETCH(sGpuCache, address, 0, ivec2(1, 0)),
82        TEXEL_FETCH(sGpuCache, address, 0, ivec2(2, 0)),
83        TEXEL_FETCH(sGpuCache, address, 0, ivec2(3, 0))
84    );
85}
86
87vec4[4] fetch_from_gpu_cache_4(int address) {
88    ivec2 uv = get_gpu_cache_uv(address);
89    return vec4[4](
90        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(0, 0)),
91        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(1, 0)),
92        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(2, 0)),
93        TEXEL_FETCH(sGpuCache, uv, 0, ivec2(3, 0))
94    );
95}
96
97//TODO: image resource is too specific for this module
98
99struct ImageSource {
100    RectWithEndpoint uv_rect;
101    vec4 user_data;
102};
103
104ImageSource fetch_image_source(int address) {
105    //Note: number of blocks has to match `renderer::BLOCKS_PER_UV_RECT`
106    vec4 data[2] = fetch_from_gpu_cache_2(address);
107    RectWithEndpoint uv_rect = RectWithEndpoint(data[0].xy, data[0].zw);
108    return ImageSource(uv_rect, data[1]);
109}
110
111ImageSource fetch_image_source_direct(ivec2 address) {
112    vec4 data[2] = fetch_from_gpu_cache_2_direct(address);
113    RectWithEndpoint uv_rect = RectWithEndpoint(data[0].xy, data[0].zw);
114    return ImageSource(uv_rect, data[1]);
115}
116
117// Fetch optional extra data for a texture cache resource. This can contain
118// a polygon defining a UV rect within the texture cache resource.
119// Note: the polygon coordinates are in homogeneous space.
120struct ImageSourceExtra {
121    vec4 st_tl;
122    vec4 st_tr;
123    vec4 st_bl;
124    vec4 st_br;
125};
126
127ImageSourceExtra fetch_image_source_extra(int address) {
128    vec4 data[4] = fetch_from_gpu_cache_4(address + VECS_PER_IMAGE_RESOURCE);
129    return ImageSourceExtra(
130        data[0],
131        data[1],
132        data[2],
133        data[3]
134    );
135}
136
137#endif //WR_VERTEX_SHADER
138