Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/intern/gpu_context.cpp
| Show All 30 Lines | |||||
| #include "BLI_assert.h" | #include "BLI_assert.h" | ||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #include "GPU_context.h" | #include "GPU_context.h" | ||||
| #include "GPU_framebuffer.h" | #include "GPU_framebuffer.h" | ||||
| #include "gpu_batch_private.h" | #include "gpu_batch_private.h" | ||||
| #include "gpu_context_private.h" | #include "gpu_context_private.h" | ||||
| #include "gpu_matrix_private.h" | |||||
| #include <vector> | #include <vector> | ||||
| #include <string.h> | #include <string.h> | ||||
| #include <pthread.h> | #include <pthread.h> | ||||
| #include <mutex> | #include <mutex> | ||||
| #include <unordered_set> | #include <unordered_set> | ||||
| #if TRUST_NO_ONE | #if TRUST_NO_ONE | ||||
| Show All 19 Lines | struct GPUContext { | ||||
| GLuint default_vao; | GLuint default_vao; | ||||
| GLuint default_framebuffer; | GLuint default_framebuffer; | ||||
| GPUFrameBuffer *current_fbo; | GPUFrameBuffer *current_fbo; | ||||
| std::unordered_set<GPUBatch *> batches; /* Batches that have VAOs from this context */ | std::unordered_set<GPUBatch *> batches; /* Batches that have VAOs from this context */ | ||||
| #ifdef DEBUG | #ifdef DEBUG | ||||
| std::unordered_set<GPUFrameBuffer *> | std::unordered_set<GPUFrameBuffer *> | ||||
| framebuffers; /* Framebuffers that have FBO from this context */ | framebuffers; /* Framebuffers that have FBO from this context */ | ||||
| #endif | #endif | ||||
| struct GPUMatrixState *matrix_state; | |||||
| std::vector<GLuint> orphaned_vertarray_ids; | std::vector<GLuint> orphaned_vertarray_ids; | ||||
| std::vector<GLuint> orphaned_framebuffer_ids; | std::vector<GLuint> orphaned_framebuffer_ids; | ||||
| std::mutex orphans_mutex; /* todo: try spinlock instead */ | std::mutex orphans_mutex; /* todo: try spinlock instead */ | ||||
| #if TRUST_NO_ONE | #if TRUST_NO_ONE | ||||
| pthread_t thread; /* Thread on which this context is active. */ | pthread_t thread; /* Thread on which this context is active. */ | ||||
| bool thread_is_used; | bool thread_is_used; | ||||
| #endif | #endif | ||||
| ▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| GPUContext *GPU_context_create(GLuint default_framebuffer) | GPUContext *GPU_context_create(GLuint default_framebuffer) | ||||
| { | { | ||||
| /* BLI_assert(thread_is_main()); */ | /* BLI_assert(thread_is_main()); */ | ||||
| GPUContext *ctx = new GPUContext; | GPUContext *ctx = new GPUContext; | ||||
| glGenVertexArrays(1, &ctx->default_vao); | glGenVertexArrays(1, &ctx->default_vao); | ||||
| ctx->default_framebuffer = default_framebuffer; | ctx->default_framebuffer = default_framebuffer; | ||||
| ctx->matrix_state = GPU_matrix_state_create(); | |||||
| GPU_context_active_set(ctx); | GPU_context_active_set(ctx); | ||||
| return ctx; | return ctx; | ||||
| } | } | ||||
| /* to be called after GPU_context_active_set(ctx_to_destroy) */ | /* to be called after GPU_context_active_set(ctx_to_destroy) */ | ||||
| void GPU_context_discard(GPUContext *ctx) | void GPU_context_discard(GPUContext *ctx) | ||||
| { | { | ||||
| /* Make sure no other thread has locked it. */ | /* Make sure no other thread has locked it. */ | ||||
| BLI_assert(ctx == active_ctx); | BLI_assert(ctx == active_ctx); | ||||
| BLI_assert(pthread_equal(pthread_self(), ctx->thread)); | BLI_assert(pthread_equal(pthread_self(), ctx->thread)); | ||||
| BLI_assert(ctx->orphaned_vertarray_ids.empty()); | BLI_assert(ctx->orphaned_vertarray_ids.empty()); | ||||
| #ifdef DEBUG | #ifdef DEBUG | ||||
| /* For now don't allow GPUFrameBuffers to be reuse in another ctx. */ | /* For now don't allow GPUFrameBuffers to be reuse in another ctx. */ | ||||
| BLI_assert(ctx->framebuffers.empty()); | BLI_assert(ctx->framebuffers.empty()); | ||||
| #endif | #endif | ||||
| /* delete remaining vaos */ | /* delete remaining vaos */ | ||||
| while (!ctx->batches.empty()) { | while (!ctx->batches.empty()) { | ||||
| /* this removes the array entry */ | /* this removes the array entry */ | ||||
| GPU_batch_vao_cache_clear(*ctx->batches.begin()); | GPU_batch_vao_cache_clear(*ctx->batches.begin()); | ||||
| } | } | ||||
| GPU_matrix_state_discard(ctx->matrix_state); | |||||
| glDeleteVertexArrays(1, &ctx->default_vao); | glDeleteVertexArrays(1, &ctx->default_vao); | ||||
| delete ctx; | delete ctx; | ||||
| active_ctx = NULL; | active_ctx = NULL; | ||||
| } | } | ||||
| /* ctx can be NULL */ | /* ctx can be NULL */ | ||||
| void GPU_context_active_set(GPUContext *ctx) | void GPU_context_active_set(GPUContext *ctx) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 158 Lines • ▼ Show 20 Lines | |||||
| { | { | ||||
| ctx->current_fbo = fb; | ctx->current_fbo = fb; | ||||
| } | } | ||||
| GPUFrameBuffer *gpu_context_active_framebuffer_get(GPUContext *ctx) | GPUFrameBuffer *gpu_context_active_framebuffer_get(GPUContext *ctx) | ||||
| { | { | ||||
| return ctx->current_fbo; | return ctx->current_fbo; | ||||
| } | } | ||||
| struct GPUMatrixState *gpu_context_active_matrix_state_get() | |||||
| { | |||||
| BLI_assert(active_ctx); | |||||
| return active_ctx->matrix_state; | |||||
| } | |||||