Saturday, June 08, 2013

Frogs for dessert

When people ask me what do you do for fun, I sometimes say I love to dine-out. I keep track of my accounts and recently realized that most of my entertainment budget is being spent under the category food. I don’t cook at home and being single it’s more convenient for me to have my meals from a restaurant. Purely for health reason I try to avoid liquor and any substance that would have adverse effect on my health.After all I am a yoga guy. So I am not a guy who can describe even the basic recipe for a cocktail or differentiate between vodka and white rum. Most of favorite restaurants and food items were discovered by chance. This is a sudden surprise I encounter in a small corner restaurant at China Square in Singapore.

9061_10151640404789516_473992597_n

Thursday, August 02, 2012

The Lean Startup

 

Every so often a business book comes along that changes how we think about innovation and entrepreneurship. I have recently stumble across an amazing presentation given by Eric Ries at LSE. He is the author of "The Lean Startup". He defines a start-up as a “human institution designed to create a new product or service under conditions of extreme uncertainty”. The words “human” and “uncertainty” are essential to what follows. A successful start-up does not just rely on a brilliant idea, but also requires managing people through all the challenges of innovation and growth, and through times when the idea will fail and when people will fight over what to do next.

 

Friday, July 20, 2012

The power of small batches (One piece flow versus batch)

When you have only one person in a work cell, what are the benefits of single piece flow? What's the difference between running in batch mode, or running one piece at a time? There's no inventory to build up between operations, so is there a benefit?
The video below shows a simple operation, stuffing envelopes, done with one person. You can compare the single piece flow version with the batch version, since they are being produced side by side.
Although there are four people in the video, they're working independently, so there's really only two processes to compare. On the right, the participants are stuffing envelopes one at a time, finishing each one completely before starting the next envelope.
The people on the left are batching the envelopes. First folding all the papers, then putting them in the envelopes, and then sealing them all.
You can immediately see the difference, with the "one at a time" process producing finished envelopes earlier. There's a huge benefit to the customer, since the lead time to delivery is significantly faster.
This is a simple exercise to perform in a training session using items commonly found in any office.

 

Sunday, April 22, 2012

What Do You Do After You Make a Zillion Dollars

 

When I was searching about Google app revenue I suddenly spotted this article. It’s all about figuring out what to do with your new found fortune! Brilliant advice by James Altucher. He is a famous columnist at Tech Crunch and The Financial Times. After loosing around 15 million dollars in just two years, he is the most suitable candidate to give such an advice. My curiosity led to read about his profile at Wikipedia and found that he is a strong advocate of the idea that one should not own a house. Here is the link for his advice at tech crunch. Facebook Shareholders: What Do You Do After You Make a Zillion Dollars

Sunday, April 01, 2012

The DirectX Graphics Processing Pipeline

 

The Direct3D 9 Graphics Pipeline (also used by XNA) is outlined in the following figure.

simple_pipeline_thumb

Each block in the figure has a specific purpose in the rendering process. Data comes in through the vertex data, primitive data and textures. The vertex data contains untransformed model vertices. These are stored in vertex buffers. The Primitive data specifies geometric primitives, like points, lines, triangles, and polygons. These are defined by groups of vertices from the vertex data, indexed in index buffers. In theory, the tessellation unit converts high-order primitives (like N-patches or displacement maps) to vertices and stores these in vertex buffers. However, there are few  (if any) GPUs that actually implement this functionality. In the newer versions of Direct3D (Direct3D 11), this stage of the pipeline got reborn and is able to do wonderful stuff, but for now, this functionality is not available in XNA.

The vertex processing stage is one of interest. Here, the vertices stored in the vertex buffer are read, and various transformations are applied to them, before sending them forward to the geometry processor. This is where the first type of shaders comes into play: the vertex shaders. When you will be writing vertex shaders, your task will be to transform vertices given in object coordinates into vertices in screen-projected coordinates. You can learn about the different coordinate spaces in the Creator’s Club site’s Education section. For now, remember that vertices read from a vertex buffer are multiplied by some matrices, and end up being projected on the 2D screen. Together with the projected position, other attributes are usually passed, like color, normals, texture coordinates.

In the geometry processing stage, several algorithms are applied on the geometry formed by the transformed vertices. These algorithms include clipping (geometry which is off the screen is removed), back face culling (geometry facing away from the view direction is culled), rasterization (triangles defined by vertices are transformed into pixels). The rasterizer takes three points of a triangle together with their attributes, and interpolates these values for each pixel inside those triangles. These values are passed forward to the pixel processors.

The pixel processing stage is the home of the second type of shader you will learn about, namely the pixel shader. A pixel shader receives as input data coming from the rasterizer. This includes texture coordinates, normals, binormals, and many others. Inside the pixel shader, you need to use this data, together with data read from textures, to output a single color. This color is taken by the pixel rendering stage, and it is modified by alpha blending, depth, and stencil testing, before finally being written into the framebuffer.

This is the general view of the graphics pipeline. It is good to have it in mind, in order to understand where and when each shader is executed.

Friday, December 02, 2011

Saving a WPF Content to a Bitmap

 

WPF has some really handy classes in it.  Have you seen RenderTargetBitmap?  Basically, it's a simple way to capture a bitmap of any WPF Visual.  It works with 3D too:

RenderTargetBitmap bmp = new RenderTargetBitmap(
width, height, 96, 96, PixelFormats.Pbgra32);

bmp.Render(myViewport3D);

Wanna copy that image to the clipboard so you can paste it into some other application?

Clipboard.SetImage(bmp);

Wanna save that image to a PNG file?

PngBitmapEncoder png = new PngBitmapEncoder();

png.Frames.Add(BitmapFrame.Create(bmp));

using (Stream stm = File.Create(filepath))

{

    png.Save(stm);

}

Nifty.  The pictures in this series of blog entries were rendered in just this fashion.  But there are a couple of details worth mentioning.

When using RenderTargetBitmap, you should probably erase it before you draw anything into it:

Rectangle vRect = new Rectangle();

vRect.Width = width;

vRect.Height = height;

vRect.Fill = Brushes.White;

vRect.Arrange(new Rect(0, 0, vRect.Width, vRect.Height));

bmp.Render(visual);

If your Viewport3D was created offscreen, it's not ready to draw.  You need to give it a size (the same as the bitmap you're rendering into) and call Measure() and Arrange() to get it ready:

myViewport3D.Width = width;

myViewport3D.Height = height;

myViewport3D.Measure(new Size(width, height));

myViewport3D.Arrange(new Rect(0, 0, width, height));

Note that none of the above is specific to the 3D features of WPF.  RenderTargetBitmap just works with a Viewport3D like any other visual.  This is one of the best things about WPF:  The 3D features are not special or weird.  They're seamlessly integrated into the framework.

If you have experience programming with some other 3D API such as OpenGL or Direct3D (upon which WPF is built, by the way), you are probably accustomed to thinking of 3D stuff as very distinct from other stuff.  Simple things like getting a 3D graphic to appear in the same window next to a listbox can require all kinds of gymnastics.  WPF doesn't have those sorts of boundaries.  If you want to put an animated 3D scene as the graphic for a toolbar button, you can.

That level of integration is deeply neato. 

Just one more detail about RenderTargetBitmap, and this one is sort-of 3D-specific:

If your Viewport3D was created offscreen and you're using our friend ScreenSpaceLines3D, you'll need to make sure your lines get scaled at least once.  Since I hacked my copy of ScreenSpaceLines3D.cs to remove the use of CompositionTarget.Rendering, I simply call my Rescale() method on every instance just after I call Measure() and Arrange() on the offscreen Viewport3D.

Sunday, July 17, 2011

.NET Volatile

Applying volatile doesn’t prevent a write followed by a read from being swapped, and this can create brainteasers. Joe Duffy illustrates the problem well with the following example: if Test1 and Test2 run simultaneously on different threads, it’s possible for a and b to both end up with a value of 0 (despite the use of volatile on both xand y):

class MyVolatileTest{
  volatile int x, y;
 
  void Test1()        // Executed on one thread
  {
    x = 1;            // Volatile write (release-fence)
    int a = y;        // Volatile read (acquire-fence)
    ...
  }
 
  void Test2()        // Executed on another thread
  {
    y = 1;            // Volatile write (release-fence)
    int b = x;        // Volatile read (acquire-fence)
    ...
  }
}

 

The MSDN documentation states that use of the volatile keyword ensures that the most up-to-date value is present in the field at all times. This is incorrect, since as we’ve seen, a write followed by a read can be reordered

Saturday, June 08, 2013

Frogs for dessert

When people ask me what do you do for fun, I sometimes say I love to dine-out. I keep track of my accounts and recently realized that most of my entertainment budget is being spent under the category food. I don’t cook at home and being single it’s more convenient for me to have my meals from a restaurant. Purely for health reason I try to avoid liquor and any substance that would have adverse effect on my health.After all I am a yoga guy. So I am not a guy who can describe even the basic recipe for a cocktail or differentiate between vodka and white rum. Most of favorite restaurants and food items were discovered by chance. This is a sudden surprise I encounter in a small corner restaurant at China Square in Singapore.

9061_10151640404789516_473992597_n

Thursday, August 02, 2012

The Lean Startup

 

Every so often a business book comes along that changes how we think about innovation and entrepreneurship. I have recently stumble across an amazing presentation given by Eric Ries at LSE. He is the author of "The Lean Startup". He defines a start-up as a “human institution designed to create a new product or service under conditions of extreme uncertainty”. The words “human” and “uncertainty” are essential to what follows. A successful start-up does not just rely on a brilliant idea, but also requires managing people through all the challenges of innovation and growth, and through times when the idea will fail and when people will fight over what to do next.

 

Friday, July 20, 2012

The power of small batches (One piece flow versus batch)

When you have only one person in a work cell, what are the benefits of single piece flow? What's the difference between running in batch mode, or running one piece at a time? There's no inventory to build up between operations, so is there a benefit?
The video below shows a simple operation, stuffing envelopes, done with one person. You can compare the single piece flow version with the batch version, since they are being produced side by side.
Although there are four people in the video, they're working independently, so there's really only two processes to compare. On the right, the participants are stuffing envelopes one at a time, finishing each one completely before starting the next envelope.
The people on the left are batching the envelopes. First folding all the papers, then putting them in the envelopes, and then sealing them all.
You can immediately see the difference, with the "one at a time" process producing finished envelopes earlier. There's a huge benefit to the customer, since the lead time to delivery is significantly faster.
This is a simple exercise to perform in a training session using items commonly found in any office.

 

Sunday, April 22, 2012

What Do You Do After You Make a Zillion Dollars

 

When I was searching about Google app revenue I suddenly spotted this article. It’s all about figuring out what to do with your new found fortune! Brilliant advice by James Altucher. He is a famous columnist at Tech Crunch and The Financial Times. After loosing around 15 million dollars in just two years, he is the most suitable candidate to give such an advice. My curiosity led to read about his profile at Wikipedia and found that he is a strong advocate of the idea that one should not own a house. Here is the link for his advice at tech crunch. Facebook Shareholders: What Do You Do After You Make a Zillion Dollars

Sunday, April 01, 2012

The DirectX Graphics Processing Pipeline

 

The Direct3D 9 Graphics Pipeline (also used by XNA) is outlined in the following figure.

simple_pipeline_thumb

Each block in the figure has a specific purpose in the rendering process. Data comes in through the vertex data, primitive data and textures. The vertex data contains untransformed model vertices. These are stored in vertex buffers. The Primitive data specifies geometric primitives, like points, lines, triangles, and polygons. These are defined by groups of vertices from the vertex data, indexed in index buffers. In theory, the tessellation unit converts high-order primitives (like N-patches or displacement maps) to vertices and stores these in vertex buffers. However, there are few  (if any) GPUs that actually implement this functionality. In the newer versions of Direct3D (Direct3D 11), this stage of the pipeline got reborn and is able to do wonderful stuff, but for now, this functionality is not available in XNA.

The vertex processing stage is one of interest. Here, the vertices stored in the vertex buffer are read, and various transformations are applied to them, before sending them forward to the geometry processor. This is where the first type of shaders comes into play: the vertex shaders. When you will be writing vertex shaders, your task will be to transform vertices given in object coordinates into vertices in screen-projected coordinates. You can learn about the different coordinate spaces in the Creator’s Club site’s Education section. For now, remember that vertices read from a vertex buffer are multiplied by some matrices, and end up being projected on the 2D screen. Together with the projected position, other attributes are usually passed, like color, normals, texture coordinates.

In the geometry processing stage, several algorithms are applied on the geometry formed by the transformed vertices. These algorithms include clipping (geometry which is off the screen is removed), back face culling (geometry facing away from the view direction is culled), rasterization (triangles defined by vertices are transformed into pixels). The rasterizer takes three points of a triangle together with their attributes, and interpolates these values for each pixel inside those triangles. These values are passed forward to the pixel processors.

The pixel processing stage is the home of the second type of shader you will learn about, namely the pixel shader. A pixel shader receives as input data coming from the rasterizer. This includes texture coordinates, normals, binormals, and many others. Inside the pixel shader, you need to use this data, together with data read from textures, to output a single color. This color is taken by the pixel rendering stage, and it is modified by alpha blending, depth, and stencil testing, before finally being written into the framebuffer.

This is the general view of the graphics pipeline. It is good to have it in mind, in order to understand where and when each shader is executed.

Friday, December 02, 2011

Saving a WPF Content to a Bitmap

 

WPF has some really handy classes in it.  Have you seen RenderTargetBitmap?  Basically, it's a simple way to capture a bitmap of any WPF Visual.  It works with 3D too:

RenderTargetBitmap bmp = new RenderTargetBitmap(
width, height, 96, 96, PixelFormats.Pbgra32);

bmp.Render(myViewport3D);

Wanna copy that image to the clipboard so you can paste it into some other application?

Clipboard.SetImage(bmp);

Wanna save that image to a PNG file?

PngBitmapEncoder png = new PngBitmapEncoder();

png.Frames.Add(BitmapFrame.Create(bmp));

using (Stream stm = File.Create(filepath))

{

    png.Save(stm);

}

Nifty.  The pictures in this series of blog entries were rendered in just this fashion.  But there are a couple of details worth mentioning.

When using RenderTargetBitmap, you should probably erase it before you draw anything into it:

Rectangle vRect = new Rectangle();

vRect.Width = width;

vRect.Height = height;

vRect.Fill = Brushes.White;

vRect.Arrange(new Rect(0, 0, vRect.Width, vRect.Height));

bmp.Render(visual);

If your Viewport3D was created offscreen, it's not ready to draw.  You need to give it a size (the same as the bitmap you're rendering into) and call Measure() and Arrange() to get it ready:

myViewport3D.Width = width;

myViewport3D.Height = height;

myViewport3D.Measure(new Size(width, height));

myViewport3D.Arrange(new Rect(0, 0, width, height));

Note that none of the above is specific to the 3D features of WPF.  RenderTargetBitmap just works with a Viewport3D like any other visual.  This is one of the best things about WPF:  The 3D features are not special or weird.  They're seamlessly integrated into the framework.

If you have experience programming with some other 3D API such as OpenGL or Direct3D (upon which WPF is built, by the way), you are probably accustomed to thinking of 3D stuff as very distinct from other stuff.  Simple things like getting a 3D graphic to appear in the same window next to a listbox can require all kinds of gymnastics.  WPF doesn't have those sorts of boundaries.  If you want to put an animated 3D scene as the graphic for a toolbar button, you can.

That level of integration is deeply neato. 

Just one more detail about RenderTargetBitmap, and this one is sort-of 3D-specific:

If your Viewport3D was created offscreen and you're using our friend ScreenSpaceLines3D, you'll need to make sure your lines get scaled at least once.  Since I hacked my copy of ScreenSpaceLines3D.cs to remove the use of CompositionTarget.Rendering, I simply call my Rescale() method on every instance just after I call Measure() and Arrange() on the offscreen Viewport3D.

Sunday, July 17, 2011

.NET Volatile

Applying volatile doesn’t prevent a write followed by a read from being swapped, and this can create brainteasers. Joe Duffy illustrates the problem well with the following example: if Test1 and Test2 run simultaneously on different threads, it’s possible for a and b to both end up with a value of 0 (despite the use of volatile on both xand y):

class MyVolatileTest{
  volatile int x, y;
 
  void Test1()        // Executed on one thread
  {
    x = 1;            // Volatile write (release-fence)
    int a = y;        // Volatile read (acquire-fence)
    ...
  }
 
  void Test2()        // Executed on another thread
  {
    y = 1;            // Volatile write (release-fence)
    int b = x;        // Volatile read (acquire-fence)
    ...
  }
}

 

The MSDN documentation states that use of the volatile keyword ensures that the most up-to-date value is present in the field at all times. This is incorrect, since as we’ve seen, a write followed by a read can be reordered