private static SharedModelResources CreateSharedResources(LoadedModelResources loadedResources) { var sharedResources = new SharedModelResources(); sharedResources.Textures = new TextureView[loadedResources.Textures.Length]; for (var textureId = 0; textureId < loadedResources.Textures.Length; textureId++) { var texturePath = loadedResources.Textures[textureId]; if (string.IsNullOrEmpty(texturePath)) { var texture = TextureLoader.GetDummyTexture(GraphicsProvider.GraphicsDevice, GraphicsProvider.ResourceFactory); sharedResources.Textures[textureId] = GraphicsProvider.ResourceFactory.CreateTextureView(texture); } else { if (_cachedTextures.TryGetValue(texturePath, out var textureView)) { sharedResources.Textures[textureId] = textureView; } else { using (var textureStream = GraphicsProvider.Path2TextureStream(texturePath)) { if (textureStream is null) { throw new FileNotFoundException(texturePath); } var texture = TextureLoader.LoadTexture(GraphicsProvider.GraphicsDevice, GraphicsProvider.ResourceFactory, textureStream); textureView = GraphicsProvider.ResourceFactory.CreateTextureView(texture); sharedResources.Textures[textureId] = textureView; if (!string.IsNullOrEmpty(texturePath)) { _cachedTextures.Add(texturePath, textureView); } } } } } sharedResources.VertexBuffers = new DeviceBuffer[loadedResources.GeosetCount]; sharedResources.IndexBuffers = new DeviceBuffer[loadedResources.GeosetCount]; for (var geoset = 0; geoset < loadedResources.GeosetCount; geoset++) { sharedResources.VertexBuffers[geoset] = GraphicsProvider.ResourceFactory.CreateBuffer(new BufferDescription( (uint)loadedResources.Vertices[geoset].Length * _animatedVertexByteSize, BufferUsage.VertexBuffer)); GraphicsProvider.GraphicsDevice.UpdateBuffer(sharedResources.VertexBuffers[geoset], 0, loadedResources.Vertices[geoset]); sharedResources.IndexBuffers[geoset] = GraphicsProvider.ResourceFactory.CreateBuffer(new BufferDescription( loadedResources.IndexCounts[geoset] * _ushortByteSize, BufferUsage.IndexBuffer)); GraphicsProvider.GraphicsDevice.UpdateBuffer(sharedResources.IndexBuffers[geoset], 0, loadedResources.Indices[geoset]); } return(sharedResources); }
private static void DisposeSharedResources(SharedModelResources sharedResources) { foreach (var vertexBuffer in sharedResources.VertexBuffers) { vertexBuffer.Dispose(); } foreach (var indexBuffer in sharedResources.IndexBuffers) { indexBuffer.Dispose(); } }
private void CreateResources(LoadedModelResources?modelResources, string?modelPath) { var isModelCached = _cachedResources.TryGetValue(modelPath ?? string.Empty, out var cachedResources); if (isModelCached) { _loadedResources = cachedResources.LoadedResources; _sharedResources = cachedResources.SharedResources; } else { if (modelResources is null) { using (var modelStream = GraphicsProvider.Path2ModelStream(modelPath)) { if (modelStream is null) { throw new FileNotFoundException(modelPath); } _loadedResources = ModelLoader.LoadModel(modelStream); } } else { _loadedResources = modelResources; } _sharedResources = CreateSharedResources(_loadedResources); if (!string.IsNullOrEmpty(modelPath)) { _cachedResources.Add(modelPath, new CachedModelResources { LoadedResources = _loadedResources, SharedResources = _sharedResources, }); } } _instanceResources = CreateInstanceResources(); }
private void CreateResources(LoadedModelResources?modelResources, string?modelPath) { var isModelCached = _cachedResources.TryGetValue(modelPath ?? string.Empty, out var cachedResources); if (isModelCached) { _loadedResources = cachedResources.LoadedResources; _sharedResources = cachedResources.SharedResources; _batch = _modelBatches[cachedResources]; } else { if (modelResources is null) { using (var modelStream = GraphicsProvider.Path2ModelStream(modelPath !)) { if (modelStream is null) { throw new FileNotFoundException(modelPath); } try { _loadedResources = ModelLoader.LoadModel(modelStream); } catch (Exception e) { throw new InvalidDataException($"Unable to load model: {modelPath}", e); } } } else { _loadedResources = modelResources; } _sharedResources = CreateSharedResources(_loadedResources); cachedResources = new CachedModelResources { LoadedResources = _loadedResources, SharedResources = _sharedResources, }; _batch = new ModelBatch { Models = new HashSet <ModelInstance>(), }; _modelBatches.Add(cachedResources, _batch); if (!string.IsNullOrEmpty(modelPath)) { _cachedResources.Add(modelPath, cachedResources); } } _instanceResources = CreateInstanceResources(); _batch.Models.Add(this); }