diff options
| -rw-r--r-- | sokol_app.h | 57 | ||||
| -rw-r--r-- | sokol_gfx.h | 176 |
2 files changed, 116 insertions, 117 deletions
diff --git a/sokol_app.h b/sokol_app.h index b2ed70f0..cc56b136 100644 --- a/sokol_app.h +++ b/sokol_app.h @@ -3749,10 +3749,10 @@ _SOKOL_PRIVATE bool _sapp_macos_create_window(_sapp_window_t* win) { attrs[i++] = NSOpenGLPFAAlphaSize; attrs[i++] = 8; attrs[i++] = NSOpenGLPFADepthSize; attrs[i++] = 24; attrs[i++] = NSOpenGLPFAStencilSize; attrs[i++] = 8; - if (_sapp.sample_count > 1) { + if (win->desc.sample_count > 1) { attrs[i++] = NSOpenGLPFAMultisample; attrs[i++] = NSOpenGLPFASampleBuffers; attrs[i++] = 1; - attrs[i++] = NSOpenGLPFASamples; attrs[i++] = (NSOpenGLPixelFormatAttribute)_sapp.sample_count; + attrs[i++] = NSOpenGLPFASamples; attrs[i++] = (NSOpenGLPixelFormatAttribute)_sapp.desc.sample_count; } else { attrs[i++] = NSOpenGLPFASampleBuffers; attrs[i++] = 0; @@ -3761,28 +3761,21 @@ _SOKOL_PRIVATE bool _sapp_macos_create_window(_sapp_window_t* win) { NSOpenGLPixelFormat* glpixelformat_obj = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]; SOKOL_ASSERT(glpixelformat_obj != nil); - _sapp.macos.view = [[_sapp_macos_view alloc] - initWithFrame:window_rect + win->macos.view = [[_sapp_macos_view alloc] + initWithFrame:content_rect pixelFormat:glpixelformat_obj]; _SAPP_OBJC_RELEASE(glpixelformat_obj); - [_sapp.macos.view updateTrackingAreas]; - if (_sapp.desc.high_dpi) { - [_sapp.macos.view setWantsBestResolutionOpenGLSurface:YES]; + win->macos.view.win_id = win->slot.id; + [win->macos.view updateTrackingAreas]; + if (win->desc.high_dpi) { + [win->macos.view setWantsBestResolutionOpenGLSurface:YES]; } else { - [_sapp.macos.view setWantsBestResolutionOpenGLSurface:NO]; + [win->macos.view setWantsBestResolutionOpenGLSurface:NO]; } - _sapp.macos.window.contentView = _sapp.macos.view; - [_sapp.macos.window makeFirstResponder:_sapp.macos.view]; - - NSTimer* timer_obj = [NSTimer timerWithTimeInterval:0.001 - target:_sapp.macos.view - selector:@selector(timerFired:) - userInfo:nil - repeats:YES]; - [[NSRunLoop currentRunLoop] addTimer:timer_obj forMode:NSDefaultRunLoopMode]; - timer_obj = nil; + win->macos.window.contentView = win->macos.view; + [win->macos.window makeFirstResponder:win->macos.view]; #endif if (win->fullscreen) { /* on GL, this already toggles a rendered frame, so set the valid flag before */ @@ -3996,7 +3989,8 @@ _SOKOL_PRIVATE void _sapp_macos_update_dimensions(_sapp_window_t* win) { cur_fb_width = (int) fb_size.width; cur_fb_height = (int) fb_size.height; #else - #error "FIXME GL!" + cur_fb_width = (int) fb_rect.size.width; + cur_fb_height = (int) fb_rect.size.height; #endif const bool dim_changed = (win->framebuffer_width != cur_fb_width) || (win->framebuffer_height != cur_fb_height); @@ -4005,7 +3999,7 @@ _SOKOL_PRIVATE void _sapp_macos_update_dimensions(_sapp_window_t* win) { CGSize drawable_size = { (CGFloat) win->framebuffer_width, (CGFloat) win->framebuffer_height }; win->macos.view.drawableSize = drawable_size; #else - #error "FIXME GL!" + // nothing to do here? #endif if (!_sapp.first_frame) { _sapp_macos_app_event(win, SAPP_EVENTTYPE_RESIZED); @@ -4177,7 +4171,7 @@ _SOKOL_PRIVATE CVReturn _sapp_macos_displaylink_callback( #if defined(SOKOL_METAL) [win->macos.view draw]; #else - #error "FIXME: GL" + [[win->macos.view openGLContext] flushBuffer]; #endif } } @@ -4397,24 +4391,29 @@ _SOKOL_PRIVATE CVReturn _sapp_macos_displaylink_callback( } [super reshape]; } -- (void)timerFired:(id)sender { - _SOKOL_UNUSED(sender); - [self setNeedsDisplay:YES]; -} - (void)prepareOpenGL { [super prepareOpenGL]; GLint swapInt = 1; - NSOpenGLContext* ctx = [_sapp.macos.view openGLContext]; - [ctx setValues:&swapInt forParameter:NSOpenGLContextParameterSwapInterval]; - [ctx makeCurrentContext]; + _sapp_window_t* win = _sapp_lookup_window(self.win_id); + if (win) { + NSOpenGLContext* ctx = [win->macos.view openGLContext]; + [ctx setValues:&swapInt forParameter:NSOpenGLContextParameterSwapInterval]; + [ctx makeCurrentContext]; + } } #endif - (void)drawRect:(NSRect)rect { _SOKOL_UNUSED(rect); + // FIXME? + /* #if !defined(SOKOL_METAL) - [[_sapp.macos.view openGLContext] flushBuffer]; + _sapp_window_t* win = _sapp_lookup_window(self.win_id); + if (win) { + [[win->macos.view openGLContext] flushBuffer]; + } #endif + */ } - (BOOL)isOpaque { diff --git a/sokol_gfx.h b/sokol_gfx.h index 5167b2d5..4c042dfd 100644 --- a/sokol_gfx.h +++ b/sokol_gfx.h @@ -5980,85 +5980,84 @@ _SOKOL_PRIVATE void _sg_gl_cache_invalidate_program(GLuint prog) { } } -_SOKOL_PRIVATE void _sg_gl_reset_state_cache(void) { - if (_sg.gl.cur_context) { - _SG_GL_CHECK_ERROR(); - #if !defined(SOKOL_GLES2) - if (!_sg.gl.gles2) { - glBindVertexArray(_sg.gl.cur_context->vao); - _SG_GL_CHECK_ERROR(); - } - #endif - memset(&_sg.gl.cache, 0, sizeof(_sg.gl.cache)); - _sg_gl_cache_clear_buffer_bindings(true); - _SG_GL_CHECK_ERROR(); - _sg_gl_cache_clear_texture_bindings(true); +_SOKOL_PRIVATE void _sg_gl_reset_state_cache(_sg_context_t* ctx) { + SOKOL_ASSERT(ctx); + _SG_GL_CHECK_ERROR(); + #if !defined(SOKOL_GLES2) + if (!_sg.gl.gles2) { + glBindVertexArray(ctx->vao); _SG_GL_CHECK_ERROR(); - for (int i = 0; i < _sg.limits.max_vertex_attrs; i++) { - _sg_gl_attr_t* attr = &_sg.gl.cache.attrs[i].gl_attr; - attr->vb_index = -1; - attr->divisor = -1; - glDisableVertexAttribArray((GLuint)i); - _SG_GL_CHECK_ERROR(); - } - _sg.gl.cache.cur_primitive_type = GL_TRIANGLES; - - /* shader program */ - glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&_sg.gl.cache.prog); + } + #endif + memset(&_sg.gl.cache, 0, sizeof(_sg.gl.cache)); + _sg_gl_cache_clear_buffer_bindings(true); + _SG_GL_CHECK_ERROR(); + _sg_gl_cache_clear_texture_bindings(true); + _SG_GL_CHECK_ERROR(); + for (int i = 0; i < _sg.limits.max_vertex_attrs; i++) { + _sg_gl_attr_t* attr = &_sg.gl.cache.attrs[i].gl_attr; + attr->vb_index = -1; + attr->divisor = -1; + glDisableVertexAttribArray((GLuint)i); _SG_GL_CHECK_ERROR(); + } + _sg.gl.cache.cur_primitive_type = GL_TRIANGLES; - /* depth and stencil state */ - _sg.gl.cache.depth.compare = SG_COMPAREFUNC_ALWAYS; - _sg.gl.cache.stencil.front.compare = SG_COMPAREFUNC_ALWAYS; - _sg.gl.cache.stencil.front.fail_op = SG_STENCILOP_KEEP; - _sg.gl.cache.stencil.front.depth_fail_op = SG_STENCILOP_KEEP; - _sg.gl.cache.stencil.front.pass_op = SG_STENCILOP_KEEP; - _sg.gl.cache.stencil.back.compare = SG_COMPAREFUNC_ALWAYS; - _sg.gl.cache.stencil.back.fail_op = SG_STENCILOP_KEEP; - _sg.gl.cache.stencil.back.depth_fail_op = SG_STENCILOP_KEEP; - _sg.gl.cache.stencil.back.pass_op = SG_STENCILOP_KEEP; - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_ALWAYS); - glDepthMask(GL_FALSE); - glDisable(GL_STENCIL_TEST); - glStencilFunc(GL_ALWAYS, 0, 0); - glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - glStencilMask(0); - - /* blend state */ - _sg.gl.cache.blend.src_factor_rgb = SG_BLENDFACTOR_ONE; - _sg.gl.cache.blend.dst_factor_rgb = SG_BLENDFACTOR_ZERO; - _sg.gl.cache.blend.op_rgb = SG_BLENDOP_ADD; - _sg.gl.cache.blend.src_factor_alpha = SG_BLENDFACTOR_ONE; - _sg.gl.cache.blend.dst_factor_alpha = SG_BLENDFACTOR_ZERO; - _sg.gl.cache.blend.op_alpha = SG_BLENDOP_ADD; - glDisable(GL_BLEND); - glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO); - glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD); - glBlendColor(0.0f, 0.0f, 0.0f, 0.0f); + /* shader program */ + glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&_sg.gl.cache.prog); + _SG_GL_CHECK_ERROR(); - /* standalone state */ - for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { - _sg.gl.cache.color_write_mask[i] = SG_COLORMASK_RGBA; - } - _sg.gl.cache.cull_mode = SG_CULLMODE_NONE; - _sg.gl.cache.face_winding = SG_FACEWINDING_CW; - _sg.gl.cache.sample_count = 1; - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - glPolygonOffset(0.0f, 0.0f); - glDisable(GL_POLYGON_OFFSET_FILL); - glDisable(GL_CULL_FACE); - glFrontFace(GL_CW); - glCullFace(GL_BACK); - glEnable(GL_SCISSOR_TEST); - glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); - glEnable(GL_DITHER); - glDisable(GL_POLYGON_OFFSET_FILL); - #if defined(SOKOL_GLCORE33) - glEnable(GL_MULTISAMPLE); - glEnable(GL_PROGRAM_POINT_SIZE); - #endif - } + /* depth and stencil state */ + _sg.gl.cache.depth.compare = SG_COMPAREFUNC_ALWAYS; + _sg.gl.cache.stencil.front.compare = SG_COMPAREFUNC_ALWAYS; + _sg.gl.cache.stencil.front.fail_op = SG_STENCILOP_KEEP; + _sg.gl.cache.stencil.front.depth_fail_op = SG_STENCILOP_KEEP; + _sg.gl.cache.stencil.front.pass_op = SG_STENCILOP_KEEP; + _sg.gl.cache.stencil.back.compare = SG_COMPAREFUNC_ALWAYS; + _sg.gl.cache.stencil.back.fail_op = SG_STENCILOP_KEEP; + _sg.gl.cache.stencil.back.depth_fail_op = SG_STENCILOP_KEEP; + _sg.gl.cache.stencil.back.pass_op = SG_STENCILOP_KEEP; + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_ALWAYS); + glDepthMask(GL_FALSE); + glDisable(GL_STENCIL_TEST); + glStencilFunc(GL_ALWAYS, 0, 0); + glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + glStencilMask(0); + + /* blend state */ + _sg.gl.cache.blend.src_factor_rgb = SG_BLENDFACTOR_ONE; + _sg.gl.cache.blend.dst_factor_rgb = SG_BLENDFACTOR_ZERO; + _sg.gl.cache.blend.op_rgb = SG_BLENDOP_ADD; + _sg.gl.cache.blend.src_factor_alpha = SG_BLENDFACTOR_ONE; + _sg.gl.cache.blend.dst_factor_alpha = SG_BLENDFACTOR_ZERO; + _sg.gl.cache.blend.op_alpha = SG_BLENDOP_ADD; + glDisable(GL_BLEND); + glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO); + glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD); + glBlendColor(0.0f, 0.0f, 0.0f, 0.0f); + + /* standalone state */ + for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { + _sg.gl.cache.color_write_mask[i] = SG_COLORMASK_RGBA; + } + _sg.gl.cache.cull_mode = SG_CULLMODE_NONE; + _sg.gl.cache.face_winding = SG_FACEWINDING_CW; + _sg.gl.cache.sample_count = 1; + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glPolygonOffset(0.0f, 0.0f); + glDisable(GL_POLYGON_OFFSET_FILL); + glDisable(GL_CULL_FACE); + glFrontFace(GL_CW); + glCullFace(GL_BACK); + glEnable(GL_SCISSOR_TEST); + glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); + glEnable(GL_DITHER); + glDisable(GL_POLYGON_OFFSET_FILL); + #if defined(SOKOL_GLCORE33) + glEnable(GL_MULTISAMPLE); + glEnable(GL_PROGRAM_POINT_SIZE); + #endif } _SOKOL_PRIVATE void _sg_gl_setup_backend(const sg_desc* desc) { @@ -6103,15 +6102,14 @@ _SOKOL_PRIVATE void _sg_gl_discard_backend(void) { _SOKOL_PRIVATE void _sg_gl_activate_context(_sg_context_t* ctx) { SOKOL_ASSERT(_sg.gl.valid); - /* NOTE: ctx can be 0 to unset the current context */ - _sg.gl.cur_context = ctx; - _sg_gl_reset_state_cache(); + _sg_gl_reset_state_cache(ctx); } /*-- GL backend resource creation and destruction ----------------------------*/ -_SOKOL_PRIVATE sg_resource_state _sg_gl_create_context(_sg_context_t* ctx) { +_SOKOL_PRIVATE sg_resource_state _sg_gl_create_context(_sg_context_t* ctx, const sg_context_desc* desc) { SOKOL_ASSERT(ctx); SOKOL_ASSERT(0 == ctx->default_framebuffer); + _SOKOL_UNUSED(desc); _SG_GL_CHECK_ERROR(); glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&ctx->default_framebuffer); _SG_GL_CHECK_ERROR(); @@ -6796,9 +6794,10 @@ _SOKOL_PRIVATE _sg_image_t* _sg_gl_pass_ds_image(const _sg_pass_t* pass) { return pass->gl.ds_att.image; } -_SOKOL_PRIVATE void _sg_gl_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) { +_SOKOL_PRIVATE void _sg_gl_begin_pass(_sg_context_t* ctx, _sg_pass_t* pass, const sg_pass_action* action, int w, int h) { /* FIXME: what if a texture used as render target is still bound, should we unbind all currently bound textures in begin pass? */ + SOKOL_ASSERT(ctx); SOKOL_ASSERT(action); SOKOL_ASSERT(!_sg.gl.in_pass); _SG_GL_CHECK_ERROR(); @@ -6824,8 +6823,7 @@ _SOKOL_PRIVATE void _sg_gl_begin_pass(_sg_pass_t* pass, const sg_pass_action* ac } else { /* default pass */ - SOKOL_ASSERT(_sg.gl.cur_context); - glBindFramebuffer(GL_FRAMEBUFFER, _sg.gl.cur_context->default_framebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, ctx->default_framebuffer); } glViewport(0, 0, w, h); glScissor(0, 0, w, h); @@ -6937,7 +6935,8 @@ _SOKOL_PRIVATE void _sg_gl_begin_pass(_sg_pass_t* pass, const sg_pass_action* ac _SG_GL_CHECK_ERROR(); } -_SOKOL_PRIVATE void _sg_gl_end_pass(void) { +_SOKOL_PRIVATE void _sg_gl_end_pass(_sg_context_t* ctx) { + SOKOL_ASSERT(ctx); SOKOL_ASSERT(_sg.gl.in_pass); _SG_GL_CHECK_ERROR(); @@ -6975,8 +6974,7 @@ _SOKOL_PRIVATE void _sg_gl_end_pass(void) { _sg.gl.cur_pass_width = 0; _sg.gl.cur_pass_height = 0; - SOKOL_ASSERT(_sg.gl.cur_context); - glBindFramebuffer(GL_FRAMEBUFFER, _sg.gl.cur_context->default_framebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, ctx->default_framebuffer); _sg.gl.in_pass = false; _SG_GL_CHECK_ERROR(); } @@ -7297,7 +7295,8 @@ _SOKOL_PRIVATE void _sg_gl_apply_bindings( _SG_GL_CHECK_ERROR(); } -_SOKOL_PRIVATE void _sg_gl_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) { +_SOKOL_PRIVATE void _sg_gl_apply_uniforms(_sg_context_t* ctx, sg_shader_stage stage_index, int ub_index, const sg_range* data) { + _SOKOL_UNUSED(ctx); SOKOL_ASSERT(_sg.gl.cache.cur_pipeline); SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->slot.id == _sg.gl.cache.cur_pipeline_id.id); SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->slot.id == _sg.gl.cache.cur_pipeline->cmn.shader_id.id); @@ -7367,8 +7366,9 @@ _SOKOL_PRIVATE void _sg_gl_draw(int base_element, int num_elements, int num_inst } } -_SOKOL_PRIVATE void _sg_gl_commit(void) { +_SOKOL_PRIVATE void _sg_gl_commit(_sg_context_t* ctx) { SOKOL_ASSERT(!_sg.gl.in_pass); + _SOKOL_UNUSED(ctx); /* "soft" clear bindings (only those that are actually bound) */ _sg_gl_cache_clear_buffer_bindings(false); _sg_gl_cache_clear_texture_bindings(false); @@ -12743,7 +12743,7 @@ static inline void _sg_discard_backend(void) { static inline void _sg_reset_state_cache(_sg_context_t* ctx) { #if defined(_SOKOL_ANY_GL) - _sg_gl_reset_state_cache(); + _sg_gl_reset_state_cache(ctx); #elif defined(SOKOL_METAL) _sg_mtl_reset_state_cache(ctx); #elif defined(SOKOL_D3D11) |