Skip to content

Commit

Permalink
feature/first-release (#1)
Browse files Browse the repository at this point in the history
* work

* more work

* more work #2

* more work #3

* more work #4

* more work #5

* fix

* try #1

* add test

* break test on purpose test

* break test on purpose

* break test on purpose #2

* break test on purpose #3

* fix test

* break test on purpose #1

* Add badge, improve docs and finalize tests

* improve markdown comment

* fix documentation

* fix documentation #2

* break test on purpose

* revert

* fix doc
  • Loading branch information
silbinarywolf committed Jul 20, 2020
1 parent 81003c3 commit 7a57b21
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Use Unix line endings in all text files.
# This helps prevent inconsistency issues with Windows Git configurations vs Linux
* text=auto eol=lf
49 changes: 49 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Go
on: [push, pull_request]
jobs:
windows:
runs-on: windows-latest
strategy:
matrix:
go: ['1.12', '1.13', '1.14']
name: Go ${{ matrix.go }} on Windows
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Build
shell: bash
run: |
go build -v -o testapp.exe ./testapp
- name: Test
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
dumpbin /exports testapp.exe > dumpbin_output.txt
grep -q AmdPowerXpressRequestHighPerformance "dumpbin_output.txt"
grep -q NvOptimusEnablement "dumpbin_output.txt"
macos:
runs-on: macOS-latest
strategy:
matrix:
go: ['1.12']
name: Go ${{ matrix.go }} on MacOS
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Build
run: |
go build -v -o test ./testapp
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright © 2020 Jae Bentvelzen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
110 changes: 109 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,111 @@
# Prefer Discrete GPU

For laptops with multiple GPUs, its likely that applications won't launch using the discrete GPU on Windows, but rather than integrated GPU. This can lead to realtime applications like games under-performing by default.
[![Actions Status](https://github.com/silbinarywolf/preferdiscretegpu/workflows/Go/badge.svg)](https://github.com/silbinarywolf/preferdiscretegpu/actions)

**This is a CGo package. Unfortunately there is no way to enable this behaviour without CGo as of Go 1.14**

For laptops with multiple GPUs, its likely that applications won't launch using the discrete GPU on Windows, but rather the integrated GPU. This can lead to real-time applications like games under-performing by default, which is not the ideal user experience.

This library exports global variables for NVIDIA and AMD drivers so that they use high performance graphics rendering settings.

## Install

```
go get https://github.com/silbinarywolf/preferdiscretegpu
```

## Requirements

* Golang 1.12+

## How to use

Just import the package for side-effects. This will have no additional behaviour on non-Windows systems.

```go
package main

import (
"fmt"

_ "github.com/silbinarywolf/preferdiscretegpu"
)

func main() {
// your code here
}
```

## How to test if this package is working

1) Open up the Windows Command Line (not Powershell) and run the following:

**NOTE: if you have have an older or newer version of Visual Studio installed, vcvarsall.bat will be in a different folder**
```
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
```

2) Build the test application that consumes this library
```
go build -v -o testapp.exe ./testapp
```

3) Run dumpbin on it:
```
dumpbin /exports testapp.exe
```

4) It should give you an output mentioning the NVIDIA/AMD constants, like so:
```
Microsoft (R) COFF/PE Dumper Version 14.00.24213.1
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file testapp.exe
File Type: EXECUTABLE IMAGE
Section contains the following exports for a.out.exe
00000000 characteristics
5F153BFB time date stamp Mon Jul 20 16:38:51 2020
0.00 version
1 ordinal base
2 number of functions
2 number of names
ordinal hint RVA name
1 0 000B6C00 AmdPowerXpressRequestHighPerformance
2 1 000B6C04 NvOptimusEnablement
Summary
1000 .CRT
30000 .bss
15000 .data
1000 .debug_abbrev
1000 .debug_aranges
14000 .debug_frame
1000 .debug_gdb_scripts
82000 .debug_info
35000 .debug_line
88000 .debug_loc
9000 .debug_pubnames
E000 .debug_pubtypes
30000 .debug_ranges
1000 .debug_str
1000 .edata
1000 .idata
1000 .pdata
C7000 .rdata
A000 .reloc
A1000 .text
1000 .tls
1000 .xdata
```

## Credits

* [Hakan Guleryuz](https://groups.google.com/forum/#!topic/golang-nuts/7OHZcXUegF0) for documenting how they were able to export the NVIDIA/AMD variables in Golang and for giving information
* [SFML Contributors](https://github.com/SFML/SFML/commit/9a453ed9e3846e9f7998295b8966428a9a0b86f6#diff-93134bfcdd8e19cbd5fe05a57a658950R63) for inspiring how this feature should be consumed (ie. opt-in imported package)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/silbinarywolf/preferdiscretegpu

go 1.12
3 changes: 3 additions & 0 deletions preferdiscretegpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package preferdiscretegpu

// do nothing for all non-Windows platforms
3 changes: 3 additions & 0 deletions preferdiscretegpu_windows.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
EXPORTS
NvOptimusEnablement @1 DATA
AmdPowerXpressRequestHighPerformance @2 DATA
9 changes: 9 additions & 0 deletions preferdiscretegpu_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package preferdiscretegpu

/*
#cgo LDFLAGS: -L${SRCDIR}/preferdiscretegpu_windows.def
__declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
*/
import "C"
58 changes: 58 additions & 0 deletions testapp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"

_ "github.com/silbinarywolf/preferdiscretegpu"
)

func main() {
fmt.Printf(`
This application should not be run to determine if the NVIDIA/AMD flags were set.
It should have "dumpbin" run against it and return something like the following:
Dump of file defaultgputest.exe
File Type: EXECUTABLE IMAGE
Section contains the following exports for a.out.exe
00000000 characteristics
5F15303A time date stamp Mon Jul 20 15:48:42 2020
0.00 version
1 ordinal base
2 number of functions
2 number of names
ordinal hint RVA name
1 0 0006B820 AmdPowerXpressRequestHighPerformance
2 1 0006B824 NvOptimusEnablement
Summary
1000 .CRT
30000 .bss
4000 .data
1000 .debug_abbrev
1000 .debug_aranges
C000 .debug_frame
1000 .debug_gdb_scripts
56000 .debug_info
22000 .debug_line
5C000 .debug_loc
3000 .debug_pubnames
A000 .debug_pubtypes
20000 .debug_ranges
1000 .debug_str
1000 .edata
1000 .idata
1000 .pdata
7E000 .rdata
6000 .reloc
67000 .text
1000 .tls
1000 .xdata
`)
}

0 comments on commit 7a57b21

Please sign in to comment.