internal EffectConstantBuffer(GraphicsDevice device, EffectData.ConstantBuffer description) : base(description.Size) { this.device = device; Description = description; Name = description.Name; Parameters = new EffectParameterCollection(description.Parameters.Count); hashCode = description.GetHashCode(); // Add all parameters to this constant buffer. for (int i = 0; i < description.Parameters.Count; i++) { var parameterRaw = description.Parameters[i]; var parameter = new EffectParameter(parameterRaw, this) { Index = i }; Parameters.Add(parameter); } // By default, all constant buffers are cleared with 0 Clear(); nativeBuffer = ToDispose(Buffer.Constant.New(device, Size)); // The buffer is considered dirty for the first usage. IsDirty = true; }
public EffectConstantBufferKey(EffectData.ConstantBuffer description) { Description = description; HashCode = description.GetHashCode(); }
internal EffectConstantBuffer GetOrCreateConstantBuffer(GraphicsDevice context, EffectData.ConstantBuffer bufferRaw) { // Only lock the constant buffer object lock (mapNameToConstantBuffer) { Dictionary <string, Dictionary <EffectConstantBufferKey, EffectConstantBuffer> > nameToConstantBufferList; // ---------------------------------------------------------------------------- // 1) Get the cache of constant buffers for a particular GraphicsDevice // ---------------------------------------------------------------------------- // TODO cache is not clear if a GraphicsDevice context is disposed // To simplify, we assume that a GraphicsDevice is alive during the whole life of the application. if (!mapNameToConstantBuffer.TryGetValue(context, out nameToConstantBufferList)) { nameToConstantBufferList = new Dictionary <string, Dictionary <EffectConstantBufferKey, EffectConstantBuffer> >(); mapNameToConstantBuffer[context] = nameToConstantBufferList; } // ---------------------------------------------------------------------------- // 2) Get a set of constant buffers for a particular constant buffer name // ---------------------------------------------------------------------------- Dictionary <EffectConstantBufferKey, EffectConstantBuffer> bufferSet; if (!nameToConstantBufferList.TryGetValue(bufferRaw.Name, out bufferSet)) { bufferSet = new Dictionary <EffectConstantBufferKey, EffectConstantBuffer>(); nameToConstantBufferList[bufferRaw.Name] = bufferSet; } // ---------------------------------------------------------------------------- // 3) Get an existing constant buffer having the same name/size/layout/parameters // ---------------------------------------------------------------------------- var bufferKey = new EffectConstantBufferKey(bufferRaw); EffectConstantBuffer buffer; if (!bufferSet.TryGetValue(bufferKey, out buffer)) { // 4) If this buffer doesn't exist, create a new one and register it. buffer = new EffectConstantBuffer(graphicsDevice, bufferRaw); bufferSet[bufferKey] = ToDispose(buffer); } return(buffer); } }
internal EffectConstantBuffer GetOrCreateConstantBuffer(GraphicsDevice context, EffectData.ConstantBuffer bufferRaw) { EffectConstantBuffer constantBuffer; // Is the effect is using shared constant buffers via the EffectPool? if (ShareConstantBuffers) { // Use the pool to share constant buffers constantBuffer = Pool.GetOrCreateConstantBuffer(context, bufferRaw); } else { // ---------------------------------------------------------------------------- // Get an existing constant buffer having the same name/sizel/ayout/parameters // ---------------------------------------------------------------------------- var bufferKey = new EffectConstantBufferKey(bufferRaw); if (!effectConstantBuffersCache.TryGetValue(bufferKey, out constantBuffer)) { // 4) If this buffer doesn't exist, create a new one and register it. constantBuffer = new EffectConstantBuffer(context, bufferRaw); effectConstantBuffersCache.Add(bufferKey, constantBuffer); DisposeCollector.Collect(constantBuffer); } } return(constantBuffer); }