This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
/
framebuffer.go
256 lines (202 loc) · 8.22 KB
/
framebuffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) 2013-2018 Laurent Moussault. All rights reserved.
// Licensed under a simplified BSD license (see LICENSE file).
package gl
import (
"strconv"
"unsafe"
)
////////////////////////////////////////////////////////////////////////////////
/*
#include "glad.h"
static inline GLuint NewFramebuffer() {
GLuint fbo;
glCreateFramebuffers(1, &fbo);
return fbo;
}
static inline void FramebufferSetEmptySize(GLuint fbo, GLint width, GLint height) {
glNamedFramebufferParameteri(fbo, GL_FRAMEBUFFER_DEFAULT_WIDTH, width);
glNamedFramebufferParameteri(fbo, GL_FRAMEBUFFER_DEFAULT_HEIGHT, height);
glNamedFramebufferParameteri(fbo, GL_FRAMEBUFFER_DEFAULT_LAYERS, 0);
glNamedFramebufferParameteri(fbo, GL_FRAMEBUFFER_DEFAULT_SAMPLES, 0);
glNamedFramebufferParameteri(fbo, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, 0);
}
static inline void FramebufferTexture(GLuint fbo, GLenum a, GLuint t, GLint l) {
glNamedFramebufferTexture(fbo, a, t, l);
}
static inline void FramebufferRenderbuffer(GLuint fbo, GLenum a, GLuint t) {
glNamedFramebufferRenderbuffer(fbo, a, GL_RENDERBUFFER, t);
}
static inline void FramebufferReadBuffer(GLuint fbo, GLenum a) {
glNamedFramebufferReadBuffer(fbo, a);
}
static inline void FramebufferDrawBuffer(GLuint fbo, GLenum a) {
glNamedFramebufferDrawBuffer(fbo, a);
}
static inline void FramebufferDrawBuffers(GLuint fbo, GLsizei n, GLenum *a) {
glNamedFramebufferDrawBuffers(fbo, n, a);
}
static inline GLenum FramebufferCheckStatus(GLuint fbo, GLenum t) {
return glCheckNamedFramebufferStatus(fbo, t);
}
static inline void FramebufferBind(GLuint fbo, GLenum t) {
glBindFramebuffer(t, fbo);
}
static inline void FramebufferClearColorUint(GLuint fbo, const GLuint *v) {
glClearNamedFramebufferuiv(fbo, GL_COLOR, 0, v);
}
static inline void FramebufferClearColor(GLuint fbo, GLint drawbuffer, const GLfloat *v) {
glClearNamedFramebufferfv(fbo, GL_COLOR, drawbuffer, v);
}
static inline void FramebufferClearDepth(GLuint fbo, const GLfloat v) {
glClearNamedFramebufferfv(fbo, GL_DEPTH, 0, &v);
}
static inline void FramebufferBlit(GLuint fbo, GLuint dstFbo, GLint srcX1, GLint srcY1, GLint srcX2, GLint srcY2, GLint dstX1, GLint dstY1, GLint dstX2, GLint dstY2, GLbitfield m, GLenum f) {
glBlitNamedFramebuffer(fbo, dstFbo, srcX1, srcY1, srcX2, srcY2, dstX1, dstY1, dstX2, dstY2, m, f);
}
static inline void FramebufferDelete(GLuint fbo) {
glDeleteFramebuffers(1, &fbo);
}
*/
import (
"C"
)
////////////////////////////////////////////////////////////////////////////////
type Framebuffer struct {
object C.GLuint
}
var DefaultFramebuffer = Framebuffer{
object: C.GLuint(0),
}
////////////////////////////////////////////////////////////////////////////////
func NewFramebuffer() Framebuffer {
var f Framebuffer
f.object = C.NewFramebuffer()
return f
}
func (fb Framebuffer) SetEmptySize(w, h int16) {
C.FramebufferSetEmptySize(fb.object, C.GLint(w), C.GLint(h))
}
////////////////////////////////////////////////////////////////////////////////
func (fb Framebuffer) Texture(a FramebufferAttachment, t Texture2D, level int32) {
C.FramebufferTexture(fb.object, C.GLenum(a), t.object, C.GLint(level))
}
func (fb Framebuffer) Renderbuffer(a FramebufferAttachment, r Renderbuffer) {
C.FramebufferRenderbuffer(fb.object, C.GLenum(a), r.object)
}
type FramebufferAttachment C.GLenum
const (
ColorAttachment0 FramebufferAttachment = C.GL_COLOR_ATTACHMENT0
ColorAttachment1 FramebufferAttachment = C.GL_COLOR_ATTACHMENT1
ColorAttachment2 FramebufferAttachment = C.GL_COLOR_ATTACHMENT2
ColorAttachment3 FramebufferAttachment = C.GL_COLOR_ATTACHMENT3
DepthAttachment FramebufferAttachment = C.GL_DEPTH_ATTACHMENT
StencilAttachment FramebufferAttachment = C.GL_STENCIL_ATTACHMENT
DepthStencilAttachment FramebufferAttachment = C.GL_DEPTH_STENCIL_ATTACHMENT
NoAttachment FramebufferAttachment = C.GL_NONE
)
////////////////////////////////////////////////////////////////////////////////
func (fb Framebuffer) ReadBuffer(a FramebufferAttachment) {
C.FramebufferReadBuffer(fb.object, C.GLenum(a))
}
func (fb Framebuffer) DrawBuffer(a FramebufferAttachment) {
C.FramebufferDrawBuffer(fb.object, C.GLenum(a))
}
func (fb Framebuffer) DrawBuffers(a []FramebufferAttachment) {
C.FramebufferDrawBuffers(fb.object, C.GLsizei(len(a)), (*C.GLenum)(&a[0]))
}
////////////////////////////////////////////////////////////////////////////////
func (fb Framebuffer) Bind(t FramebufferTarget) {
C.FramebufferBind(fb.object, C.GLenum(t))
}
type FramebufferTarget C.GLenum
const (
DrawFramebuffer FramebufferTarget = C.GL_DRAW_FRAMEBUFFER
ReadFramebuffer FramebufferTarget = C.GL_READ_FRAMEBUFFER
DrawReadFramebuffer FramebufferTarget = C.GL_FRAMEBUFFER
)
////////////////////////////////////////////////////////////////////////////////
func (fb Framebuffer) CheckStatus(t FramebufferTarget) FramebufferStatus {
e := C.FramebufferCheckStatus(fb.object, C.GLenum(t))
return FramebufferStatus(e)
}
type FramebufferStatus C.GLenum
const (
FramebufferComplete FramebufferStatus = C.GL_FRAMEBUFFER_COMPLETE
FramebufferUndefined FramebufferStatus = C.GL_FRAMEBUFFER_UNDEFINED
FramebufferIncompleteAttachment FramebufferStatus = C.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
FramebufferIncompleteMissingAttachment FramebufferStatus = C.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
FramebufferIncompleteDrawBuffer FramebufferStatus = C.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER
FramebufferIncompleteReadBuffer FramebufferStatus = C.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER
FramebufferUnsupported FramebufferStatus = C.GL_FRAMEBUFFER_UNSUPPORTED
FramebufferIncompleteMultisample FramebufferStatus = C.GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
FramebufferIncompleteLayerTargets FramebufferStatus = C.GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
)
func (fbs FramebufferStatus) String() string {
switch fbs {
case FramebufferStatus(0):
return "framebuffer check status error"
case FramebufferComplete:
return "framebuffer complete"
case FramebufferUndefined:
return "framebuffer undefined"
case FramebufferIncompleteAttachment:
return "framebuffer incomplete attachment"
case FramebufferIncompleteMissingAttachment:
return "framebuffer incomplete missing attachment"
case FramebufferIncompleteDrawBuffer:
return "framebuffer incomplete draw buffer"
case FramebufferIncompleteReadBuffer:
return "framebuffer incomplete read buffer"
case FramebufferUnsupported:
return "framebuffer unsupported"
case FramebufferIncompleteMultisample:
return "framebuffer incomplete multisample"
case FramebufferIncompleteLayerTargets:
return "framebuffer incomplete layer targets"
}
return "(unknown framebuffer status: " + strconv.Itoa(int(fbs)) + ")"
}
////////////////////////////////////////////////////////////////////////////////
func (fb Framebuffer) ClearColor(drawbuffer int32, r, g, b, a float32) {
//TODO: other variants
var c struct{ R, G, B, A float32 }
c.R = r
c.G = g
c.B = b
c.A = a
C.FramebufferClearColor(fb.object, C.GLint(drawbuffer), (*C.GLfloat)(unsafe.Pointer(&c)))
}
func (fb Framebuffer) ClearColorUint(r, g, b, a uint32) {
//TODO: other variants
var c struct{ R, G, B, A uint32 }
c.R = r
c.G = g
c.B = b
c.A = a
C.FramebufferClearColorUint(fb.object, (*C.GLuint)(unsafe.Pointer(&c)))
}
func (fb Framebuffer) ClearDepth(d float32) {
C.FramebufferClearDepth(fb.object, (C.GLfloat)(d))
}
////////////////////////////////////////////////////////////////////////////////
func (fb Framebuffer) Blit(dst Framebuffer, srcX1, srcY1, srcX2, srcY2, dstX1, dstY1, dstX2, dstY2 int32, m BufferMask, f FilterMode) {
C.FramebufferBlit(
fb.object,
dst.object,
C.GLint(srcX1), C.GLint(srcY1), C.GLint(srcX2), C.GLint(srcY2),
C.GLint(dstX1), C.GLint(dstY1), C.GLint(dstX2), C.GLint(dstY2),
C.GLbitfield(m),
C.GLenum(f),
)
}
type BufferMask C.GLbitfield
const (
ColorBufferBit BufferMask = C.GL_COLOR_BUFFER_BIT
DepthBufferBit BufferMask = C.GL_DEPTH_BUFFER_BIT
StencilBufferBit BufferMask = C.GL_STENCIL_BUFFER_BIT
)
////////////////////////////////////////////////////////////////////////////////
func (fb Framebuffer) Delete() {
C.FramebufferDelete(fb.object)
}
////////////////////////////////////////////////////////////////////////////////