]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Fix edge clamping behavior with out self-allocated textures.
[nageru] / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <cstddef>
6
7 #include "util.h"
8
9 using namespace std;
10
11 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
12         : frame_size(frame_size), buffer(buffer)
13 {
14         userdata.reset(new Userdata[num_queued_frames]);
15         for (size_t i = 0; i < num_queued_frames; ++i) {
16                 GLuint pbo;
17                 glGenBuffers(1, &pbo);
18                 check_error();
19                 glBindBuffer(buffer, pbo);
20                 check_error();
21                 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
22                 check_error();
23
24                 Frame frame;
25                 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
26                 frame.data2 = frame.data + frame_size / 2;
27                 check_error();
28                 frame.size = frame_size;
29                 frame.userdata = &userdata[i];
30                 userdata[i].pbo = pbo;
31                 frame.owner = this;
32                 frame.interleaved = true;
33
34                 // Create textures.
35                 glGenTextures(1, &userdata[i].tex_y);
36                 check_error();
37                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y);
38                 check_error();
39                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
40                 check_error();
41                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
42                 check_error();
43                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
44                 check_error();
45                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
46                 check_error();
47
48                 glGenTextures(1, &userdata[i].tex_cbcr);
49                 check_error();
50                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr);
51                 check_error();
52                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
53                 check_error();
54                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
55                 check_error();
56                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
57                 check_error();
58                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
59                 check_error();
60
61                 freelist.push(frame);
62         }
63         glBindBuffer(buffer, 0);
64         check_error();
65         glBindTexture(GL_TEXTURE_2D, 0);
66         check_error();
67 }
68
69 PBOFrameAllocator::~PBOFrameAllocator()
70 {
71         while (!freelist.empty()) {
72                 Frame frame = freelist.front();
73                 freelist.pop();
74                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
75                 glBindBuffer(buffer, pbo);
76                 check_error();
77                 glUnmapBuffer(buffer);
78                 check_error();
79                 glBindBuffer(buffer, 0);
80                 check_error();
81                 glDeleteBuffers(1, &pbo);
82                 check_error();
83                 GLuint tex_y = ((Userdata *)frame.userdata)->tex_y;
84                 glDeleteTextures(1, &tex_y);
85                 check_error();
86                 GLuint tex_cbcr = ((Userdata *)frame.userdata)->tex_cbcr;
87                 glDeleteTextures(1, &tex_cbcr);
88                 check_error();
89         }
90 }
91 //static int sumsum = 0;
92
93 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
94 {
95         Frame vf;
96
97         std::unique_lock<std::mutex> lock(freelist_mutex);  // Meh.
98         if (freelist.empty()) {
99                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
100         } else {
101                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
102                 vf = freelist.front();
103                 freelist.pop();  // Meh.
104         }
105         vf.len = 0;
106         return vf;
107 }
108
109 void PBOFrameAllocator::release_frame(Frame frame)
110 {
111         std::unique_lock<std::mutex> lock(freelist_mutex);
112         freelist.push(frame);
113         //--sumsum;
114 }