1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-06 01:28:57 +00:00

Things are back pretty much to where they were, but offset is eliminated as an input.

This commit is contained in:
Thomas Harte 2016-05-07 18:37:18 -04:00
parent c73751b15a
commit 8e4ab37719
9 changed files with 110 additions and 55 deletions

View File

@ -36,7 +36,13 @@ class MachineDocument: NSDocument, CSOpenGLViewDelegate, CSOpenGLViewResponderDe
private var cycleCountError: Int64 = 0
private var lastTime: CVTimeStamp?
private var skippedFrames = 0
// private var frameSkip = 0
final func openGLView(view: CSOpenGLView, didUpdateToTime time: CVTimeStamp, didSkipPreviousUpdate : Bool, frequency : Double) {
// frameSkip = frameSkip + 1
// let modFrameSkip = frameSkip % 10
// if modFrameSkip == 0 {
// runForNumberOfCycles(Int32(intendedCyclesPerSecond / 60))
// }
if let lastTime = lastTime {
// perform (time passed in seconds) * (intended cycles per second), converting and
// maintaining an error count to deal with underflow

View File

@ -104,9 +104,8 @@ Flywheel::SyncEvent CRT::get_next_horizontal_sync_event(bool hsync_is_requested,
#define source_input_position_y(v) (*(uint16_t *)&next_run[SourceVertexSize*v + SourceVertexOffsetOfInputPosition + 2])
#define source_output_position_x(v) (*(uint16_t *)&next_run[SourceVertexSize*v + SourceVertexOffsetOfOutputPosition + 0])
#define source_output_position_y(v) (*(uint16_t *)&next_run[SourceVertexSize*v + SourceVertexOffsetOfOutputPosition + 2])
#define source_phase(v) next_run[SourceVertexSize*v + SourceVertexOffsetOfPhaseAmplitudeAndOffset + 0]
#define source_amplitude(v) next_run[SourceVertexSize*v + SourceVertexOffsetOfPhaseAmplitudeAndOffset + 1]
#define source_offset(v) next_run[SourceVertexSize*v + SourceVertexOffsetOfPhaseAmplitudeAndOffset + 2]
#define source_phase(v) next_run[SourceVertexSize*v + SourceVertexOffsetOfPhaseAndAmplitude + 0]
#define source_amplitude(v) next_run[SourceVertexSize*v + SourceVertexOffsetOfPhaseAndAmplitude + 1]
#define source_phase_time(v) (*(uint16_t *)&next_run[SourceVertexSize*v + SourceVertexOffsetOfPhaseTime])
void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divider, bool hsync_requested, bool vsync_requested, const bool vsync_charging, const Scan::Type type, uint16_t tex_x, uint16_t tex_y)
@ -142,6 +141,7 @@ void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divi
// [0/1] 3
if(next_run)
{
// printf("%d -> %d", tex_y, _openGL_output_builder->get_composite_output_y());
source_input_position_x(0) = tex_x;
source_input_position_y(0) = source_input_position_y(1) = tex_y;
source_output_position_x(0) = (uint16_t)_horizontal_flywheel->get_current_output_position();
@ -149,8 +149,6 @@ void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divi
source_phase(0) = source_phase(1) = _colour_burst_phase;
source_amplitude(0) = source_amplitude(1) = _colour_burst_amplitude;
source_phase_time(0) = source_phase_time(1) = _colour_burst_time;
source_offset(0) = 0;
source_offset(1) = 255;
}
// decrement the number of cycles left to run for and increment the
@ -188,16 +186,23 @@ void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divi
if(needs_endpoint)
{
uint8_t *next_run = _openGL_output_builder->get_next_output_run();
if(next_run)
if(
_openGL_output_builder->composite_output_run_has_room_for_vertices(_did_start_run ? 6 : 3) &&
!_openGL_output_builder->composite_output_buffer_is_full() &&
_is_writing_composite_run == _did_start_run)
{
output_position_x(0) = output_position_x(1) = output_position_x(2) = (uint16_t)_horizontal_flywheel->get_current_output_position();
output_position_y(0) = output_position_y(1) = output_position_y(2) = (uint16_t)(_vertical_flywheel->get_current_output_position() / _vertical_flywheel_output_divider);
output_tex_x(0) = output_tex_x(1) = output_tex_x(2) = (uint16_t)_horizontal_flywheel->get_current_output_position();
output_tex_y(0) = output_tex_y(1) = output_tex_y(2) = _openGL_output_builder->get_composite_output_y();
uint8_t *next_run = _openGL_output_builder->get_next_output_run();
if(next_run)
{
// printf("{%d -> %0.2f}", _openGL_output_builder->get_composite_output_y(), (float)_vertical_flywheel->get_current_output_position() / (float)_vertical_flywheel->get_scan_period());
output_position_x(0) = output_position_x(1) = output_position_x(2) = (uint16_t)_horizontal_flywheel->get_current_output_position();
output_position_y(0) = output_position_y(1) = output_position_y(2) = (uint16_t)(_vertical_flywheel->get_current_output_position() / _vertical_flywheel_output_divider);
output_tex_x(0) = output_tex_x(1) = output_tex_x(2) = (uint16_t)_horizontal_flywheel->get_current_output_position();
output_tex_y(0) = output_tex_y(1) = output_tex_y(2) = _openGL_output_builder->get_composite_output_y();
_openGL_output_builder->complete_output_run(3);
_openGL_output_builder->complete_output_run(3);
_did_start_run ^= true;
}
}
_is_writing_composite_run ^= true;
}
@ -210,6 +215,10 @@ void CRT::advance_cycles(unsigned int number_of_cycles, unsigned int source_divi
// if this is vertical retrace then adcance a field
if(next_run_length == time_until_vertical_sync_event && next_vertical_sync_event == Flywheel::SyncEvent::EndRetrace)
{
// printf("\n!%d!\n", _openGL_output_builder->get_composite_output_y());
// TODO: eliminate the below; it's to aid with debug, aligning the top of the
// input buffer with the top of the incoming frame.
_openGL_output_builder->release_source_buffer_write_pointer();
if(_delegate)
{
_frames_since_last_delegate_call++;
@ -317,6 +326,7 @@ void CRT::output_data(unsigned int number_of_cycles, unsigned int source_divider
{
if(_openGL_output_builder->reduce_previous_allocation_to(number_of_cycles / source_divider))
{
// printf("[%d with %d]", (_vertical_flywheel->get_current_time() * 262) / _vertical_flywheel->get_scan_period(), _openGL_output_builder->get_last_write_y_posititon());
Scan scan {
.type = Scan::Type::Data,
.number_of_cycles = number_of_cycles,

View File

@ -68,7 +68,7 @@ class CRT {
uint8_t _colour_burst_phase, _colour_burst_amplitude;
uint16_t _colour_burst_time;
bool _is_writing_composite_run;
bool _is_writing_composite_run, _did_start_run;
// the outer entry point for dispatching output_sync, output_blank, output_level and output_data
void advance_cycles(unsigned int number_of_cycles, unsigned int source_divider, bool hsync_requested, bool vsync_requested, const bool vsync_charging, const Scan::Type type, uint16_t tex_x, uint16_t tex_y);

View File

@ -26,14 +26,14 @@ const GLsizei OutputVertexSize = 8;
// remapping occurs to ensure a continous stream of data for each scan, giving correct out-of-bounds behaviour
const GLsizei SourceVertexOffsetOfInputPosition = 0;
const GLsizei SourceVertexOffsetOfOutputPosition = 4;
const GLsizei SourceVertexOffsetOfPhaseAmplitudeAndOffset = 8;
const GLsizei SourceVertexOffsetOfPhaseAndAmplitude = 8;
const GLsizei SourceVertexOffsetOfPhaseTime = 12;
const GLsizei SourceVertexSize = 16;
// These constants hold the size of the rolling buffer to which the CPU writes
const GLsizei InputBufferBuilderWidth = 2048;
const GLsizei InputBufferBuilderHeight = 1024;
const GLsizei InputBufferBuilderHeight = 1024; // 1024
// This is the size of the intermediate buffers used during composite to RGB conversion
const GLsizei IntermediateBufferWidth = 2048;

View File

@ -16,7 +16,8 @@ CRTInputBufferBuilder::CRTInputBufferBuilder(size_t bytes_per_pixel) :
_bytes_per_pixel(bytes_per_pixel),
_next_write_x_position(0),
_next_write_y_position(0),
_image(new uint8_t[bytes_per_pixel * InputBufferBuilderWidth * InputBufferBuilderHeight])
_image(new uint8_t[bytes_per_pixel * InputBufferBuilderWidth * InputBufferBuilderHeight]),
_should_reset(false)
{}
CRTInputBufferBuilder::~CRTInputBufferBuilder()
@ -30,7 +31,7 @@ void CRTInputBufferBuilder::allocate_write_area(size_t required_length)
{
_last_allocation_amount = required_length;
// if(_next_write_x_position + required_length + 2 > InputBufferBuilderWidth)
if(_next_write_x_position + required_length + 2 > InputBufferBuilderWidth)
{
_next_write_x_position = 0;
_next_write_y_position++;
@ -41,13 +42,17 @@ void CRTInputBufferBuilder::allocate_write_area(size_t required_length)
_write_x_position = _next_write_x_position + 1;
_write_y_position = _next_write_y_position;
// printf("#%d,%d#", _write_x_position, _write_y_position);
_write_target_pointer = (_write_y_position * InputBufferBuilderWidth) + _write_x_position;
_next_write_x_position += required_length + 2;
}
}
if(_write_y_position == 0 && _write_x_position == 0)
{
memset(_image, 0, _bytes_per_pixel * InputBufferBuilderWidth * InputBufferBuilderHeight);
}
void CRTInputBufferBuilder::release_write_pointer()
{
if(_should_reset)
{
_next_write_x_position = _next_write_y_position = 0;
}
}
@ -78,8 +83,9 @@ uint8_t *CRTInputBufferBuilder::get_image_pointer()
uint16_t CRTInputBufferBuilder::get_and_finalise_current_line()
{
// TODO: we may have a vended allocate_write_area in play that'll be lost by this step; fix.
uint16_t result = _write_y_position + (_next_write_x_position ? 1 : 0);
_next_write_x_position = _next_write_y_position = 0;
_should_reset = (_next_write_y_position == InputBufferBuilderHeight);
return result;
}

View File

@ -27,6 +27,7 @@ struct CRTInputBufferBuilder {
bool reduce_previous_allocation_to(size_t actual_length);
uint16_t get_and_finalise_current_line();
void release_write_pointer();
uint8_t *get_image_pointer();
uint8_t *get_write_target();
@ -53,6 +54,8 @@ struct CRTInputBufferBuilder {
// the buffer
uint8_t *_image;
bool _should_reset;
};
}

View File

@ -173,10 +173,10 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
// This should return either an actual framebuffer number, if this is a target with a framebuffer intended for output,
// or 0 if no framebuffer is bound, in which case 0 is also what we want to supply to bind the implied framebuffer. So
// it works either way.
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint *)&defaultFramebuffer);
// glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint *)&defaultFramebuffer);
// TODO: is this sustainable, cross-platform? If so, why store it at all?
defaultFramebuffer = 0;
// defaultFramebuffer = 0;
}
// determine how many lines are newly reclaimed; they'll need to be cleared
@ -186,6 +186,14 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
int number_of_source_drawing_zones = getCircularRanges(_drawn_source_buffer_data_pointer, _source_buffer_data_pointer, SourceVertexBufferDataSize, 2*SourceVertexSize, source_drawing_zones);
int number_of_output_drawing_zones = getCircularRanges(_drawn_output_buffer_data_pointer, _output_buffer_data_pointer, OutputVertexBufferDataSize, 6*OutputVertexSize, output_drawing_zones);
// for(int c = 0; c < number_of_output_drawing_zones; c++)
// {
// printf("\n(%d->%d)\n", output_drawing_zones[c*2], output_drawing_zones[c*2] + output_drawing_zones[c*2 + 1]);
// }
// if(number_of_output_drawing_zones)
// printf("\n\n");
uint16_t completed_texture_y = _buffer_builder->get_and_finalise_current_line();
_composite_src_output_y %= IntermediateBufferHeight;
@ -279,18 +287,17 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
active_pipeline->shader->bind();
// clear as desired
// if(number_of_clearing_zones)
// {
// glEnable(GL_SCISSOR_TEST);
// glClearColor(active_pipeline->clear_colour[0], active_pipeline->clear_colour[1], active_pipeline->clear_colour[2], 1.0);
// for(int c = 0; c < number_of_clearing_zones; c++)
// {
// glScissor(0, clearing_zones[c*2], IntermediateBufferWidth, clearing_zones[c*2 + 1]);
// glClear(GL_COLOR_BUFFER_BIT);
// }
// glDisable(GL_SCISSOR_TEST);
// }
glClear(GL_COLOR_BUFFER_BIT);
if(number_of_clearing_zones)
{
glEnable(GL_SCISSOR_TEST);
glClearColor(active_pipeline->clear_colour[0], active_pipeline->clear_colour[1], active_pipeline->clear_colour[2], 1.0);
for(int c = 0; c < number_of_clearing_zones; c++)
{
glScissor(0, clearing_zones[c*2], IntermediateBufferWidth, clearing_zones[c*2 + 1]);
glClear(GL_COLOR_BUFFER_BIT);
}
glDisable(GL_SCISSOR_TEST);
}
// draw as desired
for(int c = 0; c < number_of_source_drawing_zones; c++)
@ -305,8 +312,11 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
// transfer to framebuffer
framebuffer->bind_framebuffer();
if(number_of_output_drawing_zones)
{
// glClearColor(0.5, 0.5, 0.5, 1.0);
// glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
// Ensure we're back on the output framebuffer, drawing from the output array buffer
@ -326,6 +336,7 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
{
glDrawArrays(GL_TRIANGLE_STRIP, output_drawing_zones[c*2] / OutputVertexSize, output_drawing_zones[c*2 + 1] / OutputVertexSize);
}
// glDrawArrays(GL_TRIANGLE_STRIP, 0, OutputVertexBufferDataSize / OutputVertexSize);
}
// copy framebuffer to the intended place
@ -333,8 +344,9 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, (GLsizei)output_width, (GLsizei)output_height);
glClear(GL_COLOR_BUFFER_BIT);
framebuffer->draw((float)output_width / (float)output_height);
// compositeTexture->draw((float)output_width / (float)output_height);
// filteredYTexture->draw((float)output_width / (float)output_height);
// drawing commands having been issued, reclaim the array buffer pointer
glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer);
@ -399,24 +411,24 @@ void OpenGLOutputBuilder::prepare_source_vertex_array()
{
if(composite_input_shader_program)
{
GLint inputPositionAttribute = composite_input_shader_program->get_attrib_location("inputPosition");
GLint outputPositionAttribute = composite_input_shader_program->get_attrib_location("outputPosition");
GLint phaseAmplitudeAndOffsetAttribute = composite_input_shader_program->get_attrib_location("phaseAmplitudeAndOffset");
GLint phaseTimeAttribute = composite_input_shader_program->get_attrib_location("phaseTime");
GLint inputPositionAttribute = composite_input_shader_program->get_attrib_location("inputPosition");
GLint outputPositionAttribute = composite_input_shader_program->get_attrib_location("outputPosition");
GLint phaseAndAmplitudeAttribute = composite_input_shader_program->get_attrib_location("phaseAndAmplitude");
GLint phaseTimeAttribute = composite_input_shader_program->get_attrib_location("phaseTime");
glBindVertexArray(source_vertex_array);
glEnableVertexAttribArray((GLuint)inputPositionAttribute);
glEnableVertexAttribArray((GLuint)outputPositionAttribute);
glEnableVertexAttribArray((GLuint)phaseAmplitudeAndOffsetAttribute);
glEnableVertexAttribArray((GLuint)phaseAndAmplitudeAttribute);
glEnableVertexAttribArray((GLuint)phaseTimeAttribute);
const GLsizei vertexStride = SourceVertexSize;
glBindBuffer(GL_ARRAY_BUFFER, source_array_buffer);
glVertexAttribPointer((GLuint)inputPositionAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfInputPosition);
glVertexAttribPointer((GLuint)outputPositionAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfOutputPosition);
glVertexAttribPointer((GLuint)phaseAmplitudeAndOffsetAttribute, 3, GL_UNSIGNED_BYTE, GL_TRUE, vertexStride, (void *)SourceVertexOffsetOfPhaseAmplitudeAndOffset);
glVertexAttribPointer((GLuint)phaseTimeAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfPhaseTime);
glVertexAttribPointer((GLuint)inputPositionAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfInputPosition);
glVertexAttribPointer((GLuint)outputPositionAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfOutputPosition);
glVertexAttribPointer((GLuint)phaseAndAmplitudeAttribute, 2, GL_UNSIGNED_BYTE, GL_TRUE, vertexStride, (void *)SourceVertexOffsetOfPhaseAndAmplitude);
glVertexAttribPointer((GLuint)phaseTimeAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)SourceVertexOffsetOfPhaseTime);
}
}

View File

@ -117,10 +117,16 @@ class OpenGLOutputBuilder {
_output_mutex->unlock();
}
inline bool composite_output_run_has_room_for_vertices(GLsizei vertices_to_write)
{
return _composite_src_output_y <= _cleared_composite_output_y + IntermediateBufferHeight - vertices_to_write * OutputVertexSize;
}
inline uint8_t *get_next_output_run()
{
if(_output_buffer_data_pointer == _drawn_output_buffer_data_pointer + OutputVertexBufferDataSize) return nullptr;
_output_mutex->lock();
// printf("{%ld}", _output_buffer_data_pointer % OutputVertexBufferDataSize);
return &_output_buffer_data[_output_buffer_data_pointer % OutputVertexBufferDataSize];
}
@ -135,9 +141,9 @@ class OpenGLOutputBuilder {
return _output_device;
}
inline bool composite_output_buffer_is_full()
inline bool composite_output_buffer_has_room_for_vertices(GLsizei vertices_to_write)
{
return _composite_src_output_y == _cleared_composite_output_y + IntermediateBufferHeight;
return _composite_src_output_y <= _cleared_composite_output_y + IntermediateBufferHeight - vertices_to_write * OutputVertexSize;
}
inline uint16_t get_composite_output_y()
@ -145,9 +151,15 @@ class OpenGLOutputBuilder {
return _composite_src_output_y % IntermediateBufferHeight;
}
inline bool composite_output_buffer_is_full()
{
return _composite_src_output_y == _cleared_composite_output_y + IntermediateBufferHeight;
}
inline void increment_composite_output_y()
{
_composite_src_output_y++;
if(!composite_output_buffer_is_full())
_composite_src_output_y++;
}
inline uint8_t *allocate_write_area(size_t required_length)
@ -171,6 +183,11 @@ class OpenGLOutputBuilder {
return _buffer_builder->get_last_write_y_position();
}
inline void release_source_buffer_write_pointer()
{
_buffer_builder->release_write_pointer();
}
void draw_frame(unsigned int output_width, unsigned int output_height, bool only_if_dirty);
void set_openGL_context_will_change(bool should_delete_resources);
void set_composite_sampling_function(const char *shader);

View File

@ -20,7 +20,7 @@ namespace {
{
{"inputPosition", 0},
{"outputPosition", 1},
{"phaseAmplitudeAndOffset", 2},
{"phaseAndAmplitude", 2},
{"phaseTime", 3},
{nullptr}
};
@ -37,7 +37,7 @@ std::unique_ptr<IntermediateShader> IntermediateShader::make_shader(const char *
"in vec2 inputPosition;"
"in vec2 outputPosition;"
"in vec3 phaseAmplitudeAndOffset;"
"in vec2 phaseAndAmplitude;"
"in float phaseTime;"
"uniform float phaseCyclesPerTick;"
@ -53,7 +53,8 @@ std::unique_ptr<IntermediateShader> IntermediateShader::make_shader(const char *
"void main(void)"
"{"
"vec2 extensionVector = vec2(extension, 0.0) * 2.0 * (phaseAmplitudeAndOffset.z - 0.5);"
"float direction = float(gl_VertexID & 1);"
"vec2 extensionVector = vec2(extension, 0.0) * 2.0 * (direction - 0.5);"
"vec2 extendedInputPosition = %s + extensionVector;"
"vec2 extendedOutputPosition = outputPosition + extensionVector;"
@ -74,8 +75,8 @@ std::unique_ptr<IntermediateShader> IntermediateShader::make_shader(const char *
"inputPositionsVarying[10] = mappedInputPosition + (vec2(offsets[0], 0.0) / textureSize);"
"delayLinePositionVarying = mappedInputPosition - vec2(0.0, 1.0);"
"phaseAndAmplitudeVarying.x = (phaseCyclesPerTick * (extendedOutputPosition.x - phaseTime) + phaseAmplitudeAndOffset.x) * 2.0 * 3.141592654;"
"phaseAndAmplitudeVarying.y = 0.33;"
"phaseAndAmplitudeVarying.x = (phaseCyclesPerTick * (extendedOutputPosition.x - phaseTime) + phaseAndAmplitude.x) * 2.0 * 3.141592654;"
"phaseAndAmplitudeVarying.y = 0.33;" // TODO: reinstate connection with phaseAndAmplitude
"vec2 eyePosition = 2.0*(extendedOutputPosition / outputTextureSize) - vec2(1.0) + vec2(0.5)/textureSize;"
"gl_Position = vec4(eyePosition, 0.0, 1.0);"