-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback.rb
258 lines (210 loc) · 8.47 KB
/
feedback.rb
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
257
258
require_relative './lib/application'
require_relative './lib/utils'
require_relative './lib/data'
require 'optimist'
require 'rmath3d/rmath3d'
class Feedback
include Logging
def initialize(window)
@window = window
@name = 'feedback'
end
end
class Calculate < Feedback
# this is named 'draw' for consistency,
# even though it doesn't actually draw anything!
def draw
vertex_source = File.join('shaders', @name, 'vertexShader.glsl')
geometry_source = File.join('shaders', @name, 'geoShader.glsl')
@shader_program = Utils::ShaderProgram.new
@shader_program.load_and_attach(:vertex, File.open(vertex_source, 'r', &:read))
@shader_program.load_and_attach(:geometry, File.open(geometry_source, 'r', &:read))
varying_ptr = ['outValue'].pack('p')
glTransformFeedbackVaryings(@shader_program.id, 1, varying_ptr, GL_INTERLEAVED_ATTRIBS)
@shader_program.link
@shader_program.use
@vao = Utils::VertexArray.new
@vao.bind
data = [1.0, 2.0, 3.0, 4.0, 5.0]
@vbo = Utils::VertexBuffer.new
@vbo.bind
@vbo.load_buffer(data, :float)
@shader_program.enable_vertex_attrib('inValue', 1, :float, 0)
# Create transform buffer
@tbo = Utils::VertexBuffer.new
@tbo.bind
@data_size = @tbo.fiddle_type(:float) * data.length * 3
glBufferData(GL_ARRAY_BUFFER, @data_size, Utils::NullPtr, GL_STATIC_READ)
# Throw away the rasterizer, we don't need visual output
glEnable(GL_RASTERIZER_DISCARD)
# Create our query buffer
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, @tbo.id)
buf = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
glGenQueries(1, buf)
query = buf[0, Fiddle::SIZEOF_INT].unpack('L')[0]
# must be done BEFORE transform feedback
# Other useful queries: GL_PRIMITIVES_GENERATED, GL_TIME_ELAPSED, etc.
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, query)
# unique to feedback shaders
glBeginTransformFeedback(GL_TRIANGLES)
glDrawArrays(GL_POINTS, 0, 5)
glEndTransformFeedback()
# Must stop the query AFTER transform feedback
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN)
glFlush()
# get results back
feedback = Fiddle::Pointer.malloc(Fiddle::SIZEOF_FLOAT * 15)
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER,
Utils::NullPtr,
(15 * Fiddle::SIZEOF_FLOAT),
feedback)
response = feedback[0, @data_size].unpack('F*')
response.each { |n| puts n }
# get query results
prim_buf = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
glGetQueryObjectuiv(query, GL_QUERY_RESULT, prim_buf)
primitives = prim_buf[0, Fiddle::SIZEOF_INT].unpack('L*')[0]
puts "#{primitives} primitives written!"
end
end
class Gravity < Feedback
def initialize(window)
super(window)
@previous_frame_time = 0
@frame_time = 0
end
def draw
@running = true
vertex_source = File.join('shaders', @name, 'vertexShader_gravity.glsl')
frag_source = File.join('shaders', @name, 'fragmentShader.glsl')
@shader_program = Utils::ShaderProgram.new
@shader_program.load_from(vertex: File.open(vertex_source, 'r', &:read),
fragment: File.open(frag_source, 'r', &:read))
varying_ptr = %w[outPosition outVelocity].pack('p*')
glTransformFeedbackVaryings(@shader_program.id, 2, varying_ptr, GL_INTERLEAVED_ATTRIBS)
@shader_program.link
@shader_program.use
uniform_time = @shader_program.uniform_location('time')
uniform_mouse_pos = @shader_program.uniform_location('mousePos')
@vao = Utils::VertexArray.new
@vao.bind
# Vertex format: 6 floats per vertex:
# pos.x pox.y vel.x vel.y origPos.x origPos.y
@data = Array.new(600) { 0.0 }
10.times do |y|
# OPTIMIZE: should probably use .each_cons(4) or .each_slice(4)
10.times do |x|
@data[60 * y + 6 * x] = 0.2 * x - 0.9
@data[60 * y + 6 * x + 1] = 0.2 * y - 0.9
@data[60 * y + 6 * x + 4] = 0.2 * x - 0.9
@data[60 * y + 6 * x + 5] = 0.2 * y - 0.9
end
end
@data_size = Fiddle::SIZEOF_FLOAT * @data.length
@data_ptr = Fiddle::Pointer[@data.pack('F*')]
@vbo = Utils::VertexBuffer.new
@vbo.bind
@vbo.load_buffer(@data, :float, GL_STREAM_DRAW)
@shader_program.enable_vertex_attrib('position', 2, :float, 6)
@shader_program.enable_vertex_attrib('velocity', 2, :float, 6, 2)
@shader_program.enable_vertex_attrib('originalPos', 2, :float, 6, 4)
# Create transform feedback buffer
@tbo = Utils::VertexBuffer.new
@tbo.bind
glBufferData(GL_ARRAY_BUFFER, Fiddle::SIZEOF_FLOAT * 400, Utils::NullPtr, GL_STATIC_READ)
# Set the transform feedback buffer as our base
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, @tbo.id)
# We will receive 400 items back from the feedback (4 items per row * 100 rows)
@feedback_length = 400
@feedback_size = Fiddle::SIZEOF_FLOAT * @feedback_length
@feedback = Fiddle::Pointer.malloc(@feedback_size)
@vbo.bind
glPointSize(5.0)
previous_time = Time.now
# Set up our variables for tracking the mouse pointer.
# We also warp the mouse into the window to prevent
# any initial behavior with nonsense coordinates
mouse_x = @window.width / 2
mouse_y = @window.height / 2
SDL2::Mouse::Cursor.warp(@window.window, mouse_x, mouse_y)
while @running
event = SDL2::Event.poll
case event
when SDL2::Event::Quit
@running = false
when SDL2::Event::KeyUp
case event.sym
when SDL2::Key::ESCAPE, SDL2::Key::Q
@running = false
end
when SDL2::Event::MouseMotion
mouse_x = event.x
mouse_y = event.y
end
# Render
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
now = Time.now
time = (now - previous_time)
previous_time = now
glUniform1f(uniform_time, time)
# Send updated mouse position to shader
glUniform2f(uniform_mouse_pos, mouse_x / 400.0 - 1, -mouse_y / 400.0 + 1)
# Setup a query for rendering details.
buf = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
glGenQueries(1, buf)
query = buf[0, Fiddle::SIZEOF_INT].unpack('L')[0]
glBeginQuery(GL_TIME_ELAPSED, query)
# Unique to feedback shaders
glBeginTransformFeedback(GL_POINTS)
# Draw our gravity-influenced points
glDrawArrays(GL_POINTS, 0, 100)
glEndTransformFeedback()
glEndQuery(GL_TIME_ELAPSED)
@window.window.gl_swap
# Get our updated coordinates from the feedback buffer
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, @feedback_size, @feedback)
response = @feedback[0, @feedback_size].unpack('F*')
# each "row" is 4: 100.times {4} = 400
# OPTIMIZE: should probably use .each_cons(4) or .each_slice(4)
100.times do |i|
@data[6 * i] = response[4 * i]
@data[6 * i + 1] = response[4 * i + 1]
@data[6 * i + 2] = response[4 * i + 2]
@data[6 * i + 3] = response[4 * i + 3]
end
# glBufferData reallocates the whole vertex data buffer,
# so we use this call to just update the existing buffer
glBufferSubData(GL_ARRAY_BUFFER, 0, @data_size, Fiddle::Pointer[@data.pack('F*')])
# get time elapsed from query
time_buf = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
glGetQueryObjectuiv(query, GL_QUERY_RESULT, time_buf)
time_elapsed = time_buf[0, Fiddle::SIZEOF_INT].unpack('L*')[0]
frame_average = (time_elapsed + @previous_frame_time) / 2
@previous_frame_time = @frame_time
@frame_time = frame_average
end
puts "Average per-frame time: #{@frame_time} ns"
end
end
examples = %w[calculate gravity]
opts = Optimist.options do
opt :size, 'width X height string', default: '800x600'
opt :example, "example to run: #{examples.join(', ')}", default: 'calculate'
opt :verbose, 'say a lot', default: false
end
window_size = Utils.parse_window_size(opts[:size])
Optimist.die('Valid size string is required') unless window_size
Optimist.die('Valid width is required') unless window_size[:width] > 0
Optimist.die('Valid height is required') unless window_size[:height] > 0
Optimist.die("Example must be one of: #{examples}") unless examples.include?(opts[:example])
window = Application.new(window_size[:width],
window_size[:height],
'feedback',
opts[:verbose])
case opts[:example]
when 'calculate'
Calculate.new(window).draw
when 'gravity'
Gravity.new(window).draw
end