protected BatchBase(GraphicsDevice device, Shaders.EffectBytecode defaultEffectByteCode, ResourceBufferInfo resourceBufferInfo, VertexDeclaration vertexDeclaration, int indexSize = sizeof(short)) { if (resourceBufferInfo == null) { throw new ArgumentNullException("resourceBufferInfo"); } if (vertexDeclaration == null) { throw new ArgumentNullException("vertexDeclaration"); } GraphicsDevice = device; DefaultEffect = new Effect(device, defaultEffectByteCode) { Name = "BatchDefaultEffect" }; drawsQueue = new ElementInfo[resourceBufferInfo.BatchCapacity]; drawTextures = new DrawTextures[resourceBufferInfo.BatchCapacity]; TextureComparer = new TextureIdComparer(); BackToFrontComparer = new SpriteBackToFrontComparer(); FrontToBackComparer = new SpriteFrontToBackComparer(); // set the vertex layout and size indexStructSize = indexSize; this.vertexDeclaration = vertexDeclaration; vertexStructSize = vertexDeclaration.CalculateSize(); parameters = new ParameterCollection(); // Creates the vertex buffer (shared by within a device context). ResourceContext = GraphicsDevice.GetOrCreateSharedData(GraphicsDeviceSharedDataType.PerContext, resourceBufferInfo.ResourceKey, () => new DeviceResourceContext(GraphicsDevice, DefaultEffect, vertexDeclaration, resourceBufferInfo, indexStructSize)); }
public void RecreateBuffers(GraphicsDevice graphicsDevice) { if (requiredQuads == 0) { ResourceContext?.Dispose(); ResourceContext = null; } else { ResourceContext?.Dispose(); ResourceContext = new DeviceResourceContext(graphicsDevice, VertexDeclaration, requiredQuads * verticesPerQuad, indexStructSize, requiredQuads * IndicesPerQuad); } }
public void RecreateBuffers(GraphicsDevice graphicsDevice) { if (requiredQuads == 0) { ResourceContext?.Dispose(); ResourceContext = null; } else { ResourceContext?.Dispose(); ResourceContext = new DeviceResourceContext(graphicsDevice, VertexDeclaration, requiredQuads * verticesPerQuad, indexStructSize, requiredQuads * IndicesPerQuad); } // The default assumption is that every particle defines a separate segment and no segments are shared SetVerticesPerSegment(verticesPerParticle, verticesPerParticle, verticesPerParticle); }
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)); }
/// <summary> /// Initiates a new vertex and index buffers /// </summary> /// <param name="device"><see cref="GraphicsDevice"/> to use</param> /// <param name="vertexCount">Required vertices count. Stride is estimated from the vertex declaration</param> /// <param name="indexCount">Required indices count. Stride is automatically estimated</param> private unsafe void InitBuffer(GraphicsDevice device, int vertexCount, int indexCount) { resourceContext = new DeviceResourceContext(device, VertexDeclaration, vertexCount, indexStructSize, indexCount); vertexStructSize = VertexDeclaration.VertexStride; var mappedIndices = device.MapSubresource(resourceContext.IndexBuffer, 0, MapMode.WriteDiscard, false, 0, indexCount * indexStructSize); var indexPointer = mappedIndices.DataBox.DataPointer; var k = 0; for (var i = 0; i < indexCount; k += verticesPerQuad) { *(short *)(indexPointer + indexStructSize * i++) = (short)(k + 0); *(short *)(indexPointer + indexStructSize * i++) = (short)(k + 1); *(short *)(indexPointer + indexStructSize * i++) = (short)(k + 2); *(short *)(indexPointer + indexStructSize * i++) = (short)(k + 0); *(short *)(indexPointer + indexStructSize * i++) = (short)(k + 2); *(short *)(indexPointer + indexStructSize * i++) = (short)(k + 3); } device.UnmapSubresource(mappedIndices); }