/// <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); }
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 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; }
protected BatchBase(GraphicsDevice device, EffectBytecode defaultEffectByteCode, EffectBytecode defaultEffectByteCodeSRgb, ResourceBufferInfo resourceBufferInfo, VertexDeclaration vertexDeclaration, int indexSize = sizeof(short)) { if (defaultEffectByteCode == null) { throw new ArgumentNullException(nameof(defaultEffectByteCode)); } if (defaultEffectByteCodeSRgb == null) { throw new ArgumentNullException(nameof(defaultEffectByteCodeSRgb)); } if (resourceBufferInfo == null) { throw new ArgumentNullException("resourceBufferInfo"); } if (vertexDeclaration == null) { throw new ArgumentNullException("vertexDeclaration"); } graphicsDevice = device; mutablePipeline = new MutablePipelineState(device); // TODO GRAPHICS REFACTOR Should we initialize FX lazily? DefaultEffect = new EffectInstance(new Effect(device, defaultEffectByteCode) { Name = "BatchDefaultEffect" }); DefaultEffectSRgb = new EffectInstance(new Effect(device, defaultEffectByteCodeSRgb) { Name = "BatchDefaultEffectSRgb" }); drawsQueue = new ElementInfo[resourceBufferInfo.BatchCapacity]; drawTextures = new Texture[resourceBufferInfo.BatchCapacity]; TextureComparer = new TextureIdComparer(); BackToFrontComparer = new SpriteBackToFrontComparer(); FrontToBackComparer = new SpriteFrontToBackComparer(); // set the vertex layout and size indexStructSize = indexSize; vertexStructSize = vertexDeclaration.CalculateSize(); // Creates the vertex buffer (shared by within a device context). ResourceContext = graphicsDevice.GetOrCreateSharedData(GraphicsDeviceSharedDataType.PerContext, resourceBufferInfo.ResourceKey, d => new DeviceResourceContext(graphicsDevice, vertexDeclaration, resourceBufferInfo)); }