Skip to content

Commit

Permalink
Merge branch 'develop3d' into stable_ARMED!
Browse files Browse the repository at this point in the history
  • Loading branch information
tomspilman committed Oct 26, 2012
2 parents 069c1d0 + 726427f commit 72b7861
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
78 changes: 59 additions & 19 deletions MonoGame.Framework/Graphics/SamplerStateCollection.cs
Expand Up @@ -44,23 +44,37 @@
using System;
using System.Collections.Generic;

#if MONOMAC
using MonoMac.OpenGL;
#elif WINDOWS || LINUX
using OpenTK.Graphics.OpenGL;
#elif GLES
using OpenTK.Graphics.ES20;
using TextureUnit = OpenTK.Graphics.ES20.All;
using TextureTarget = OpenTK.Graphics.ES20.All;
#endif

namespace Microsoft.Xna.Framework.Graphics
{

public sealed class SamplerStateCollection
{
private SamplerState[] _samplers;

private int _dirty;
#if DIRECTX
private int _d3dDirty;
#endif

internal SamplerStateCollection( int maxSamplers )
{
_samplers = new SamplerState[maxSamplers];

for (var i = 0; i < maxSamplers; i++)
_samplers[i] = SamplerState.LinearWrap;

_dirty = int.MaxValue;

#if DIRECTX
_d3dDirty = int.MaxValue;
#endif
}

public SamplerState this [int index]
Expand All @@ -76,54 +90,80 @@ internal SamplerStateCollection( int maxSamplers )
return;

_samplers[index] = value;
_dirty |= 1 << index;

#if DIRECTX
_d3dDirty |= 1 << index;
#endif
}
}

internal void Clear()
{
for (var i = 0; i < _samplers.Length; i++)
_samplers[i] = null;

_dirty = int.MaxValue;

#if DIRECTX
_d3dDirty = int.MaxValue;
#endif
}

internal void SetSamplers(GraphicsDevice device)
{
#if DIRECTX
// Skip out if nothing has changed.
if (_dirty == 0)
if (_d3dDirty == 0)
return;

#if DIRECTX
// NOTE: We make the assumption here that the caller has
// locked the d3dContext for us to use.
var pixelShaderStage = device._d3dContext.PixelShader;
#endif

for (var i = 0; i < _samplers.Length; i++)
{
var mask = 1 << i;
if ((_dirty & mask) == 0)
if ((_d3dDirty & mask) == 0)
continue;

var sampler = _samplers[i];
#if OPENGL
var texture = device.Textures[i];
if (sampler != null && texture != null)
sampler.Activate(texture.glTarget, texture.LevelCount > 1);
#elif DIRECTX
SharpDX.Direct3D11.SamplerState state = null;
if (sampler != null)
state = sampler.GetState(device);

pixelShaderStage.SetSampler(i, state);
#endif

_dirty &= ~mask;
if (_dirty == 0)
_d3dDirty &= ~mask;
if (_d3dDirty == 0)
break;
}

_dirty = 0;
_d3dDirty = 0;

#elif OPENGL

for (var i = 0; i < _samplers.Length; i++)
{
var sampler = _samplers[i];
var texture = device.Textures[i];

if (sampler != null && texture != null && sampler != texture.glLastSamplerState)
{
// TODO: Avoid doing this redundantly (see TextureCollection.SetTextures())
// However, I suspect that rendering from the same texture with different sampling modes
// is a relatively rare occurrence...
GL.ActiveTexture(TextureUnit.Texture0 + i);
GraphicsExtensions.CheckGLError();

// NOTE: We don't have to bind the texture here because it is already bound in
// TextureCollection.SetTextures(). This, of course, assumes that SetTextures() is called
// before this method is called. If that ever changes this code will misbehave.
// GL.BindTexture(texture.glTarget, texture.glTexture);
// GraphicsExtensions.CheckGLError();

sampler.Activate(texture.glTarget, texture.LevelCount > 1);
texture.glLastSamplerState = sampler;
}
}
#endif
}
}
}
5 changes: 4 additions & 1 deletion MonoGame.Framework/Graphics/Texture.cs
Expand Up @@ -71,6 +71,7 @@ public abstract class Texture : GraphicsResource
internal int glTexture = -1;
internal TextureTarget glTarget;
internal TextureUnit glTextureUnit = TextureUnit.Texture0;
internal SamplerState glLastSamplerState = null;
#endif

public SurfaceFormat Format
Expand Down Expand Up @@ -144,7 +145,7 @@ internal int GetPitch(int width)

return pitch;
}


#if DIRECTX

internal SharpDX.Direct3D11.ShaderResourceView GetShaderResourceView()
Expand Down Expand Up @@ -192,6 +193,8 @@ protected override void Dispose(bool disposing)
GraphicsExtensions.CheckGLError();
});
}

glLastSamplerState = null;
#endif
}
base.Dispose(disposing);
Expand Down
3 changes: 2 additions & 1 deletion MonoGame.Framework/Input/MouseState.cs
Expand Up @@ -73,7 +73,8 @@ public struct MouseState
left._y == right._y &&
left._leftButton == right._leftButton &&
left._middleButton == right._middleButton &&
left._rightButton == right._rightButton;
left._rightButton == right._rightButton &&
left._scrollWheelValue == right._scrollWheelValue;
}

public static bool operator !=(MouseState left, MouseState right)
Expand Down

0 comments on commit 72b7861

Please sign in to comment.