• Create Account

## Direct3D9 SetViewport not working properly?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1XGT08  Members

Posted 19 August 2014 - 04:06 AM

Hello,

It seems that no matter what I do, SetViewport in Direct3D9 doesn't work for me. For example, if I call SetViewport to map the viewport to the entire render area (Left:0, Top:0, Width: viewWidth, Height: viewHeight, MinZ: 0.0f, MaxZ: 1.0f), it will only render to approximately a quarter if the render area.

If I maximize the window, it will render the entire width, but a small gap remains at the bottom. If I resize it again to its original dimensions, it will render again in 1/4 area and the backbuffer is not cleared properly.

I would like to mention that I call SetViewport when the window is resized and, also, after the device has been reset. I tried calling SetViewport between BeginScene and EndScene, but with no success.

Has anybody had the same problem?

### #2Buckeye  GDNet+

Posted 19 August 2014 - 07:12 AM

Post your code for setting the window and backbuffer sizes, and for setting the viewport. Include in the code how you determine the size of the window and backbuffer.

My first guess is that you create a window with specified sizes which you use later, assuming those are the width and height of the window client, rather than actually getting the size of the client area.

Edited by Buckeye, 19 August 2014 - 07:15 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #3XGT08  Members

Posted 19 August 2014 - 10:09 AM

Hello,

The code is made a little complicated by the fact that I use a device enumeration class which handles device caps enumeration, window creation etc.

But I can tell you that the size of the viewport is determined when I recieve a WM_SIZE message. So the size of the viewport is:

-Width: LOWORD(lParam)

-Height: HIWORD(lParam)

I checked if the D3DVIEWPORT9 struct instance contains the correct values and indeed it does. Anyway, I will dig a little bit more. I am sure there must be something which I am missing. When I find a solution I will post it here. Thanks!

### #4XGT08  Members

Posted 19 August 2014 - 11:39 AM

Ok. I discovered what the problem was.

I was using a windowed mode device and I was setting the backbuffer dimensions to the screen resolution for a window of 800 by 600. Because the viewport dimensions were being set to the same size and because the backbuffer is much bigger, it makes sense that the backbuffer wasn't being filled properly.

Steps to handle window resize properly:

-set Backbuffer dimensions to 0 in the present params struct instance;

-reset device

OR check out Buckeye's post below ;)

Cheers!

Edited by XGT08, 19 August 2014 - 12:01 PM.

### #5Buckeye  GDNet+

Posted 19 August 2014 - 11:41 AM

I can tell you that the size of the viewport is determined when I recieve a WM_SIZE message

It's the relationship of the viewport to the backbuffer, not the client area, that's likely creating your problem. That is, the size of the viewport (if you want to Present the entire backbuffer) should match the size of the backbuffer, not necessarily the client area size. Commonly,  though, the backbuffer size is reset when the window is resized, and the viewport is set to the size of the backbuffer.

EDIT: cross-posting. Glad you found the problem.

EDIT2:

reset the device every time the window is resized and make sure the bacbuffer dimensions match the window's client area dimensions

The easiest way to ensure that is to set the present parameters BackBufferWidth and BackBufferHeight to 0. Then:

OnLostDevice(); // whatever your routine is.
HR( device->Reset(&present_params) ); // check for errors
OnResetDevice(); // whatever your routine is.
ResetViewportSize(present_params.BackBufferWidth, present_params.BackBufferHeight);


The device will use the current window client size to set the backbuffer. Use the params to set the viewport.

Edited by Buckeye, 19 August 2014 - 11:52 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #6XGT08  Members

Posted 19 August 2014 - 11:55 AM

Hello Buckeye,

Yes, that is entirely true. Your are right. The problem is caused by the fact that I am not using the entire portion of the backbuffer for rendering. It has nothing to do with stretching. I was hoping that nobody managed to read my last post . I was just about to edit it.

And again, you are right about handling thw window resize. Thanks a lot!

Cheers!

### #7Buckeye  GDNet+

Posted 19 August 2014 - 12:03 PM

No problem.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.