public System.Reflection.Pointer GetID3DXEffectInterface(byte[] shaderCode) { //XNA 4 embeds a small header on the front of the effect code... sigh //header is the following 4 bytes: //207,11,240,188 //the offset of the shader source code //plus uints storing state flag information about each pass in each technique (eek) //then the souce uint passBytes = 2048; //no idea how many passes there are yet, so guess! uint headerSize = passBytes * 4 + 8; byte[] paddedCode = new byte[shaderCode.Length + headerSize]; this.shaderByteCode = paddedCode; byte[] offset = BitConverter.GetBytes(headerSize); paddedCode[0] = 207; paddedCode[1] = 11; paddedCode[2] = 240; paddedCode[3] = 188; paddedCode[4] = offset[0]; paddedCode[5] = offset[1]; paddedCode[6] = offset[2]; paddedCode[7] = offset[3]; for (int i = 0; i < shaderCode.Length; i++) paddedCode[i + headerSize] = shaderCode[i]; try { Effect = new Effect(Graphics.GraphicsDevice, paddedCode); object ptr = Effect.GetType().GetField("pComPtr", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(Effect); return (System.Reflection.Pointer)ptr; } catch (Exception ex) { //an exception can be thrown if the animation generation failed. In which case, compile the shader using XNA to pick out the errors try { string errors; EffectCompiler.CompileEffect(source, filename, buildForXbox, out errors); if (errors != null) { ShaderExtensionGenerator.ThrowGeneratorError(errors, filename); } } catch { } ShaderExtensionGenerator.ThrowGeneratorError(ex.Message, filename); return null; } }
/// <summary> /// Prepare an effect so that it is ready for rendering /// </summary> /// <param name="effect"></param> protected void PrepareEffect(Effect effect) { // We have been passed a base effect object. // Determine its type and call the appropriate PrepareEffect overload. if (effect is BasicEffect) { PrepareEffect((BasicEffect)effect); return; } if (effect is AlphaTestEffect) { PrepareEffect((AlphaTestEffect)effect); return; } if (effect is DualTextureEffect) { PrepareEffect((DualTextureEffect)effect); return; } if (effect is EnvironmentMapEffect) { PrepareEffect((EnvironmentMapEffect)effect); return; } // Not a supported effect throw new NotSupportedException("Cannot prepare effects of type '" + effect.GetType().Name + "', not currently implemented."); }
private static string GetName(Effect effect) { if (effect != null) { if (!string.IsNullOrEmpty(effect.Name)) return effect.Name; var effectType = effect.GetType(); if (effectType != typeof(Effect)) return effectType.Name; } return null; }