Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/metal/mtl_context.mm
| Show First 20 Lines • Show All 217 Lines • ▼ Show 20 Lines | MTLContext::MTLContext(void *ghost_window, void *ghost_context) | ||||
| /* Bound Samplers struct. */ | /* Bound Samplers struct. */ | ||||
| for (int i = 0; i < MTL_MAX_TEXTURE_SLOTS; i++) { | for (int i = 0; i < MTL_MAX_TEXTURE_SLOTS; i++) { | ||||
| samplers_.mtl_sampler[i] = nil; | samplers_.mtl_sampler[i] = nil; | ||||
| samplers_.mtl_sampler_flags[i] = DEFAULT_SAMPLER_STATE; | samplers_.mtl_sampler_flags[i] = DEFAULT_SAMPLER_STATE; | ||||
| } | } | ||||
| /* Initialize samplers. */ | /* Initialize samplers. */ | ||||
| for (uint i = 0; i < GPU_SAMPLER_MAX; i++) { | for (uint i = 0; i < GPU_SAMPLER_ICON; i++) { | ||||
| MTLSamplerState state; | MTLSamplerState state; | ||||
| state.state = static_cast<eGPUSamplerState>(i); | state.state = static_cast<eGPUSamplerState>(i); | ||||
| sampler_state_cache_[i] = this->generate_sampler_from_state(state); | sampler_state_cache_[i] = this->generate_sampler_from_state(state); | ||||
| } | } | ||||
| /* Special sampler for icons. */ | |||||
| sampler_state_cache_[GPU_SAMPLER_ICON] = this->generate_icon_sampler(); | |||||
| } | } | ||||
| MTLContext::~MTLContext() | MTLContext::~MTLContext() | ||||
| { | { | ||||
| BLI_assert(this == reinterpret_cast<MTLContext *>(GPU_context_active_get())); | BLI_assert(this == reinterpret_cast<MTLContext *>(GPU_context_active_get())); | ||||
| /* Ensure rendering is complete command encoders/command buffers are freed. */ | /* Ensure rendering is complete command encoders/command buffers are freed. */ | ||||
| if (MTLBackend::get()->is_inside_render_boundary()) { | if (MTLBackend::get()->is_inside_render_boundary()) { | ||||
| this->finish(); | this->finish(); | ||||
| ▲ Show 20 Lines • Show All 1,396 Lines • ▼ Show 20 Lines | |||||
| id<MTLSamplerState> MTLContext::get_sampler_from_state(MTLSamplerState sampler_state) | id<MTLSamplerState> MTLContext::get_sampler_from_state(MTLSamplerState sampler_state) | ||||
| { | { | ||||
| BLI_assert((uint)sampler_state >= 0 && ((uint)sampler_state) < GPU_SAMPLER_MAX); | BLI_assert((uint)sampler_state >= 0 && ((uint)sampler_state) < GPU_SAMPLER_MAX); | ||||
| return sampler_state_cache_[(uint)sampler_state]; | return sampler_state_cache_[(uint)sampler_state]; | ||||
| } | } | ||||
| id<MTLSamplerState> MTLContext::generate_sampler_from_state(MTLSamplerState sampler_state) | id<MTLSamplerState> MTLContext::generate_sampler_from_state(MTLSamplerState sampler_state) | ||||
| { | { | ||||
| /* Check if sampler already exists for given state. */ | |||||
| MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init]; | MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init]; | ||||
| descriptor.normalizedCoordinates = true; | descriptor.normalizedCoordinates = true; | ||||
| MTLSamplerAddressMode clamp_type = (sampler_state.state & GPU_SAMPLER_CLAMP_BORDER) ? | MTLSamplerAddressMode clamp_type = (sampler_state.state & GPU_SAMPLER_CLAMP_BORDER) ? | ||||
| MTLSamplerAddressModeClampToBorderColor : | MTLSamplerAddressModeClampToBorderColor : | ||||
| MTLSamplerAddressModeClampToEdge; | MTLSamplerAddressModeClampToEdge; | ||||
| MTLSamplerAddressMode repeat_type = (sampler_state.state & GPU_SAMPLER_MIRROR_REPEAT) ? | MTLSamplerAddressMode repeat_type = (sampler_state.state & GPU_SAMPLER_MIRROR_REPEAT) ? | ||||
| MTLSamplerAddressModeMirrorRepeat : | MTLSamplerAddressModeMirrorRepeat : | ||||
| Show All 19 Lines | id<MTLSamplerState> MTLContext::generate_sampler_from_state(MTLSamplerState sampler_state) | ||||
| float aniso_filter = max_ff(16, U.anisotropic_filter); | float aniso_filter = max_ff(16, U.anisotropic_filter); | ||||
| descriptor.maxAnisotropy = (sampler_state.state & GPU_SAMPLER_MIPMAP) ? aniso_filter : 1; | descriptor.maxAnisotropy = (sampler_state.state & GPU_SAMPLER_MIPMAP) ? aniso_filter : 1; | ||||
| descriptor.compareFunction = (sampler_state.state & GPU_SAMPLER_COMPARE) ? | descriptor.compareFunction = (sampler_state.state & GPU_SAMPLER_COMPARE) ? | ||||
| MTLCompareFunctionLessEqual : | MTLCompareFunctionLessEqual : | ||||
| MTLCompareFunctionAlways; | MTLCompareFunctionAlways; | ||||
| descriptor.supportArgumentBuffers = true; | descriptor.supportArgumentBuffers = true; | ||||
| id<MTLSamplerState> state = [this->device newSamplerStateWithDescriptor:descriptor]; | id<MTLSamplerState> state = [this->device newSamplerStateWithDescriptor:descriptor]; | ||||
| sampler_state_cache_[(uint)sampler_state] = state; | |||||
| BLI_assert(state != nil); | BLI_assert(state != nil); | ||||
| [descriptor autorelease]; | [descriptor autorelease]; | ||||
| return state; | return state; | ||||
| } | } | ||||
| id<MTLSamplerState> MTLContext::generate_icon_sampler() | |||||
| { | |||||
| MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init]; | |||||
| descriptor.minFilter = MTLSamplerMinMagFilterLinear; | |||||
| descriptor.magFilter = MTLSamplerMinMagFilterLinear; | |||||
| descriptor.mipFilter = MTLSamplerMipFilterNearest; | |||||
| descriptor.lodMinClamp = 0; | |||||
| descriptor.lodMaxClamp = 1; | |||||
| id<MTLSamplerState> icon_state = [this->device newSamplerStateWithDescriptor:descriptor]; | |||||
| BLI_assert(icon_state != nil); | |||||
| [descriptor autorelease]; | |||||
| return icon_state; | |||||
| } | |||||
| id<MTLSamplerState> MTLContext::get_default_sampler_state() | id<MTLSamplerState> MTLContext::get_default_sampler_state() | ||||
| { | { | ||||
| if (default_sampler_state_ == nil) { | if (default_sampler_state_ == nil) { | ||||
| default_sampler_state_ = this->get_sampler_from_state(DEFAULT_SAMPLER_STATE); | default_sampler_state_ = this->get_sampler_from_state(DEFAULT_SAMPLER_STATE); | ||||
| } | } | ||||
| return default_sampler_state_; | return default_sampler_state_; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 147 Lines • Show Last 20 Lines | |||||