/// <summary> /// Initializes a new instance of the <see cref="SpriteBatch" /> class. /// </summary> /// <param name="graphicsDevice">The graphics device.</param> public SpriteBatch(Device graphicsDevice, string debugName) //: base(graphicsDevice, debugName) { //graphicsDevice.DefaultEffectPool.RegisterBytecode(effectBytecode); GraphicsDevice = graphicsDevice; spriteQueue = new SpriteInfo[MaxBatchSize]; spriteTextures = new TextureInfo[MaxBatchSize]; string curdir = System.IO.Directory.GetCurrentDirectory(); System.IO.Directory.SetCurrentDirectory("Content\\Effects2\\Toolkit"); spriteEffect = Effect.FromFile(graphicsDevice, "SpriteEffect.fx", ShaderFlags.None); System.IO.Directory.SetCurrentDirectory(curdir); //spriteEffect.Technique = spriteEffect.GetTechnique(0]; EffectHandle technique = spriteEffect.GetTechnique(0); effectMatrixTransform = spriteEffect.GetParameter(null, "MatrixTransform"); effectTexture = spriteEffect.GetParameter(null, "Texture"); effectCubeTexture = spriteEffect.GetParameter(null, "SpriteTextureCube"); spriteTechnique = spriteEffect.GetTechnique("SpriteBatch"); spriteTechniqueCube0 = spriteEffect.GetTechnique("SpriteBatchCube0"); spriteTechniqueCube1 = spriteEffect.GetTechnique("SpriteBatchCube1"); spriteTechniqueCube2 = spriteEffect.GetTechnique("SpriteBatchCube2"); spriteTechniqueCube3 = spriteEffect.GetTechnique("SpriteBatchCube3"); spriteTechniqueCube4 = spriteEffect.GetTechnique("SpriteBatchCube4"); spriteTechniqueCube5 = spriteEffect.GetTechnique("SpriteBatchCube5"); // Creates the vertex buffer (shared by within a device context). //resourceContext = GraphicsDevice.GetOrCreateSharedData(SharedDataType.PerContext, "SpriteBatch.VertexBuffer", () => new ResourceContext(GraphicsDevice)); VBResourceContext = new ResourceContext(graphicsDevice, debugName); // Creates the index buffer (shared within a Direct3D11 Device) //indexBuffer = GraphicsDevice.GetOrCreateSharedData(SharedDataType.PerDevice, "SpriteBatch.IndexBuffer", () => Buffer.Index.New(GraphicsDevice, indices)); indexBuffer = new IndexBuffer(graphicsDevice, indices.Length * 2, Usage.WriteOnly, Pool.Default, true); indexBuffer.DebugName = "SpriteBatchIB(" + debugName + ")"; indexBuffer.Lock(0, 0, LockFlags.None).WriteRange(indices); indexBuffer.Unlock(); }
/// <summary> /// Begins a sprite batch rendering using the specified sorting mode and blend state, sampler, depth stencil, rasterizer state objects, plus a custom effect and a 2D transformation matrix. Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, DepthStencilState.None, RasterizerState.CullCounterClockwise, SamplerState.LinearClamp). Passing a null effect selects the default SpriteBatch Class shader. /// </summary> /// <param name="sortMode">Sprite drawing order.</param> /// <param name="blendState">Blending options.</param> /// <param name="samplerState">Texture sampling options.</param> /// <param name="depthStencilState">Depth and stencil options.</param> /// <param name="rasterizerState">Rasterization options.</param> /// <param name="effect">Effect state options.</param> /// <param name="transformMatrix">Transformation matrix for scale, rotate, translate options.</param> public void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix) { if (isBeginCalled) { throw new InvalidOperationException("End must be called before begin"); } this.blendState = blendState; this.samplerState = samplerState; this.depthStencilState = depthStencilState; this.rasterizerState = rasterizerState; this.spriteSortMode = sortMode; this.customEffect = effect; this.transformMatrix = transformMatrix; // If custom effect is not null, get all its potential default parameters if (customEffect != null) { customEffectMatrixTransform = customEffect.GetParameter(null, "MatrixTransform"); customEffectTexture = customEffect.GetParameter(null, "Texture"); customEffectSampler = customEffect.GetParameter(null, "TextureSampler"); } // Immediate mode, then prepare for rendering here instead of End() if (sortMode == SpriteSortMode.Immediate) { if (VBResourceContext.IsInImmediateMode) { throw new InvalidOperationException("Only one SpriteBatch at a time can use SpriteSortMode.Immediate"); } PrepareForRendering(); VBResourceContext.IsInImmediateMode = true; } // Sets to true isBeginCalled isBeginCalled = true; }