protected override void Destroy() { for (int i = 0; i < VertexBufferCount; i++) { vertexBuffers[i].Dispose(); } activeVertexBufferIndex = -1; mappedVertexBufferPointer = IntPtr.Zero; if (indexBuffer != null) { indexBuffer.Dispose(); indexBuffer = null; } indexBufferBinding = null; pipelineState = null; if (simpleEffect != null) { simpleEffect.Dispose(); simpleEffect = null; } for (int i = 0; i < VertexBufferCount; i++) { inputElementDescriptions[i] = null; } charsToRenderCount = -1; base.Destroy(); }
/// <summary> /// Initializes a FastTextRendering instance (create and build required ressources, ...). /// </summary> /// <param name="graphicsContext">The current GraphicsContext.</param> private unsafe void Initialize(GraphicsContext graphicsContext, int maxCharacters) { maxCharacterCount = maxCharacters; var indexBufferSize = maxCharacters * 6 * sizeof(int); var indexBufferLength = indexBufferSize / IndexStride; // Map and build the indice buffer indexBuffer = graphicsContext.Allocator.GetTemporaryBuffer(new BufferDescription(indexBufferSize, BufferFlags.IndexBuffer, GraphicsResourceUsage.Dynamic)); var mappedIndices = graphicsContext.CommandList.MapSubresource(indexBuffer, 0, MapMode.WriteNoOverwrite, false, 0, indexBufferSize); var indexPointer = mappedIndices.DataBox.DataPointer; var i = 0; for (var c = 0; c < maxCharacters; c++) { *(int *)(indexPointer + IndexStride * i++) = c * 4 + 0; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 1; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 2; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 1; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 3; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 2; } graphicsContext.CommandList.UnmapSubresource(mappedIndices); indexBufferBinding = new IndexBufferBinding(Buffer.Index.New(graphicsContext.CommandList.GraphicsDevice, new DataPointer(indexPointer, indexBufferSize)), true, indexBufferLength); // Create vertex buffers vertexBuffers = new Buffer[VertexBufferCount]; for (int j = 0; j < VertexBufferCount; j++) { vertexBuffers[j] = Buffer.Vertex.New(graphicsContext.CommandList.GraphicsDevice, new VertexPositionNormalTexture[VertexBufferLength], GraphicsResourceUsage.Dynamic); } vertexBuffersBinding = new VertexBufferBinding[VertexBufferCount]; for (int j = 0; j < VertexBufferCount; j++) { vertexBuffersBinding[j] = new VertexBufferBinding(vertexBuffers[j], VertexPositionNormalTexture.Layout, 0); } inputElementDescriptions = new InputElementDescription[VertexBufferCount][]; for (int j = 0; j < VertexBufferCount; j++) { inputElementDescriptions[j] = vertexBuffersBinding[j].Declaration.CreateInputElements(); } // Create the pipeline state object pipelineState = new MutablePipelineState(graphicsContext.CommandList.GraphicsDevice); pipelineState.State.SetDefaults(); pipelineState.State.InputElements = inputElementDescriptions[0]; pipelineState.State.PrimitiveType = PrimitiveType.TriangleList; // Create the effect simpleEffect = new EffectInstance(new Effect(graphicsContext.CommandList.GraphicsDevice, SpriteEffect.Bytecode)); simpleEffect.Parameters.Set(TexturingKeys.Sampler, graphicsContext.CommandList.GraphicsDevice.SamplerStates.LinearClamp); simpleEffect.UpdateEffect(graphicsContext.CommandList.GraphicsDevice); }
/// <summary> /// Initializes a new instance of the <see cref="PrimitiveQuad" /> class. /// </summary> /// <param name="graphicsDevice">The graphics device.</param> /// <param name="effect">The effect.</param> public PrimitiveQuad(GraphicsDevice graphicsDevice) { GraphicsDevice = graphicsDevice; sharedData = GraphicsDevice.GetOrCreateSharedData(GraphicsDeviceSharedDataType.PerDevice, "PrimitiveQuad::VertexBuffer", d => new SharedData(GraphicsDevice)); simpleEffect = new EffectInstance(new Effect(GraphicsDevice, SpriteEffect.Bytecode)); simpleEffect.Parameters.Set(SpriteBaseKeys.MatrixTransform, Matrix.Identity); simpleEffect.UpdateEffect(graphicsDevice); pipelineState = new MutablePipelineState(GraphicsDevice); pipelineState.State.SetDefaults(); pipelineState.State.InputElements = VertexDeclaration.CreateInputElements(); pipelineState.State.PrimitiveType = PrimitiveType; }