Jump to content

  • Log In with Google      Sign In   
  • Create Account


PhillipHamlyn

Member Since 20 Jul 2012
Offline Last Active Jul 03 2014 08:27 AM
-----

#5100293 Unit Testing ftw?

Posted by PhillipHamlyn on 10 October 2013 - 02:12 PM

I'm not a game programmer but I am a commercial systems programmer, and automated unit tests are now stock-in-trade. We tend to use the tools in two ways (we use C#, so nUnit or MSTest are our toolkit)

 

1) To investigate the behaviour of a unit of code when writing it - code the test alongside the unit. You end up doing far less debugging step-through type stuff that way.

2) To formally test the boundary conditions of the code to make sure critical sections (like multithreaded code) works as designed, and in a way which would only be evident at runtime.

 

Both styles of tests are checked in to the source code control system at the same time, and we pump out statistics that give us code test coverage with each build - if it falls below a certain percentage we start to get twitchy - but there is no set limit. Typically code which is both complex (cyclomatic complexity) and not covered by unit tests, is flagged as being a hotspot and we then pick up some additional testing.

 

In my experience, and having come to automated testing late, I wouldn't now code without it. You dont actually need a unit test toolkit since these are just harnesses that allow you to execute a single portion of your code - you could do the same with a parameterised command line application if you wanted to. I'm not dogmatic about TDD - but in my experience programmers work faster and more accurately if they write unit tests at the same time as their line-of-business code. Its actually cheaper, and not just in the long run - any project greater than a couple of days will be made more accurate and more timely if the programmer codes tests at the same time.

 

As an example in the game world, in my hobby terrain project, I used unit tests to isolate and explore the behaviour of my mesh clipping and frustum culling, to make sure it was behaving as I expected. It also means I can semi-confidently change a portion of otherwise complex code, and use my unit tests as a "trigger" to spot any changes to external behaviour that I didn't know about. At its most extreme in the commercial world I've seen a big drop in the amount of paperwork specifications and "explanatory" documents needing to be produced by programmers - although this outcome isn't the goal, the fact that the behaviour of the code is explained in a series of unit tests does help incoming programmers.

 

The last thing to say is that using automated unit tests leads you to write better code - not just less bugs, but more structured, more isolated and more open and allows much more confident maintenance of complex systems, and less time spent in marathon debugging sessions.

 

Phillip




#5097909 How to avoid global state here?

Posted by PhillipHamlyn on 30 September 2013 - 01:26 PM

SeraphLance,

 

I think you might be getting stuck in dogma; sure global state isn't delightful, but its better than the alterative method you've got round to designing (in my opinion). Tracing in C# has a CorrellationManager so you can do correllation of messages from the same class instance. Isn't that what you are after here ?

 

Phillip




#5095985 Who has implemented a licensing API/SDK

Posted by PhillipHamlyn on 22 September 2013 - 12:50 PM

My background in development is entirely commerical/business, not gaming, but in that area I have had to deal with issues of licensing and copyright protection of APIs (not the product itself - just the API). After some thought we ended up developing a trivial protection system using easily-reverse-engineered license keys purchased by the third party developer company. Althought it in no way prevented software theft, it did provide evidence that software theft had taken place, which was good enough for my bosses.

 

Possession of a key issued to a legitimate third party organisation whose content was copyrighted, was in itself a copyright theft. Use of that key was therefore in some way illegal. The important point seemed to be that (a) the software was protected (however trivially) and (b) the method used to gain access to the software prevented casual or mistaken use by a third party. I put the case to my bosses that theft is a law-enforcement issue, all I could do was provide some basic protection such that there could be no defence of ignorance (i.e. "I didn't know it was copyrighted" or "it was open source code I found on the web"). It was made easier for me because we work exclusively in the Microsoft .net world where all code is open and trivially decompiled - just like (for instance) JavaScript - so there is no real technical protection from copyright theft.

 

Interestingly the prevention of theft of the companies IPR was their main reason for investigating web hosting - it prevented the software from ever leaving our premesis, so was inherently secure. Didn't end up being a strong enough reason to actually convert it for the web, which probably reflects more on the weight given to copyright theft over and above, say, user experience.

 

Phillip.




#5085021 Shadow Mapping Directional/Sun Light

Posted by PhillipHamlyn on 11 August 2013 - 02:57 PM

I just used my camera position suitably transformed back down the inverse of the light direction. I vary the absolute distance (i.e. the multiplication factor applied to the light normalized direction) based on size of the view frustum available to the player at any given time - for landscapes I don't shade beyond a given perceived visible range, but in a closed environment you might choose to bring the light source nearer in to get more detail in the shadowing.

 

I understand that the light projection matrix is orthogonal for sunlight, but perhaps not for spot light sources ?

 

Hope this helps.




#5070934 A new job & Intellectual Property protection

Posted by PhillipHamlyn on 18 June 2013 - 02:58 PM

Hmm; in my experience I've seen two types of employment contract;

1) One which says "all software written during the period of employment" belongs to the employer (i.e. irrespective of whether I wrote it in company time).

2) One which says "all software written under the instruction of the employer" belongs to the employer. (i.e. stuff they ask me to write explicitly).

 

I've also been in the position of doing pre-acquisition due-diligence and looking at source code that has multiple copyright claims written into the source code, by former or current employees. Its a real problem then - the employer has to be really diligent that they dont end up with a complex mess of competing claims, so you can see it from their point of view.

 

In my understanding of UK law, if one simply strikes out the offending clause, counter signs the correction, then signs the document, thats the contract that is valid, irrespective if they hold an unblemished, unsigned copy. It takes two parties to agree a contract - they can simply not agree to counter sign your amended contract and there is no contract in force. Although verbal contracts can have considerable weight, I personally would always go for a written one.

 

As all previous posters have said ; talk to a lawyer - this restriction of your contract needn't be a contentious issue with your future employer, they might value you more highly if they consider you "inventive" :0) I imagine this happens a lot where sales people "bring their client list" with them to another employer, they would negotiate some level of recompense for the value of the contacts - in that case they are gifting their new employer with the substance (the contact list) but getting paid for releasing it to them.

 

Good luck with your new job.

 

Phillip




#5053970 What is the difference between these two namespaces?

Posted by PhillipHamlyn on 16 April 2013 - 03:13 PM

The only thing that namespaces do is to segment classes of the same name from each other. The author may structure their namespaces in some logically useful manner, but it doesn't mean anything to the compiler - they just serve to uniquely identify a class.




#5053199 Simplex Noise Texture Lookups?

Posted by PhillipHamlyn on 14 April 2013 - 11:07 AM

Ok, I need to qualify my earlier reply; In XNA4 Vector4 textures can only use Point Sampling in Vertex Shaders. I have no information on whether this is different for SlimDX or other frameworks, so you might be able to rely on the sampler to do the Wrap for you.

 

So I vary the height of the vertex like this;

// Now sample the noise texture to add some variation into the nearby tiles.
float4 noiseCoordinates = (float4)0;
noiseCoordinates.x = heightMapUV.x * (Param_HeightMapWorldSize ) * 10;
noiseCoordinates.y = heightMapUV.y * (Param_HeightMapWorldSize ) * 10; 
noiseCoordinates.z = 0.0f;
noiseCoordinates.w = 0.0f; // First mip map
		
float4 noiseHeight = tex2Dlod(VertexNoiseTextureSampler, noiseCoordinates);
worldPosition.y += (noiseHeight - 0.5f) * 2;



#5052767 Simplex Noise Texture Lookups?

Posted by PhillipHamlyn on 13 April 2013 - 05:56 AM

I do this by generating a simplex based texture as part of my compilation cycyle and use the texture in the game engine just like any other texture. In the shader I sample the noise texture using Wrap and just use the camera world position XZ, suitably scaled up or down depending on the density of the nosie required, as the texture UV parameter. Because Vertex Texture Fetch only supports Point sampling, you need to roll your own Wrap method. (at least this is true for XNA4 and Vector4 textures - other implementations might not have that restriction).




#5033862 GeoClipMap and Point Sampling with Linear Interpolation

Posted by PhillipHamlyn on 18 February 2013 - 01:15 PM

Hi,

 

Just to keep the trail clean, and to answer my own question.

 

Since Point sampling is the only one allowed for a vertex texture fetch (VTF) and it works on nearest point (i.e. round()) not trunc() most examples of manual linear interpolation will not work if the Vertex position moves between frames (such as when you are doing a geoclipmap or other camera-anchored vertexes which then use world oriented data for height lookups or other stuff).

 

Most examples dont really care if the Lerp operation is offset agains the "wrong" pixel in the texture. When you are presenting a camera-anchored vertex buffer then the user will see all sorts of popping and flipping if you dont change the manual linear filter implementation.

 

I haven't tested this thoroughly, but I need to check the actual pixel that is sampled by calculating my texture offset using a trunc() of my initial texture UV * textureSize. I can then safely Lerp() the Frac() of the initial texture UV * textureSize since I know that I have definitely sampled the correct pixel, and not been rounded to the next one (in which case frac() would be useless).

 

I'll post my outcome once I've tested this.




#5018728 XNA Generate Mip Maps with Texture.FromStream()

Posted by PhillipHamlyn on 07 January 2013 - 02:49 PM

// Input Image is a byte[] from your Png
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(this.InputImage);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
Texture2D intermediateTexture = Texture2D.FromStream(deviceAccessor.GetCurrentGraphicsDevice(), memoryStream);

Texture2D texture = null;
RenderTarget2D renderTarget = new RenderTarget2D(deviceAccessor.GetCurrentGraphicsDevice(), InputImageSize.Width, InputImageSize.Height, mipMap: true, preferredFormat: surfaceFormat, preferredDepthFormat: DepthFormat.None);

BlendState blendState = BlendState.Opaque;

currentGraphicsDevice.SetRenderTarget(renderTarget);
using (SpriteBatch sprite = new SpriteBatch(currentGraphicsDevice))
{
sprite.Begin(SpriteSortMode.Immediate, blendState, samplerState, DepthStencilState.None, RasterizerState.CullNone,
effect: null);
sprite.Draw(this.IntermediateTexture, new Vector2(0, 0), Color.White);
sprite.End();
}

texture = (Texture2D)renderTarget;
currentGraphicsDevice.SetRenderTarget(null);
intermediateTexture.Dispose();
return texture;


#5016138 XNA Change RasterizerState or BlendState; defensive programming

Posted by PhillipHamlyn on 31 December 2012 - 01:11 PM

Partly to answer some of my question; I decompiled GraphicsDevice and it does cache the last Set RasterizerState and BlendState and checks to see if you are changing from the cached one. However both RasterizerState and BlendState are classes and therefore the GraphicsDevice only checks that the reference pointer is the same as the last used - and I can't see that the RasterizerState and BlendState classes override the Equals method to compare the content of the states with each other.

 

So - I've got some clarity - XNA will cache the last used reference to a graphics resource, but since you cannot change the content of a graphics resource once committed to the graphics device (i.e. change the graphicsDevice.RasterizerState.CullMode directly) you have to create a new instance of the RasterizerState (or other graphics resouce) meaning that XNA will not pick up that Reference A is equivalent to newly created Reference B, meaning its cache is only marginally beneficial.

 

Do programmers tend to define and manage their own Graphics State caches to aid in defensive programming ?

 

Phillip




#5013939 simple sql question

Posted by PhillipHamlyn on 24 December 2012 - 08:32 AM

Depends on whether you are mostly reading from the DB (in which case, put indexes that cover every Where clause) or the DB has quite a lot of writes (in which case you need to be a little more discerning).

 

However; on balance, the DB is there to be read - you will read it much more often that write to it, so generate indexes that cover all your Where clauses, and only remove them if you find they are not used.

 

Also, generally dont bother indexing a table that will have < 100 rows; most optimisers wont use the index.




#5007202 Big Array to String Speed

Posted by PhillipHamlyn on 04 December 2012 - 03:49 PM

Depends on the framework implementation of String.Join. I expect its StringBuilder internally.

... and using my trusty copy of .Net Reflector I see that it does indeed use StringBuilder internally.


#5007192 Big Array to String Speed

Posted by PhillipHamlyn on 04 December 2012 - 03:29 PM

If its C# or any other .net language you absolutely must use StringBuilder.
Consider - all strings in .net languages are immutable - they cannot be changed once created. So;

A = A + B then
A = A + C then
A = A + D
essentially means creating a new string called A and filling it with A + C. Repeat 10,000 times and you have the memory heap fragmentation from hell.

StringBuilder keeps a list of the pointers to the original strings, calculates the size, and copies them all in one operation, meaning the intermediate instances of A shown above are not created. Hugely faster and more memory efficient.


#5002062 Virtual still the bad way ?

Posted by PhillipHamlyn on 18 November 2012 - 10:29 AM


...

You introduce an indirection which adds an extra method call in means of performance.
Furthermore not using Polymorphism where it is appropriate will increase code size dramatically.

For testing this should imo not be a runtime decision, but a decision at build time.
In C++ you can easily swap in mockup classes in a type hierarchy with #ifdefs or by including different directories for unit/integration tests.
Not sure about C# though.


But inheritence uses a virtual method lookup anyway, so no net loss ?
Dont agree with the code size argument - I dont see a difference in code size either way. Same logic; same compiler.
Agreed you can use Mocking, but inheritence concepts indicate that the inheritor "is interested in" or "knows" about the implementation details of the inherited class and extends that implementation, Mocking replaces the implementation with a different one and therefore invalidates the assumptions on which the inheritence contract was originally made. With a dependency inversion approach, no assumptions are made about the implementation because these are interfaced away leaving only the calling contract.




PARTNERS